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:
parent
b575871a8e
commit
085a225eb4
@ -289,6 +289,7 @@ const val pinnedMessageField = "pinned_message"
|
|||||||
const val activeUsernamesField = "active_usernames"
|
const val activeUsernamesField = "active_usernames"
|
||||||
const val customTitleField = "custom_title"
|
const val customTitleField = "custom_title"
|
||||||
const val optionIdsField = "option_ids"
|
const val optionIdsField = "option_ids"
|
||||||
|
const val voterChatField = "voter_chat"
|
||||||
const val ipAddressField = "ip_address"
|
const val ipAddressField = "ip_address"
|
||||||
const val linkedChatIdField = "linked_chat_id"
|
const val linkedChatIdField = "linked_chat_id"
|
||||||
const val hasHiddenMembersField = "has_hidden_members"
|
const val hasHiddenMembersField = "has_hidden_members"
|
||||||
|
@ -2,19 +2,95 @@ package dev.inmo.tgbotapi.types.polls
|
|||||||
|
|
||||||
import dev.inmo.tgbotapi.abstracts.FromUser
|
import dev.inmo.tgbotapi.abstracts.FromUser
|
||||||
import dev.inmo.tgbotapi.types.*
|
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 dev.inmo.tgbotapi.types.chat.User
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||||
|
import kotlinx.serialization.encoding.Decoder
|
||||||
|
import kotlinx.serialization.encoding.Encoder
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class PollAnswer(
|
sealed interface PollAnswer: FromUser {
|
||||||
@SerialName(pollIdField)
|
val pollId: PollIdentifier
|
||||||
val pollId: PollIdentifier,
|
override val user: User
|
||||||
@SerialName(userField)
|
|
||||||
override val user: User,
|
|
||||||
@SerialName(optionIdsField)
|
|
||||||
val chosen: List<Int>
|
val chosen: List<Int>
|
||||||
) : FromUser {
|
|
||||||
@Transient
|
@Transient
|
||||||
override val from: User
|
override val from: User
|
||||||
get() = 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
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user