1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-23 00:57:13 +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> {
val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseMessageUpdate>>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseSentMessageUpdate>>()
for (update in this) {
if (update !is BaseMessageUpdate) {
resultUpdates.add(update)
continue
}
val asEditMediaGroupMessage = update.toEditMediaGroupUpdate()
if (asEditMediaGroupMessage != null) {
resultUpdates.add(asEditMediaGroupMessage)
} else {
val data = update.data
if (data is MediaGroupMessage) {
(mediaGroups[data.mediaGroupId] ?: mutableListOf<BaseMessageUpdate>().also { mediaGroups[data.mediaGroupId] = it }).add(update)
} else {
resultUpdates.add(update)
when (update) {
is BaseEditMessageUpdate -> resultUpdates.add(
update.toEditMediaGroupUpdate()
)
is BaseSentMessageUpdate -> {
val data = update.data
if (data is MediaGroupMessage) {
mediaGroups.getOrPut(data.mediaGroupId) {
mutableListOf()
}.add(update)
} else {
resultUpdates.add(update)
}
}
else -> resultUpdates.add(update)
}
}
mediaGroups.values.map {
@ -48,7 +49,7 @@ internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
return resultUpdates
}
internal fun List<BaseMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {
internal fun List<BaseSentMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {
if (isEmpty()) {
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) {
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
else -> null
else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}")
}
}