1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

add support of voter_chat in PollAnswer

This commit is contained in:
InsanusMokrassar 2023-08-18 23:27:15 +06:00
parent b575871a8e
commit 085a225eb4
2 changed files with 84 additions and 7 deletions

View File

@ -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"

View File

@ -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<Int>
) : 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<Int>,
) : PollAnswer
@Serializable
data class InChannel(
@SerialName(pollIdField)
override val pollId: PollIdentifier,
@SerialName(voterChatField)
val voterChat: ChannelChat,
@SerialName(optionIdsField)
override val chosen: List<Int>
) : PollAnswer {
@SerialName(userField)
override val user: User = defaultUser
companion object {
val defaultUser = CommonBot(
UserId(136817688L),
"",
"",
Username("@Channel_Bot")
)
}
}
companion object : KSerializer<PollAnswer> {
@Serializable
private data class PollAnswerSurrogate(
@SerialName(pollIdField)
val pollId: PollIdentifier,
@SerialName(userField)
val user: User,
@SerialName(optionIdsField)
val chosen: List<Int>,
@SerialName(voterChatField)
val voterChat: ChannelChat?
)
operator fun invoke(
pollId: PollIdentifier,
user: User,
chosen: List<Int>,
) = 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
)
)
}
}
}