1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-21 15:53:47 +00:00

start support of prize_star_count

This commit is contained in:
InsanusMokrassar 2024-09-06 23:56:09 +06:00
parent 1980f8a9ca
commit 6809b9d2ef
6 changed files with 253 additions and 33 deletions

View File

@ -288,6 +288,7 @@ const val requestUsernameField = "request_username"
const val requestPhotoField = "request_photo"
const val requestNameField = "request_name"
const val maxQuantityField = "max_quantity"
const val prizeStarCountField = "prize_star_count"
const val userIsBotField = "user_is_bot"
const val userIsPremiumField = "user_is_premium"

View File

@ -57,13 +57,16 @@ sealed interface ChatBoostSource {
val unclaimed: Boolean
val claimed: Boolean
get() = !unclaimed
val prizeStarCount: Int?
@Serializable(ChatBoostSource.Companion::class)
data class Claimed(
@SerialName(giveawayMessageIdField)
override val messageId: MessageId,
@SerialName(userField)
override val user: PreviewUser
override val user: PreviewUser,
@SerialName(prizeStarCountField)
override val prizeStarCount: Int?
) : Giveaway, ByUser {
@Required
@EncodeDefault
@ -78,7 +81,9 @@ sealed interface ChatBoostSource {
@Serializable(ChatBoostSource.Companion::class)
data class Unclaimed(
@SerialName(giveawayMessageIdField)
override val messageId: MessageId
override val messageId: MessageId,
@SerialName(prizeStarCountField)
override val prizeStarCount: Int?
) : Giveaway {
@Required
@EncodeDefault
@ -115,7 +120,9 @@ sealed interface ChatBoostSource {
@SerialName(giveawayMessageIdField)
val messageId: MessageId? = null,
@SerialName(isUnclaimedField)
val unclaimed: Boolean? = null
val unclaimed: Boolean? = null,
@SerialName(prizeStarCountField)
val prizeStarCount: Int?
)
companion object : KSerializer<ChatBoostSource> {
@ -143,10 +150,12 @@ sealed interface ChatBoostSource {
when {
surrogate.user != null && surrogate.unclaimed == false -> Giveaway.Claimed(
surrogate.messageId,
surrogate.user
surrogate.user,
surrogate.prizeStarCount
)
surrogate.unclaimed == true -> Giveaway.Unclaimed(
surrogate.messageId
surrogate.messageId,
surrogate.prizeStarCount
)
else -> null
}
@ -166,6 +175,7 @@ sealed interface ChatBoostSource {
value.user,
(value as? Giveaway) ?.messageId,
(value as? Giveaway) ?.unclaimed,
(value as? Giveaway) ?.prizeStarCount
)
Surrogate.serializer().serialize(encoder, surrogate)

View File

@ -23,5 +23,7 @@ data class Giveaway(
@SerialName(countryCodesField)
val countries: List<IetfLang>? = null,
@SerialName(premiumSubscriptionMonthCountField)
override val premiumMonths: Int? = null
) : GiveawayInfo, ReplyInfo.External.ContentVariant
override val premiumMonths: Int? = null,
@SerialName(prizeStarCountField)
override val prizeStarCount: Int? = null
) : GiveawayInfo.OptionallyStars, GiveawayInfo.OptionallyPremium, ReplyInfo.External.ContentVariant

View File

@ -2,7 +2,54 @@ package dev.inmo.tgbotapi.types.giveaway
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ChatEvent
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.PublicChatEvent
import dev.inmo.tgbotapi.types.prizeStarCountField
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@Serializable
object GiveawayCreated : ChatEvent, PublicChatEvent
@Serializable(GiveawayCreated.Serializer::class)
sealed interface GiveawayCreated : ChatEvent, PublicChatEvent {
val prizeStarCount: Int?
@Serializable
data class Stars(
@SerialName(prizeStarCountField)
override val prizeStarCount: Int
) : GiveawayCreated
object Serializer : KSerializer<GiveawayCreated> {
@Serializable
private data class Surrogate(
val prizeStarCount: Int?
)
override val descriptor: SerialDescriptor
get() = Surrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): GiveawayCreated {
val surrogate = Surrogate.serializer().deserialize(decoder)
return when {
surrogate.prizeStarCount == null -> Common
else -> Stars(surrogate.prizeStarCount)
}
}
override fun serialize(encoder: Encoder, value: GiveawayCreated) {
Surrogate.serializer().serialize(
encoder,
Surrogate(
value.prizeStarCount
)
)
}
}
companion object : GiveawayCreated {
val Common = this
override val prizeStarCount: Int?
get() = null
}
}

View File

@ -10,6 +10,25 @@ import kotlinx.serialization.Serializable
sealed interface GiveawayInfo {
val selectionDate: TelegramDate
val onlyNewMembers: Boolean
val premiumMonths: Int?
val additionalPrizeDescription: String?
@Serializable
sealed interface OptionallyPremium : GiveawayInfo {
val premiumMonths: Int?
}
@Serializable
sealed interface Premium : OptionallyPremium {
override val premiumMonths: Int
}
@Serializable
sealed interface OptionallyStars : GiveawayInfo {
val prizeStarCount: Int?
}
@Serializable
sealed interface Stars : OptionallyStars {
override val prizeStarCount: Int
}
}

View File

@ -11,7 +11,7 @@ import kotlinx.serialization.encoding.Encoder
@Serializable(GiveawayPublicResults.Companion::class)
sealed interface GiveawayPublicResults: GiveawayInfo, GiveawayResults, WithPreviewChatAndMessageId,
ReplyInfo.External.ContentVariant {
ReplyInfo.External.ContentVariant, GiveawayInfo.OptionallyStars, GiveawayInfo.OptionallyPremium {
val count: Int
val winners: List<PreviewUser>
val additionalChats: Int
@ -46,11 +46,81 @@ sealed interface GiveawayPublicResults: GiveawayInfo, GiveawayResults, WithPrevi
@SerialName(prizeDescriptionField)
override val additionalPrizeDescription: String? = null
@SerialName(premiumSubscriptionMonthCountField)
override val premiumMonths: Int? = null
override val premiumMonths: Int?
get() = null
@SerialName(prizeStarCountField)
override val prizeStarCount: Int?
get() = null
}
sealed interface Winners : GiveawayPublicResults {
override val prizeStarCount: Int?
get() = null
override val premiumMonths: Int?
get() = null
@Serializable
data class Premium (
@SerialName(chatsField)
override val chat: PreviewChat,
@SerialName(giveawayMessageIdField)
override val messageId: MessageId,
@SerialName(winnersSelectionDateField)
override val selectionDate: TelegramDate,
@SerialName(winnersCountField)
override val count: Int,
@SerialName(winnersField)
override val winners: List<PreviewUser>,
@SerialName(premiumSubscriptionMonthCountField)
override val premiumMonths: Int,
@SerialName(additionalChatCountField)
override val additionalChats: Int = 0,
@SerialName(unclaimedPrizeCountField)
override val unclaimedCount: Int = 0,
@SerialName(onlyNewMembersField)
override val onlyNewMembers: Boolean = false,
@SerialName(hasPublicWinnersField)
override val publicWinners: Boolean = false,
@SerialName(prizeDescriptionField)
override val additionalPrizeDescription: String? = null,
) : Winners, GiveawayInfo.Premium {
@SerialName(wasRefundedField)
@Required
@EncodeDefault
override val refunded: Boolean = false
}
@Serializable
data class Stars (
@SerialName(chatsField)
override val chat: PreviewChat,
@SerialName(giveawayMessageIdField)
override val messageId: MessageId,
@SerialName(winnersSelectionDateField)
override val selectionDate: TelegramDate,
@SerialName(winnersCountField)
override val count: Int,
@SerialName(winnersField)
override val winners: List<PreviewUser>,
@SerialName(prizeStarCountField)
override val prizeStarCount: Int,
@SerialName(additionalChatCountField)
override val additionalChats: Int = 0,
@SerialName(unclaimedPrizeCountField)
override val unclaimedCount: Int = 0,
@SerialName(onlyNewMembersField)
override val onlyNewMembers: Boolean = false,
@SerialName(hasPublicWinnersField)
override val publicWinners: Boolean = false,
@SerialName(prizeDescriptionField)
override val additionalPrizeDescription: String? = null,
) : Winners, GiveawayInfo.Stars {
@SerialName(wasRefundedField)
@Required
@EncodeDefault
override val refunded: Boolean = false
}
}
@Serializable
data class Winners (
data class Unknown (
@SerialName(chatsField)
override val chat: PreviewChat,
@SerialName(giveawayMessageIdField)
@ -61,6 +131,10 @@ sealed interface GiveawayPublicResults: GiveawayInfo, GiveawayResults, WithPrevi
override val count: Int,
@SerialName(winnersField)
override val winners: List<PreviewUser>,
@SerialName(prizeStarCountField)
override val prizeStarCount: Int?,
@SerialName(premiumSubscriptionMonthCountField)
override val premiumMonths: Int?,
@SerialName(additionalChatCountField)
override val additionalChats: Int = 0,
@SerialName(unclaimedPrizeCountField)
@ -71,8 +145,6 @@ sealed interface GiveawayPublicResults: GiveawayInfo, GiveawayResults, WithPrevi
override val publicWinners: Boolean = false,
@SerialName(prizeDescriptionField)
override val additionalPrizeDescription: String? = null,
@SerialName(premiumSubscriptionMonthCountField)
override val premiumMonths: Int? = null
) : GiveawayPublicResults {
@SerialName(wasRefundedField)
@Required
@ -105,7 +177,9 @@ sealed interface GiveawayPublicResults: GiveawayInfo, GiveawayResults, WithPrevi
@SerialName(prizeDescriptionField)
val additionalPrizeDescription: String? = null,
@SerialName(premiumSubscriptionMonthCountField)
val premiumMonths: Int? = null
val premiumMonths: Int? = null,
@SerialName(prizeStarCountField)
val starsCount: Int? = null
)
companion object : KSerializer<GiveawayPublicResults> {
@ -115,27 +189,52 @@ sealed interface GiveawayPublicResults: GiveawayInfo, GiveawayResults, WithPrevi
override fun deserialize(decoder: Decoder): GiveawayPublicResults {
val surrogate = Surrogate.serializer().deserialize(decoder)
return when (surrogate.refunded) {
true -> Refunded(
return when {
surrogate.refunded -> Refunded(
chat = surrogate.chat,
messageId = surrogate.messageId,
selectionDate = surrogate.selectionDate
)
false -> {
Winners(
chat = surrogate.chat,
messageId = surrogate.messageId,
selectionDate = surrogate.selectionDate,
count = surrogate.count,
winners = surrogate.winners,
additionalChats = surrogate.additionalChats,
unclaimedCount = surrogate.unclaimedCount,
onlyNewMembers = surrogate.onlyNewMembers,
publicWinners = surrogate.publicWinners,
additionalPrizeDescription = surrogate.additionalPrizeDescription,
premiumMonths = surrogate.premiumMonths,
)
}
surrogate.premiumMonths != null -> Winners.Premium(
chat = surrogate.chat,
messageId = surrogate.messageId,
selectionDate = surrogate.selectionDate,
count = surrogate.count,
winners = surrogate.winners,
additionalChats = surrogate.additionalChats,
unclaimedCount = surrogate.unclaimedCount,
onlyNewMembers = surrogate.onlyNewMembers,
publicWinners = surrogate.publicWinners,
additionalPrizeDescription = surrogate.additionalPrizeDescription,
premiumMonths = surrogate.premiumMonths,
)
surrogate.starsCount != null -> Winners.Stars(
chat = surrogate.chat,
messageId = surrogate.messageId,
selectionDate = surrogate.selectionDate,
count = surrogate.count,
winners = surrogate.winners,
additionalChats = surrogate.additionalChats,
unclaimedCount = surrogate.unclaimedCount,
onlyNewMembers = surrogate.onlyNewMembers,
publicWinners = surrogate.publicWinners,
additionalPrizeDescription = surrogate.additionalPrizeDescription,
prizeStarCount = surrogate.starsCount,
)
else -> Unknown(
chat = surrogate.chat,
messageId = surrogate.messageId,
selectionDate = surrogate.selectionDate,
count = surrogate.count,
winners = surrogate.winners,
additionalChats = surrogate.additionalChats,
unclaimedCount = surrogate.unclaimedCount,
onlyNewMembers = surrogate.onlyNewMembers,
publicWinners = surrogate.publicWinners,
additionalPrizeDescription = surrogate.additionalPrizeDescription,
premiumMonths = surrogate.premiumMonths,
prizeStarCount = surrogate.starsCount,
)
}
}
@ -152,10 +251,52 @@ sealed interface GiveawayPublicResults: GiveawayInfo, GiveawayResults, WithPrevi
publicWinners = value.publicWinners,
additionalPrizeDescription = value.additionalPrizeDescription,
premiumMonths = value.premiumMonths,
starsCount = value.prizeStarCount,
refunded = value.refunded
)
Surrogate.serializer().serialize(encoder, surrogate)
}
fun Winners(
chat: PreviewChat,
messageId: MessageId,
selectionDate: TelegramDate,
count: Int,
winners: List<PreviewUser>,
additionalChats: Int = 0,
unclaimedCount: Int = 0,
onlyNewMembers: Boolean = false,
publicWinners: Boolean = false,
additionalPrizeDescription: String? = null,
premiumMonths: Int? = null
) = premiumMonths ?.let {
Winners.Premium(
chat = chat,
messageId = messageId,
selectionDate = selectionDate,
count = count,
winners = winners,
premiumMonths = premiumMonths,
additionalChats = additionalChats,
unclaimedCount = unclaimedCount,
onlyNewMembers = onlyNewMembers,
publicWinners = publicWinners,
additionalPrizeDescription = additionalPrizeDescription
)
} ?: Unknown(
chat = chat,
messageId = messageId,
selectionDate = selectionDate,
count = count,
winners = winners,
prizeStarCount = null,
premiumMonths = premiumMonths,
additionalChats = additionalChats,
unclaimedCount = unclaimedCount,
onlyNewMembers = onlyNewMembers,
publicWinners = publicWinners,
additionalPrizeDescription = additionalPrizeDescription
)
}
}