From 2c33c876635840e3f392a73018219a4f4a6240a5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 Mar 2019 07:24:10 +0800 Subject: [PATCH 1/4] start 0.12.2 --- CHANGELOG.md | 2 ++ build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad45e81a32..d2eba3cbcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ * Added additional media group types (like `MessageMediaGroupUpdate`) * Fixed handling of media group updates in `UpdatesFilter` +### 0.12.2 + ## 0.11.0 * Kotlin `1.3.11` -> `1.3.21` diff --git a/build.gradle b/build.gradle index b96204cf28..ce0d1adb5c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -project.version = "0.12.1" +project.version = "0.12.2" project.group = "com.github.insanusmokrassar" buildscript { From 1f0af84adcefe962d318a657d928963dbfab5853 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 Mar 2019 07:47:46 +0800 Subject: [PATCH 2/4] rewrite structure of media groups updates --- .../ChannelPostMediaGroupUpdate.kt | 12 ++-- .../EditChannelPostMediaGroupUpdate.kt | 10 ++- .../EditMessageMediaGroupUpdate.kt | 12 ++-- .../MediaGroupUpdates/MediaGroupUpdate.kt | 6 +- .../MessageMediaGroupUpdate.kt | 10 ++- .../BaseMessageUpdateToMediaGroupUpdate.kt | 7 +- .../utils/extensions/UpdatesFilter.kt | 69 +++++++++---------- .../utils/extensions/UpdatesPoller.kt | 21 +++--- .../utils/extensions/UpdatesPolling.kt | 15 ++-- .../utils/extensions/Webhooks.kt | 13 ++-- 10 files changed, 85 insertions(+), 90 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt index 38d07b2ac9..4e1f82900f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt @@ -4,13 +4,11 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.update.ChannelPostUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class ChannelPostMediaGroupUpdate( - override val updateId: UpdateIdentifier, - override val data: MediaGroupMessage + override val origins: List ) : MediaGroupUpdate { - constructor(sourceUpdate: ChannelPostUpdate) : this( - sourceUpdate.updateId, - sourceUpdate.data as MediaGroupMessage - ) -} \ No newline at end of file + override val updateId: UpdateIdentifier = origins.last().updateId + override val data: List = origins.mapNotNull { it.data as? MediaGroupMessage } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt index dc90196f52..82d9bae6fc 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt @@ -3,13 +3,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.update.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class EditChannelPostMediaGroupUpdate( - override val updateId: UpdateIdentifier, - override val data: MediaGroupMessage + override val origins: List ) : MediaGroupUpdate { - constructor(sourceUpdate: EditChannelPostUpdate) : this( - sourceUpdate.updateId, - sourceUpdate.data as MediaGroupMessage - ) + override val updateId: UpdateIdentifier = origins.last().updateId + override val data: List = origins.mapNotNull { it.data as? MediaGroupMessage } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt index 529703f845..c5c9158e6f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt @@ -4,13 +4,11 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.update.EditMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class EditMessageMediaGroupUpdate( - override val updateId: UpdateIdentifier, - override val data: MediaGroupMessage + override val origins: List ) : MediaGroupUpdate { - constructor(sourceUpdate: EditMessageUpdate) : this( - sourceUpdate.updateId, - sourceUpdate.data as MediaGroupMessage - ) -} \ No newline at end of file + override val updateId: UpdateIdentifier = origins.last().updateId + override val data: List = origins.mapNotNull { it.data as? MediaGroupMessage } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt index 1e36a565d0..cccce9247b 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt @@ -2,7 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.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.Update -interface MediaGroupUpdate: BaseMessageUpdate { - override val data: MediaGroupMessage +interface MediaGroupUpdate: Update { + override val data: List + val origins: List } diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt index c10024551a..57fa89c54c 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt @@ -3,13 +3,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class MessageMediaGroupUpdate( - override val updateId: UpdateIdentifier, - override val data: MediaGroupMessage + override val origins: List ) : MediaGroupUpdate { - constructor(sourceUpdate: MessageUpdate) : this( - sourceUpdate.updateId, - sourceUpdate.data as MediaGroupMessage - ) + override val updateId: UpdateIdentifier = origins.last().updateId + override val data: List = origins.mapNotNull { it.data as? MediaGroupMessage } } \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BaseMessageUpdateToMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BaseMessageUpdateToMediaGroupUpdate.kt index 27413b5dd0..e94d4af561 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BaseMessageUpdateToMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/BaseMessageUpdateToMediaGroupUpdate.kt @@ -1,16 +1,15 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils -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 -fun BaseMessageUpdate.toMediaGroupUpdate(): MediaGroupUpdate? = (this as? MediaGroupUpdate) ?: ((data as? MediaGroupMessage) ?.let { - when (this) { +fun List.toMediaGroupUpdate(): MediaGroupUpdate? = (this as? MediaGroupUpdate) ?: let { + when (first()) { is MessageUpdate -> MessageMediaGroupUpdate(this) is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(this) is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) else -> null } -}) +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt index 1892a48460..a15a6a931a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt @@ -4,24 +4,25 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.* 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 import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate data class UpdatesFilter( private val messageCallback: UpdateReceiver? = null, - private val messageMediaGroupCallback: UpdateReceiver>? = null, + private val messageMediaGroupCallback: UpdateReceiver? = null, private val editedMessageCallback: UpdateReceiver? = null, - private val editedMessageMediaGroupCallback: UpdateReceiver>? = null, + private val editedMessageMediaGroupCallback: UpdateReceiver? = null, private val channelPostCallback: UpdateReceiver? = null, - private val channelPostMediaGroupCallback: UpdateReceiver>? = null, + private val channelPostMediaGroupCallback: UpdateReceiver? = null, private val editedChannelPostCallback: UpdateReceiver? = null, - private val editedChannelPostMediaGroupCallback: UpdateReceiver>? = null, + private val editedChannelPostMediaGroupCallback: UpdateReceiver? = null, private val chosenInlineResultCallback: UpdateReceiver? = null, private val inlineQueryCallback: UpdateReceiver? = null, private val callbackQueryCallback: UpdateReceiver? = null, private val shippingQueryCallback: UpdateReceiver? = null, private val preCheckoutQueryCallback: UpdateReceiver? = null ) { - val asUpdateReceiver: UpdateReceiver = this::invoke + val asUpdateReceiver: UpdateReceiver = this::invoke val allowedUpdates = listOfNotNull( (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, @@ -34,42 +35,40 @@ data class UpdatesFilter( preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY } ) - suspend fun invoke(update: Any) { + suspend fun invoke(update: Update) { when (update) { is MessageUpdate -> messageCallback ?.invoke(update) - is List<*> -> when (update.firstOrNull()) { - is MessageUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList -> - messageMediaGroupCallback ?.also { receiver -> - receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) - } ?: messageCallback ?.also { receiver -> - mappedList.forEach { receiver(it) } - } - } - is EditMessageUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList -> - editedMessageMediaGroupCallback ?.also { receiver -> - receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) - } ?: editedMessageCallback ?.also { receiver -> - mappedList.forEach { receiver(it) } - } - } - is ChannelPostUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList -> - channelPostMediaGroupCallback ?.also { receiver -> - receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) - } ?: channelPostCallback ?.also { receiver -> - mappedList.forEach { receiver(it) } - } - } - is EditChannelPostUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList -> - editedChannelPostMediaGroupCallback ?.also { receiver -> - receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) - } ?: editedChannelPostCallback ?.also { receiver -> - mappedList.forEach { receiver(it) } - } + is MessageMediaGroupUpdate -> messageMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: messageCallback ?.also { receiver -> + update.origins.mapNotNull { it as? MessageUpdate }.forEach { + receiver(it) } } is EditMessageUpdate -> editedMessageCallback ?.invoke(update) + is EditMessageMediaGroupUpdate -> editedMessageMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: editedMessageCallback ?.also { receiver -> + update.origins.mapNotNull { it as? EditMessageUpdate }.forEach { + receiver(it) + } + } is ChannelPostUpdate -> channelPostCallback ?.invoke(update) + is ChannelPostMediaGroupUpdate -> channelPostMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: channelPostCallback ?.also { receiver -> + update.origins.mapNotNull { it as? ChannelPostUpdate }.forEach { + receiver(it) + } + } is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update) + is EditChannelPostMediaGroupUpdate -> editedChannelPostMediaGroupCallback ?.also { receiver -> + receiver(update) + } ?: editedChannelPostCallback ?.also { receiver -> + update.origins.mapNotNull { it as? EditChannelPostUpdate }.forEach { + receiver(it) + } + } is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update) is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update) is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update) @@ -81,7 +80,7 @@ data class UpdatesFilter( fun createSimpleUpdateFilter( messageCallback: UpdateReceiver? = null, - mediaGroupCallback: UpdateReceiver>? = null, + mediaGroupCallback: UpdateReceiver? = null, editedMessageCallback: UpdateReceiver? = null, channelPostCallback: UpdateReceiver? = null, editedChannelPostCallback: UpdateReceiver? = null, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt index f32795c25d..3009dced12 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPoller.kt @@ -7,6 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId +import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate import kotlinx.coroutines.* import java.util.concurrent.Executors @@ -15,30 +16,26 @@ class UpdatesPoller( private val requestsDelayMillis: Long = 1000, private val scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), private val allowedUpdates: List? = null, - private val block: UpdateReceiver + private val block: UpdateReceiver ) { private var lastHandledUpdate: UpdateIdentifier = 0L private val mediaGroup: MutableList = mutableListOf() private var pollerJob: Job? = null - private suspend fun sendToBlock(data: Any) { + private suspend fun sendToBlock(data: Update) { block(data) - lastHandledUpdate = when (data) { - is Update -> data.updateId - is List<*> -> (data.last() as? Update) ?.updateId ?: throw IllegalStateException( - "Found non-updates oriented list" - ) - else -> throw IllegalStateException( - "Unknown type of data" - ) - } + lastHandledUpdate = data.updateId } private suspend fun pushMediaGroupUpdate(update: BaseMessageUpdate? = null) { val inputMediaGroupId = (update ?.data as? MediaGroupMessage) ?.mediaGroupId if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) { - sendToBlock(listOf(*mediaGroup.toTypedArray())) + listOf(*mediaGroup.toTypedArray()).toMediaGroupUpdate() ?.let { + sendToBlock(it) + } ?: mediaGroup.forEach { + sendToBlock(it) + } mediaGroup.clear() } inputMediaGroupId ?.let { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt index 81cfa0b971..49ae28603e 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt @@ -2,8 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.types.update.* -import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate +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 import kotlinx.coroutines.* import java.util.concurrent.Executors @@ -13,20 +14,20 @@ fun RequestsExecutor.startGettingOfUpdates( requestsDelayMillis: Long = 1000, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), allowedUpdates: List? = null, - block: UpdateReceiver + block: UpdateReceiver ): Job { return UpdatesPoller(this, requestsDelayMillis, scope, allowedUpdates, block).start() } fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, - messageMediaGroupCallback: UpdateReceiver>? = null, + messageMediaGroupCallback: UpdateReceiver? = null, editedMessageCallback: UpdateReceiver? = null, - editedMessageMediaGroupCallback: UpdateReceiver>? = null, + editedMessageMediaGroupCallback: UpdateReceiver? = null, channelPostCallback: UpdateReceiver? = null, - channelPostMediaGroupCallback: UpdateReceiver>? = null, + channelPostMediaGroupCallback: UpdateReceiver? = null, editedChannelPostCallback: UpdateReceiver? = null, - editedChannelPostMediaGroupCallback: UpdateReceiver>? = null, + editedChannelPostMediaGroupCallback: UpdateReceiver? = null, chosenInlineResultCallback: UpdateReceiver? = null, inlineQueryCallback: UpdateReceiver? = null, callbackQueryCallback: UpdateReceiver? = null, @@ -60,7 +61,7 @@ fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, - mediaGroupCallback: UpdateReceiver>? = null, + mediaGroupCallback: UpdateReceiver? = null, editedMessageCallback: UpdateReceiver? = null, channelPostCallback: UpdateReceiver? = null, editedChannelPostCallback: UpdateReceiver? = null, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 8201dbf37d..1e848cfa96 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -8,6 +8,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG 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.utils.toMediaGroupUpdate import io.ktor.application.Application import io.ktor.application.call import io.ktor.request.receiveText @@ -41,7 +42,7 @@ suspend fun RequestsExecutor.setWebhook( allowedUpdates: List? = null, maxAllowedConnections: Int? = null, engineFactory: ApplicationEngineFactory<*, *> = Netty, - block: UpdateReceiver + block: UpdateReceiver ): Job { val executeDeferred = certificate ?.let { executeAsync( @@ -60,7 +61,7 @@ suspend fun RequestsExecutor.setWebhook( ) ) val updatesChannel = Channel(Channel.UNLIMITED) - val mediaGroupChannel = Channel>(Channel.UNLIMITED) + val mediaGroupChannel = Channel>(Channel.UNLIMITED) val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( 1000L, scope = scope @@ -113,14 +114,18 @@ suspend fun RequestsExecutor.setWebhook( for (update in updatesChannel) { val data = update.data when (data) { - is MediaGroupMessage -> mediaGroupChannel.send(data.mediaGroupId to update) + is MediaGroupMessage -> mediaGroupChannel.send(data.mediaGroupId to update as BaseMessageUpdate) else -> block(update) } } } launch { for (mediaGroupUpdate in mediaGroupAccumulatedChannel) { - block(mediaGroupUpdate.second.mapNotNull { (it as? BaseMessageUpdate) }) + mediaGroupUpdate.second.toMediaGroupUpdate() ?.let { + block(it) + } ?: mediaGroupUpdate.second.forEach { + block(it) + } } } engine.start(false) From 2bb4129901f6ad152e72bf375f6458b2ae9b2599 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 Mar 2019 08:16:01 +0800 Subject: [PATCH 3/4] update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2eba3cbcc..abff925ce9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,14 @@ ### 0.12.2 +* New in `MediaGroupUpdate`: + * It is subtype of `Update` and can be use as regular update with list of messages + * Data now is list with `MediaGroupMessage` + * Added field `origins` which represent origin updates for `MediaGroupMessage` + * `updateId` now represent LAST id of origins updates +* `UpdatesFilter` and other objects now work with `UpdateReceiver` as common supertype +for receivers. + ## 0.11.0 * Kotlin `1.3.11` -> `1.3.21` From c9e72920df07cc289bed06ce6f19236d33bb4af4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 Mar 2019 16:12:17 +0800 Subject: [PATCH 4/4] optimize imports --- .../bot/Ktor/base/MultipartRequestCallFactory.kt | 1 - .../insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt | 2 +- .../TelegramBotAPI/requests/abstracts/InputFile.kt | 2 -- .../TelegramBotAPI/requests/abstracts/SimpleRequest.kt | 4 +--- .../TelegramBotAPI/requests/send/media/base/DataRequest.kt | 4 +--- .../TelegramBotAPI/requests/webhook/GetWebhookInfo.kt | 3 ++- .../TelegramBotAPI/requests/webhook/SetWebhook.kt | 3 ++- .../update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt | 2 -- .../MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt | 1 - .../update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt | 2 -- .../update/MediaGroupUpdates/MessageMediaGroupUpdate.kt | 1 - .../github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt | 3 ++- .../TelegramBotAPI/utils/extensions/UpdatesFilter.kt | 2 -- .../TelegramBotAPI/utils/extensions/UpdatesPolling.kt | 1 - .../TelegramBotAPI/utils/extensions/Webhooks.kt | 6 +++--- 15 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt index 41d4f8be9e..b4b992ab7f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/MultipartRequestCallFactory.kt @@ -11,7 +11,6 @@ import io.ktor.client.request.forms.MultiPartFormDataContent import io.ktor.client.request.forms.formData import io.ktor.client.request.url import io.ktor.http.* -import kotlinx.io.streams.asInput import kotlinx.serialization.ImplicitReflectionSerializer class MultipartRequestCallFactory : KtorCallFactory { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt index a491568b30..9b55e4c99f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt @@ -1,8 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest -import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST +import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import kotlinx.serialization.* import kotlinx.serialization.internal.ArrayListSerializer diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt index 869e1663e2..7926308dbf 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt @@ -3,8 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile import kotlinx.serialization.* import java.io.File -import java.nio.file.Files -import java.util.* sealed class InputFile { abstract val fileId: String diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt index 5a0a8f7f02..d25f5a575f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/SimpleRequest.kt @@ -1,8 +1,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts -import kotlinx.serialization.ContextSerializer -import kotlinx.serialization.KSerializer -import kotlinx.serialization.Serializable +import kotlinx.serialization.* @Serializable(SimpleRequestSerializer::class) interface SimpleRequest : Request diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt index d9b3f3213b..4685ccbac9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt @@ -1,9 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest -import kotlinx.serialization.ContextSerializer -import kotlinx.serialization.KSerializer -import kotlinx.serialization.Serializable +import kotlinx.serialization.* @Serializable(DataRequestSerializer::class) interface DataRequest : SimpleRequest diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/GetWebhookInfo.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/GetWebhookInfo.kt index 8b4decd01b..27b686964a 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/GetWebhookInfo.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/GetWebhookInfo.kt @@ -2,7 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.types.WebhookInfo -import kotlinx.serialization.* +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializable @Serializable class GetWebhookInfo : SimpleRequest { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt index 7649006554..79c6ca74fb 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt @@ -1,7 +1,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.* -import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.* +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.DataRequest +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.MultipartRequestImpl import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* import kotlinx.serialization.internal.BooleanSerializer diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt index 4e1f82900f..b571ad2c51 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/ChannelPostMediaGroupUpdate.kt @@ -2,8 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.ChannelPostUpdate -import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class ChannelPostMediaGroupUpdate( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt index 82d9bae6fc..2ff35f9463 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditChannelPostMediaGroupUpdate.kt @@ -2,7 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class EditChannelPostMediaGroupUpdate( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt index c5c9158e6f..0f8ca911f9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/EditMessageMediaGroupUpdate.kt @@ -2,8 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.EditMessageUpdate -import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class EditMessageMediaGroupUpdate( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt index 57fa89c54c..f1d253c869 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MessageMediaGroupUpdate.kt @@ -2,7 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate data class MessageMediaGroupUpdate( diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt index b2bcc701e5..e8c0583fac 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/JSON.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils -import kotlinx.serialization.* +import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.KSerializer import kotlinx.serialization.json.* @Deprecated("This method can throw exceptions") diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt index a15a6a931a..4fcbdf807e 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesFilter.kt @@ -3,9 +3,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions import com.github.insanusmokrassar.TelegramBotAPI.types.* 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 -import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate data class UpdatesFilter( private val messageCallback: UpdateReceiver? = null, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt index 49ae28603e..257b1db839 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt @@ -3,7 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor 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 import kotlinx.coroutines.* import java.util.concurrent.Executors diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 1e848cfa96..7d0af080a7 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -5,7 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook 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.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate @@ -13,7 +13,8 @@ import io.ktor.application.Application import io.ktor.application.call import io.ktor.request.receiveText import io.ktor.response.respond -import io.ktor.routing.* +import io.ktor.routing.post +import io.ktor.routing.routing import io.ktor.server.engine.* import io.ktor.server.netty.Netty import kotlinx.coroutines.* @@ -23,7 +24,6 @@ import java.util.concurrent.Executors import java.util.concurrent.TimeUnit - /** * Reverse proxy webhook. *