1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-02 07:55:25 +00:00
tgbotapi/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt

121 lines
4.3 KiB
Kotlin
Raw Normal View History

2019-04-16 07:51:53 +00:00
package com.github.insanusmokrassar.TelegramBotAPI.types.polls
import com.github.insanusmokrassar.TelegramBotAPI.types.*
2020-03-22 07:37:01 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat
2020-01-23 10:36:25 +00:00
import kotlinx.serialization.*
2020-03-22 07:37:01 +00:00
import kotlinx.serialization.builtins.ListSerializer
2020-01-23 10:36:25 +00:00
import kotlinx.serialization.json.*
@Serializable(PollSerializer::class)
sealed class Poll {
abstract val id: PollIdentifier
abstract val question: String
abstract val options: List<PollOption>
2020-01-23 10:40:57 +00:00
abstract val votesCount: Int
abstract val isClosed: Boolean
2020-01-23 10:36:25 +00:00
abstract val isAnonymous: Boolean
}
@Serializable
data class UnknownPollType internal constructor(
2020-01-23 10:36:25 +00:00
@SerialName(idField)
override val id: PollIdentifier,
@SerialName(questionField)
override val question: String,
@SerialName(optionsField)
override val options: List<PollOption>,
2020-01-23 10:40:57 +00:00
@SerialName(totalVoterCountField)
override val votesCount: Int,
2020-01-23 10:36:25 +00:00
@SerialName(isClosedField)
override val isClosed: Boolean = false,
2020-01-23 10:36:25 +00:00
@SerialName(isAnonymousField)
override val isAnonymous: Boolean = false,
val raw: String
) : Poll()
@Serializable
data class RegularPoll(
@SerialName(idField)
override val id: PollIdentifier,
@SerialName(questionField)
override val question: String,
@SerialName(optionsField)
override val options: List<PollOption>,
2020-01-23 10:40:57 +00:00
@SerialName(totalVoterCountField)
override val votesCount: Int,
2020-01-23 10:36:25 +00:00
@SerialName(isClosedField)
override val isClosed: Boolean = false,
2020-01-23 10:36:25 +00:00
@SerialName(isAnonymousField)
override val isAnonymous: Boolean = false,
@SerialName(allowsMultipleAnswersField)
val allowMultipleAnswers: Boolean = false
) : Poll()
2019-04-16 07:51:53 +00:00
@Serializable
2020-01-23 10:36:25 +00:00
data class QuizPoll(
2019-04-16 07:51:53 +00:00
@SerialName(idField)
2020-01-23 10:36:25 +00:00
override val id: PollIdentifier,
2019-04-16 07:51:53 +00:00
@SerialName(questionField)
2020-01-23 10:36:25 +00:00
override val question: String,
2019-04-16 07:51:53 +00:00
@SerialName(optionsField)
2020-01-23 10:36:25 +00:00
override val options: List<PollOption>,
2020-01-23 10:40:57 +00:00
@SerialName(totalVoterCountField)
override val votesCount: Int,
/**
* Nullable due to documentation (https://core.telegram.org/bots/api#poll)
*/
@SerialName(correctOptionIdField)
val correctOptionId: Int? = null,
2019-04-16 07:51:53 +00:00
@SerialName(isClosedField)
override val isClosed: Boolean = false,
2020-01-23 10:36:25 +00:00
@SerialName(isAnonymousField)
override val isAnonymous: Boolean = false
2020-01-23 10:36:25 +00:00
) : Poll()
@Serializer(Poll::class)
internal object PollSerializer : KSerializer<Poll> {
2020-03-22 07:37:01 +00:00
private val pollOptionsSerializer = ListSerializer(PollOption.serializer())
2020-01-23 10:36:25 +00:00
override fun deserialize(decoder: Decoder): Poll {
val asJson = JsonObjectSerializer.deserialize(decoder)
return when (asJson.getPrimitive(typeField).content) {
2020-03-22 07:37:01 +00:00
regularPollType -> nonstrictJsonFormat.fromJson(
2020-01-23 10:36:25 +00:00
RegularPoll.serializer(),
asJson
)
2020-03-22 07:37:01 +00:00
quizPollType -> nonstrictJsonFormat.fromJson(
2020-01-23 10:36:25 +00:00
QuizPoll.serializer(),
asJson
)
else -> UnknownPollType(
asJson.getPrimitive(idField).content,
asJson.getPrimitive(questionField).content,
2020-03-22 07:37:01 +00:00
nonstrictJsonFormat.fromJson(
2020-01-23 10:36:25 +00:00
pollOptionsSerializer,
asJson.getArray(optionsField)
),
2020-01-23 10:40:57 +00:00
asJson.getPrimitive(totalVoterCountField).int,
2020-01-23 10:36:25 +00:00
asJson.getPrimitiveOrNull(isClosedField) ?.booleanOrNull ?: false,
asJson.getPrimitiveOrNull(isAnonymousField) ?.booleanOrNull ?: true,
asJson.toString()
)
}
}
2020-03-22 07:37:01 +00:00
override fun serialize(encoder: Encoder, value: Poll) {
val asJson = when (value) {
is RegularPoll -> nonstrictJsonFormat.toJson(RegularPoll.serializer(), value)
is QuizPoll -> nonstrictJsonFormat.toJson(QuizPoll.serializer(), value)
is UnknownPollType -> throw IllegalArgumentException("Currently unable to correctly serialize object of poll $value")
2020-01-23 10:36:25 +00:00
}
val resultJson = JsonObject(
2020-03-22 07:37:01 +00:00
asJson.jsonObject + (typeField to when (value) {
2020-01-23 10:36:25 +00:00
is RegularPoll -> JsonPrimitive(regularPollType)
is QuizPoll -> JsonPrimitive(quizPollType)
2020-03-22 07:37:01 +00:00
is UnknownPollType -> throw IllegalArgumentException("Currently unable to correctly serialize object of poll $value")
2020-01-23 10:36:25 +00:00
})
)
JsonObjectSerializer.serialize(encoder, resultJson)
}
}