From 085a225eb4e0e8b65cd65f28ffe2975c5f20aeeb Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 18 Aug 2023 23:27:15 +0600 Subject: [PATCH] add support of voter_chat in PollAnswer --- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 1 + .../inmo/tgbotapi/types/polls/PollAnswer.kt | 90 +++++++++++++++++-- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 95023b91a9..29100932c6 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -289,6 +289,7 @@ const val pinnedMessageField = "pinned_message" const val activeUsernamesField = "active_usernames" const val customTitleField = "custom_title" const val optionIdsField = "option_ids" +const val voterChatField = "voter_chat" const val ipAddressField = "ip_address" const val linkedChatIdField = "linked_chat_id" const val hasHiddenMembersField = "has_hidden_members" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt index 6b46f43124..d98231241b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/PollAnswer.kt @@ -2,19 +2,95 @@ package dev.inmo.tgbotapi.types.polls import dev.inmo.tgbotapi.abstracts.FromUser import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.chat.ChannelChat +import dev.inmo.tgbotapi.types.chat.CommonBot import dev.inmo.tgbotapi.types.chat.User import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder @Serializable -data class PollAnswer( - @SerialName(pollIdField) - val pollId: PollIdentifier, - @SerialName(userField) - override val user: User, - @SerialName(optionIdsField) +sealed interface PollAnswer: FromUser { + val pollId: PollIdentifier + override val user: User val chosen: List -) : FromUser { @Transient override val from: User get() = user + + @Serializable + data class Common( + @SerialName(pollIdField) + override val pollId: PollIdentifier, + @SerialName(userField) + override val user: User, + @SerialName(optionIdsField) + override val chosen: List, + ) : PollAnswer + + @Serializable + data class InChannel( + @SerialName(pollIdField) + override val pollId: PollIdentifier, + @SerialName(voterChatField) + val voterChat: ChannelChat, + @SerialName(optionIdsField) + override val chosen: List + ) : PollAnswer { + @SerialName(userField) + override val user: User = defaultUser + + companion object { + val defaultUser = CommonBot( + UserId(136817688L), + "", + "", + Username("@Channel_Bot") + ) + } + } + + companion object : KSerializer { + @Serializable + private data class PollAnswerSurrogate( + @SerialName(pollIdField) + val pollId: PollIdentifier, + @SerialName(userField) + val user: User, + @SerialName(optionIdsField) + val chosen: List, + @SerialName(voterChatField) + val voterChat: ChannelChat? + ) + operator fun invoke( + pollId: PollIdentifier, + user: User, + chosen: List, + ) = Common(pollId, user, chosen) + + override val descriptor: SerialDescriptor + get() = PollAnswerSurrogate.serializer().descriptor + + override fun deserialize(decoder: Decoder): PollAnswer { + val surrogate = PollAnswerSurrogate.serializer().deserialize(decoder) + return if (surrogate.voterChat != null) { + InChannel(surrogate.pollId, surrogate.voterChat, surrogate.chosen) + } else { + Common(surrogate.pollId, surrogate.user, surrogate.chosen) + } + } + + override fun serialize(encoder: Encoder, value: PollAnswer) { + PollAnswerSurrogate.serializer().serialize( + encoder, + PollAnswerSurrogate( + value.pollId, + value.user, + value.chosen, + (value as? InChannel) ?.voterChat + ) + ) + } + } }