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

clear methodology of converting common updates to media group updates

This commit is contained in:
InsanusMokrassar 2019-03-14 16:30:36 +08:00
parent 94e88ddc37
commit 26c67b2f99
3 changed files with 15 additions and 18 deletions

View File

@ -38,28 +38,28 @@ data class UpdatesFilter(
when (update) { when (update) {
is MessageUpdate -> messageCallback ?.invoke(update) is MessageUpdate -> messageCallback ?.invoke(update)
is List<*> -> when (update.firstOrNull()) { is List<*> -> when (update.firstOrNull()) {
is MessageUpdate, is MessageMediaGroupUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList -> is MessageUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList ->
messageMediaGroupCallback ?.also { receiver -> messageMediaGroupCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
} ?: messageCallback ?.also { receiver -> } ?: messageCallback ?.also { receiver ->
mappedList.forEach { receiver(it) } mappedList.forEach { receiver(it) }
} }
} }
is EditMessageUpdate, is EditMessageMediaGroupUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList -> is EditMessageUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList ->
editedMessageMediaGroupCallback ?.also { receiver -> editedMessageMediaGroupCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
} ?: editedMessageCallback ?.also { receiver -> } ?: editedMessageCallback ?.also { receiver ->
mappedList.forEach { receiver(it) } mappedList.forEach { receiver(it) }
} }
} }
is ChannelPostUpdate, is ChannelPostMediaGroupUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList -> is ChannelPostUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList ->
channelPostMediaGroupCallback ?.also { receiver -> channelPostMediaGroupCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
} ?: channelPostCallback ?.also { receiver -> } ?: channelPostCallback ?.also { receiver ->
mappedList.forEach { receiver(it) } mappedList.forEach { receiver(it) }
} }
} }
is EditChannelPostUpdate, is EditChannelPostMediaGroupUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList -> is EditChannelPostUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList ->
editedChannelPostMediaGroupCallback ?.also { receiver -> editedChannelPostMediaGroupCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
} ?: editedChannelPostCallback ?.also { receiver -> } ?: editedChannelPostCallback ?.also { receiver ->

View File

@ -3,11 +3,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -19,7 +18,7 @@ class UpdatesPoller(
private val block: UpdateReceiver<Any> private val block: UpdateReceiver<Any>
) { ) {
private var lastHandledUpdate: UpdateIdentifier = 0L private var lastHandledUpdate: UpdateIdentifier = 0L
private val mediaGroup: MutableList<MediaGroupUpdate> = mutableListOf() private val mediaGroup: MutableList<BaseMessageUpdate> = mutableListOf()
private var pollerJob: Job? = null private var pollerJob: Job? = null
@ -36,14 +35,14 @@ class UpdatesPoller(
} }
} }
private suspend fun pushMediaGroupUpdate(mediaGroupUpdate: MediaGroupUpdate? = null) { private suspend fun pushMediaGroupUpdate(mediaGroupUpdate: BaseMessageUpdate? = null) {
val inputMediaGroupId = mediaGroupUpdate ?.data ?.mediaGroupId val inputMediaGroupId = (mediaGroupUpdate ?.data as? MediaGroupMessage) ?.mediaGroupId
if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) { if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) {
sendToBlock(listOf(*mediaGroup.toTypedArray())) sendToBlock(listOf(*mediaGroup.toTypedArray()))
mediaGroup.clear() mediaGroup.clear()
} }
mediaGroupUpdate ?.let { inputMediaGroupId ?.let {
mediaGroup.add(it) mediaGroup.add(mediaGroupUpdate)
} }
} }
@ -60,9 +59,8 @@ class UpdatesPoller(
private suspend fun handleUpdates(updates: List<Update>) { private suspend fun handleUpdates(updates: List<Update>) {
updates.forEach { update -> updates.forEach { update ->
val mediaGroupUpdate = (update as? BaseMessageUpdate) ?.toMediaGroupUpdate() (update as? BaseMessageUpdate) ?.let {
mediaGroupUpdate ?.let { _ -> pushMediaGroupUpdate(it)
pushMediaGroupUpdate(mediaGroupUpdate)
} ?:let { } ?:let {
pushMediaGroupUpdate() pushMediaGroupUpdate()
sendToBlock(update) sendToBlock(update)

View File

@ -6,6 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import io.ktor.application.Application import io.ktor.application.Application
import io.ktor.application.call import io.ktor.application.call
@ -17,8 +18,6 @@ import io.ktor.server.netty.Netty
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.io.FileInputStream
import java.security.KeyStore
import java.util.concurrent.Executors import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -121,7 +120,7 @@ suspend fun RequestsExecutor.setWebhook(
} }
launch { launch {
for (mediaGroupUpdate in mediaGroupAccumulatedChannel) { for (mediaGroupUpdate in mediaGroupAccumulatedChannel) {
block(mediaGroupUpdate.second) block(mediaGroupUpdate.second.mapNotNull { (it as? BaseMessageUpdate) })
} }
} }
engine.start(false) engine.start(false)