From 43ac09a79b33f831c50aef44f1b0570292b341c8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 31 Mar 2020 11:28:48 +0600 Subject: [PATCH] fixes in startGettingUpdates --- CHANGELOG.md | 6 ++++++ .../extensions/api/GetUpdates.kt | 4 ++-- .../extensions/api/updates/UpdatesPolling.kt | 18 +++++++++++++++--- .../TelegramBotAPI/requests/GetUpdates.kt | 8 +++++++- .../TelegramBotAPI/types/Common.kt | 1 + 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80049c9e1f..41011eb580 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ * `AddAnimatedStickerToSet` request was added * `SetStickerSetThumb` request was added * Most of sticker actions now implements `StandardStickerSetAction` instead of `StickerSetAction` + * `getUpdatesLimit` was added to be ensure in get updates limit + * `GetUpdates` now will check count of requesting updates and throw exception if it is not in range `1 .. 100` + * `GetUpdates#limit` now is not nullable and by default set up to 100 * `TelegramBotAPI-extensions-api`: * Extensions `sendDice` was added * Extension `getMyCommands` request was added @@ -28,6 +31,9 @@ * **All extensions `addStickerToSet` was renamed to `addStaticStickerToSet`** * Extensions `addAnimatedStickerToSet` was added * Extensions `setStickerSetThumb` was added + * Extension `startGettingUpdates` now will drop `SentMediaGroupUpdate` in case if it is the last in updates group + and size of retrieved updates is equal to 100 (max count of retrieved updates) + * Extensions `getUpdates` now will receive only not nullable `limit` parameter ## 0.25.0 diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt index 728dfb0ec5..778ef5e983 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt @@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update suspend fun RequestsExecutor.getUpdates( offset: UpdateIdentifier? = null, - limit: Int? = null, + limit: Int = getUpdatesLimit.last, timeout: Seconds? = null, allowed_updates: List? = ALL_UPDATES_LIST ) = execute( @@ -18,7 +18,7 @@ suspend fun RequestsExecutor.getUpdates( suspend fun RequestsExecutor.getUpdates( lastUpdate: Update, - limit: Int? = null, + limit: Int = getUpdatesLimit.last, timeout: Seconds? = null, allowed_updates: List? = ALL_UPDATES_LIST ) = getUpdates( diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt index bd4e99f8ab..fe2fba5f2e 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -5,8 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestExceptio import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.lastUpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.getUpdates -import com.github.insanusmokrassar.TelegramBotAPI.types.Seconds -import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier +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.Update @@ -30,7 +29,20 @@ fun RequestsExecutor.startGettingOfUpdates( offset = lastUpdateIdentifier?.plus(1), timeout = timeoutSeconds, allowed_updates = allowedUpdates - ).convertWithMediaGroupUpdates() + ).let { originalUpdates -> + val converted = originalUpdates.convertWithMediaGroupUpdates() + /** + * Dirty hack for cases when the media group was retrieved not fully: + * + * We are throw out the last media group and will reretrieve it again in the next get updates + * and it will guarantee that it is full + */ + if (originalUpdates.size == getUpdatesLimit.last && converted.last() is SentMediaGroupUpdate) { + converted - converted.last() + } else { + converted + } + } supervisorScope { for (update in updates) { diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt index 5f89bcc368..3169c63936 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt @@ -14,7 +14,7 @@ private val updatesListSerializer = ListSerializer( @Serializable data class GetUpdates( val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates - val limit: Int? = null, + val limit: Int = getUpdatesLimit.last, val timeout: Seconds? = null, val allowed_updates: List? = ALL_UPDATES_LIST ): SimpleRequest> { @@ -25,4 +25,10 @@ data class GetUpdates( override val requestSerializer: SerializationStrategy<*> get() = serializer() + + init { + if (limit !in getUpdatesLimit) { + error("GetUpdates request can be called only with limit in range $getUpdatesLimit (actual value is $limit)") + } + } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 1b5ba30e63..97b58db5d1 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -23,6 +23,7 @@ typealias DiceResult = Int typealias Seconds = Int +val getUpdatesLimit = 1 .. 100 val callbackQueryAnswerLength = 0 until 200 val captionLength = 0 until 1024 val textLength = 0 until 4096