From b60fab48716add1d0dab50152de54070d5852e97 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 26 Jun 2021 01:27:28 +0600 Subject: [PATCH] BotCommandScope --- CHANGELOG.md | 3 + .../types/commands/BotCommandScope.kt | 120 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 37789223d9..67f14cd528 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ `performInParallel`, by default `true`) * `API`: * All `reply` and subsequent extension have been replaced in send package + +* `Bot API 5.3`: + * Add type `BotCommandScope`, its serializer `BotCommandScopeSerializer` and all its children ## 0.35.0 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt new file mode 100644 index 0000000000..dcb606e335 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt @@ -0,0 +1,120 @@ +package dev.inmo.tgbotapi.types.commands + +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.utils.RiskFeature +import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +@Serializable +private class SurrogateBotCommandScope( + @SerialName(typeField) + @Required + val type: String, + @SerialName(chatIdField) + val chatId: ChatIdentifier? = null, + @SerialName(userIdField) + val userId: UserId? = null +) { + fun asBotCommandScope() = when (type) { + "default" -> BotCommandScopeDefault + "all_private_chats" -> BotCommandScopeAllPrivateChats + "all_group_chats" -> BotCommandScopeAllGroupChats + "all_chat_administrators" -> BotCommandScopeAllChatAdministrators + "chat_administrators" -> BotCommandScopeChatAdministrators( + chatId ?: error("chat_administrators type must have $chatIdField field, but have no") + ) + "chat_member" -> BotCommandScopeChatMember( + chatId ?: error("chat_administrators type must have $chatIdField field, but have no"), + userId ?: error("chat_administrators type must have $userIdField field, but have no") + ) + else -> UnknownBotCommandScope(type) + } + + companion object { + fun from(scope: BotCommandScope) = when (scope) { + is UnknownBotCommandScope, + BotCommandScopeDefault, + BotCommandScopeAllPrivateChats, + BotCommandScopeAllGroupChats, + BotCommandScopeAllChatAdministrators -> SurrogateBotCommandScope(scope.type) + is BotCommandScopeChatAdministrators -> SurrogateBotCommandScope(scope.type, scope.chatId) + is BotCommandScopeChatMember -> SurrogateBotCommandScope(scope.type, scope.chatId, scope.userId) + } + } +} + +@Serializable(BotCommandScopeSerializer::class) +sealed interface BotCommandScope { + val type: String +} + +@Serializable +data class UnknownBotCommandScope internal constructor( + override val type: String +) : BotCommandScope + +@Serializable +object BotCommandScopeDefault : BotCommandScope { + @Required + override val type: String = "default" +} + +@Serializable +object BotCommandScopeAllPrivateChats : BotCommandScope { + @Required + override val type: String = "all_private_chats" +} + +@Serializable +object BotCommandScopeAllGroupChats : BotCommandScope { + @Required + override val type: String = "all_group_chats" +} + +@Serializable +object BotCommandScopeAllChatAdministrators : BotCommandScope { + @Required + override val type: String = "all_chat_administrators" +} + + +sealed interface ChatBotCommandScope : BotCommandScope { + val chatId: ChatIdentifier +} + +@Serializable +data class BotCommandScopeChatAdministrators( + override val chatId: ChatIdentifier +) : ChatBotCommandScope { + @Required + override val type: String = "chat_administrators" +} + +@Serializable +data class BotCommandScopeChatMember( + override val chatId: ChatIdentifier, + val userId: UserId +) : ChatBotCommandScope { + @Required + override val type: String = "chat_member" +} + + +object BotCommandScopeSerializer : KSerializer { + + @RiskFeature + override val descriptor: SerialDescriptor = SurrogateBotCommandScope.serializer().descriptor + + override fun deserialize( + decoder: Decoder + ): BotCommandScope = SurrogateBotCommandScope.serializer().deserialize(decoder).asBotCommandScope() + + override fun serialize(encoder: Encoder, value: BotCommandScope) { + SurrogateBotCommandScope.serializer().serialize( + encoder, SurrogateBotCommandScope.from(value) + ) + } + +}