All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.github.insanusmokrassar.TelegramBotAPI.utils.BaseMessageUpdateToMediaGroupUpdate.kt Maven / Gradle / Ivy

There is a newer version: 0.28.3
Show newest version
package com.github.insanusmokrassar.TelegramBotAPI.utils

import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update

private inline val Pair.message
    get() = first

internal fun List.convertWithMediaGroupUpdates(): List {
    val resultUpdates = mutableListOf()
    val mediaGroups = mutableMapOf>()
    for (update in this) {
        val asEditMediaGroupMessage = update.toEditMediaGroupUpdate()
        if (asEditMediaGroupMessage != null) {
            resultUpdates.add(asEditMediaGroupMessage)
        } else {
            val data = update.data
            if (data is MediaGroupMessage) {
                (mediaGroups[data.mediaGroupId] ?: mutableListOf().also { mediaGroups[data.mediaGroupId] = it }).add(update)
            } else {
                resultUpdates.add(update)
            }
        }
    }
    mediaGroups.values.map {
        it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate ->
            resultUpdates.add(mediaGroupUpdate)
        }
    }
    return resultUpdates.sortedBy { it.updateId }
}

internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {
    if (isEmpty()) {
        return@let null
    }
    val resultList = sortedBy { it.updateId }
    when (first()) {
        is MessageUpdate -> MessageMediaGroupUpdate(resultList)
        is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(resultList)
        else -> null
    }
}

internal fun BaseMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate? = (this as? EditMediaGroupUpdate) ?: let {
    when (this) {
        is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
        is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
        else -> null
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy