mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
refactor UpdatesUtils
This commit is contained in:
parent
71c0b688e8
commit
b11da02c8b
@ -21,23 +21,24 @@ 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) {
|
|
||||||
resultUpdates.add(asEditMediaGroupMessage)
|
|
||||||
} else {
|
|
||||||
val data = update.data
|
val data = update.data
|
||||||
if (data is MediaGroupMessage) {
|
if (data is MediaGroupMessage) {
|
||||||
(mediaGroups[data.mediaGroupId] ?: mutableListOf<BaseMessageUpdate>().also { mediaGroups[data.mediaGroupId] = it }).add(update)
|
mediaGroups.getOrPut(data.mediaGroupId) {
|
||||||
|
mutableListOf()
|
||||||
|
}.add(update)
|
||||||
} else {
|
} else {
|
||||||
resultUpdates.add(update)
|
resultUpdates.add(update)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else -> resultUpdates.add(update)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mediaGroups.values.map {
|
mediaGroups.values.map {
|
||||||
it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate ->
|
it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate ->
|
||||||
@ -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}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user