1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/commands/BotCommandScope.kt
2021-06-26 01:27:28 +06:00

121 lines
3.6 KiB
Kotlin

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<BotCommandScope> {
@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)
)
}
}