From 57175b769863b905f5476676b4b7c195f321c270 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 6 Dec 2024 12:56:00 +0600 Subject: [PATCH] add affiliate in TransactionPartner.User --- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 5 +- .../tgbotapi/types/payments/AffiliateInfo.kt | 24 +++++ .../types/payments/stars/StarTransaction.kt | 12 +-- .../payments/stars/TransactionPartner.kt | 101 +++++++++++------- 4 files changed, 99 insertions(+), 43 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/AffiliateInfo.kt 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 d58ed84330..61a402d508 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 @@ -521,7 +521,10 @@ const val totalAmountField = "total_amount" const val invoicePayloadField = "invoice_payload" const val requestCountField = "request_count" const val sponsorUserField = "sponsor_user" -const val commisionPerMilleField = "commission_per_mille" +const val affiliateUserField = "affiliate_user" +const val affiliateChatField = "affiliate_chat" +const val commissionPerMilleField = "commission_per_mille" +const val affiliateField = "affiliate" const val paidMediaPayloadField = "paid_media_payload" const val shippingOptionIdField = "shipping_option_id" const val shippingQueryIdField = "shipping_query_id" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/AffiliateInfo.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/AffiliateInfo.kt new file mode 100644 index 0000000000..fda6153240 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/AffiliateInfo.kt @@ -0,0 +1,24 @@ +package dev.inmo.tgbotapi.types.payments + +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.chat.PreviewBot +import dev.inmo.tgbotapi.types.chat.PreviewChat +import dev.inmo.tgbotapi.types.chat.PreviewUser +import dev.inmo.tgbotapi.types.chat.User +import dev.inmo.tgbotapi.types.payments.abstracts.Amounted +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class AffiliateInfo( + @SerialName(commissionPerMilleField) + val commissionPerMille: Int, + @SerialName(amountField) + override val amount: Long, + @SerialName(nanostarAmountField) + val nanostarAmount: Long, + @SerialName(affiliateUserField) + val affiliateUser: User? = null, + @SerialName(affiliateChatField) + val affiliateChat: PreviewChat? = null, +) : Amounted diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/StarTransaction.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/StarTransaction.kt index 28500e70ba..9dc7173a39 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/StarTransaction.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/StarTransaction.kt @@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.types.payments.stars import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.PreviewUser +import dev.inmo.tgbotapi.types.payments.abstracts.Amounted import dev.inmo.tgbotapi.utils.decodeDataAndJson import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded import kotlinx.serialization.KSerializer @@ -16,9 +17,8 @@ import kotlinx.serialization.json.JsonElement @Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @Serializable(StarTransaction.Companion::class) @ClassCastsIncluded -sealed interface StarTransaction { +sealed interface StarTransaction : Amounted { val id: StarTransactionId - val amount: Int val nanostarAmount: Int val date: TelegramDate val partner: TransactionPartner @@ -30,7 +30,7 @@ sealed interface StarTransaction { @SerialName(idField) override val id: StarTransactionId, @SerialName(amountField) - override val amount: Int, + override val amount: Long, @SerialName(nanostarAmountField) override val nanostarAmount: Int, @SerialName(dateField) @@ -50,7 +50,7 @@ sealed interface StarTransaction { @SerialName(idField) override val id: StarTransactionId, @SerialName(amountField) - override val amount: Int, + override val amount: Long, @SerialName(nanostarAmountField) override val nanostarAmount: Int, @SerialName(dateField) @@ -69,7 +69,7 @@ sealed interface StarTransaction { data class Unknown( @SerialName(idField) override val id: StarTransactionId, - override val amount: Int, + override val amount: Long, @SerialName(nanostarAmountField) override val nanostarAmount: Int, override val date: TelegramDate, @@ -85,7 +85,7 @@ sealed interface StarTransaction { @Serializable private data class Surrogate( val id: StarTransactionId, - val amount: Int, + val amount: Long, @SerialName(nanostarAmountField) val nanostarAmount: Int, val date: TelegramDate, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/TransactionPartner.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/TransactionPartner.kt index 969aa8b32e..feaf9bd10a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/TransactionPartner.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/payments/stars/TransactionPartner.kt @@ -8,6 +8,7 @@ import dev.inmo.tgbotapi.types.chat.PreviewBot import dev.inmo.tgbotapi.types.chat.PreviewUser import dev.inmo.tgbotapi.types.gifts.Gift import dev.inmo.tgbotapi.types.message.payments.PaidMedia +import dev.inmo.tgbotapi.types.payments.AffiliateInfo import dev.inmo.tgbotapi.utils.TimeSpanAsSecondsSerializer import dev.inmo.tgbotapi.utils.decodeDataAndJson import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded @@ -45,6 +46,8 @@ sealed interface TransactionPartner { data class User( @SerialName(userField) val user: PreviewUser, + @SerialName(affiliateField) + val affiliate: AffiliateInfo? = null, @SerialName(invoicePayloadField) val invoicePayload: InvoicePayload? = null, @SerialName(subscriptionPeriodField) @@ -88,8 +91,8 @@ sealed interface TransactionPartner { data class AffiliateProgram( @SerialName(sponsorUserField) val sponsorUser: PreviewBot?, - @SerialName(commisionPerMilleField) - val commisionPerMille: Int, + @SerialName(commissionPerMilleField) + val commissionPerMille: Int, ) : TransactionPartner { @EncodeDefault override val type: String = Companion.type @@ -123,10 +126,17 @@ sealed interface TransactionPartner { val type: String, val withdrawal_state: RevenueWithdrawalState? = null, val user: PreviewUser? = null, + val affiliate: AffiliateInfo? = null, val invoice_payload: InvoicePayload? = null, + @Serializable(TimeSpanAsSecondsSerializer::class) + val subscription_period: TimeSpan? = null, + val paid_media: List? = null, + val paid_media_payload: PaidMediaPayload? = null, + val gift: Gift? = null, val request_count: Int? = null, val sponsor_user: PreviewBot? = null, val commission_per_mille: Int? = null, + val invoicePayload: InvoicePayload? = null ) override val descriptor: SerialDescriptor @@ -138,44 +148,63 @@ sealed interface TransactionPartner { val unknown by lazy { Unknown(data.type, json) } - return when (data.type) { - Other.type -> Other - User.type -> User( - data.user ?: return unknown, - ) - TelegramAPI.type -> TelegramAPI( - data.request_count ?: return unknown, - ) - Ads.type -> Ads - Fragment.type -> Fragment( - data.withdrawal_state ?: return unknown, - ) - AffiliateProgram.type -> AffiliateProgram( - data.sponsor_user, - data.commission_per_mille ?: return unknown, - ) - else -> unknown + return with(data) { + when (data.type) { + Other.type -> Other + User.type -> User( + user = user ?: return unknown, + affiliate = affiliate, + invoicePayload = invoice_payload, + subscriptionPeriod = subscription_period, + paidMedia = paid_media, + paidMediaPayload = paid_media_payload, + gift = gift + ) + TelegramAPI.type -> TelegramAPI( + data.request_count ?: return unknown, + ) + Ads.type -> Ads + Fragment.type -> Fragment( + data.withdrawal_state ?: return unknown, + ) + AffiliateProgram.type -> AffiliateProgram( + data.sponsor_user, + data.commission_per_mille ?: return unknown, + ) + else -> unknown + } } } override fun serialize(encoder: Encoder, value: TransactionPartner) { - val surrogate = when (value) { - Other -> Surrogate(type = value.type) - Ads -> Surrogate(type = value.type) - is User -> Surrogate(type = value.type, user = value.user) - is TelegramAPI -> Surrogate(type = value.type, request_count = value.requestCount) - is Fragment -> Surrogate( - type = value.type, - withdrawal_state = value.withdrawalState - ) - is AffiliateProgram -> Surrogate( - type = value.type, - sponsor_user = value.sponsorUser, - commission_per_mille = value.commisionPerMille - ) - is Unknown -> value.raw ?.let { - return JsonElement.serializer().serialize(encoder, it) - } ?: Surrogate(type = value.type) + val surrogate = with (value) { + when (this) { + Other -> Surrogate(type = value.type) + Ads -> Surrogate(type = value.type) + is User -> Surrogate( + type = value.type, + user = user, + affiliate = affiliate, + invoice_payload = invoicePayload, + subscription_period = subscriptionPeriod, + paid_media = paidMedia, + paid_media_payload = paidMediaPayload, + gift = gift + ) + is TelegramAPI -> Surrogate(type = value.type, request_count = requestCount) + is Fragment -> Surrogate( + type = value.type, + withdrawal_state = withdrawalState + ) + is AffiliateProgram -> Surrogate( + type = value.type, + sponsor_user = sponsorUser, + commission_per_mille = commissionPerMille + ) + is Unknown -> raw ?.let { + return JsonElement.serializer().serialize(encoder, it) + } ?: Surrogate(type = value.type) + } } Surrogate.serializer().serialize(encoder, surrogate)