1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

refactor UpdatesUtils

This commit is contained in:
InsanusMokrassar 2020-03-18 22:56:57 +06:00
parent 71c0b688e8
commit b11da02c8b

View File

@ -21,22 +21,23 @@ internal fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> { internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>() val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseMessageUpdate>>() val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseSentMessageUpdate>>()
for (update in this) { for (update in this) {
if (update !is BaseMessageUpdate) { when (update) {
resultUpdates.add(update) is BaseEditMessageUpdate -> resultUpdates.add(
continue update.toEditMediaGroupUpdate()
} )
val asEditMediaGroupMessage = update.toEditMediaGroupUpdate() is BaseSentMessageUpdate -> {
if (asEditMediaGroupMessage != null) { val data = update.data
resultUpdates.add(asEditMediaGroupMessage) if (data is MediaGroupMessage) {
} else { mediaGroups.getOrPut(data.mediaGroupId) {
val data = update.data mutableListOf()
if (data is MediaGroupMessage) { }.add(update)
(mediaGroups[data.mediaGroupId] ?: mutableListOf<BaseMessageUpdate>().also { mediaGroups[data.mediaGroupId] = it }).add(update) } else {
} else { resultUpdates.add(update)
resultUpdates.add(update) }
} }
else -> resultUpdates.add(update)
} }
} }
mediaGroups.values.map { mediaGroups.values.map {
@ -48,7 +49,7 @@ internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
return resultUpdates return resultUpdates
} }
internal fun List<BaseMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { internal fun List<BaseSentMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {
if (isEmpty()) { if (isEmpty()) {
return@let null return@let null
} }
@ -60,10 +61,10 @@ internal fun List<BaseMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpd
} }
} }
internal fun BaseMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate? = (this as? EditMediaGroupUpdate) ?: let { internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let {
when (this) { when (this) {
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
else -> null else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}")
} }
} }