From a8ed162c9c25613bf6b9a6e6f4c5555ad6725a05 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 15 Jul 2024 08:55:08 +0600 Subject: [PATCH] small fixes/refactors in SendLocation --- .../tgbotapi/requests/send/SendLocation.kt | 106 +++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/SendLocation.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/SendLocation.kt index 3edb9f96b1..3f722ec7fd 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/SendLocation.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/SendLocation.kt @@ -11,6 +11,9 @@ import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializ import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.utils.throwRangeError import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder private val commonResultDeserializer: DeserializationStrategy> @@ -102,7 +105,7 @@ fun SendLiveLocation( replyMarkup = replyMarkup ) -@Serializable +@Serializable(SendLocation.Companion::class) sealed interface SendLocation : SendContentMessageRequest>, ReplyingMarkupSendMessageRequest>, PositionedSendMessageRequest>, @@ -121,6 +124,7 @@ sealed interface SendLocation : SendContentMessageRequest : SendContentMessageRequest : SendContentMessageRequest> get() = liveResultDeserializer override val requestSerializer: SerializationStrategy<*> - get() = Live.serializer() + get() = serializer() init { if (livePeriod !in livePeriodLimit) { @@ -170,9 +176,11 @@ sealed interface SendLocation : SendContentMessageRequest : SendContentMessageRequest get() = serializer() } + + companion object : KSerializer> { + @Serializable + private class Surrogate( + @SerialName(chatIdField) + val chatId: ChatIdentifier, + @SerialName(latitudeField) + val latitude: Double, + @SerialName(longitudeField) + val longitude: Double, + @SerialName(livePeriodField) + val livePeriod: Seconds? = null, + @SerialName(horizontalAccuracyField) + val horizontalAccuracy: Meters? = null, + @SerialName(headingField) + val heading: Degrees? = null, + @SerialName(proximityAlertRadiusField) + val proximityAlertRadius: Meters? = null, + @SerialName(messageThreadIdField) + val threadId: MessageThreadId? = chatId.threadId, + @SerialName(businessConnectionIdField) + val businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, + @SerialName(disableNotificationField) + val disableNotification: Boolean = false, + @SerialName(protectContentField) + val protectContent: Boolean = false, + @SerialName(messageEffectIdField) + val effectId: EffectId? = null, + @SerialName(replyParametersField) + val replyParameters: ReplyParameters? = null, + @SerialName(replyMarkupField) + val replyMarkup: KeyboardMarkup? = null + ) + + override val descriptor: SerialDescriptor + get() = Surrogate.serializer().descriptor + + override fun deserialize(decoder: Decoder): SendLocation<*> { + val surrogate = Surrogate.serializer().deserialize(decoder) + + return when (surrogate.livePeriod) { + null -> Static( + chatId = surrogate.chatId, + latitude = surrogate.latitude, + longitude = surrogate.longitude, + threadId = surrogate.threadId, + businessConnectionId = surrogate.businessConnectionId, + disableNotification = surrogate.disableNotification, + protectContent = surrogate.protectContent, + effectId = surrogate.effectId, + replyParameters = surrogate.replyParameters, + replyMarkup = surrogate.replyMarkup + ) + else -> Live( + chatId = surrogate.chatId, + latitude = surrogate.latitude, + longitude = surrogate.longitude, + livePeriod = surrogate.livePeriod, + horizontalAccuracy = surrogate.horizontalAccuracy, + heading = surrogate.heading, + proximityAlertRadius = surrogate.proximityAlertRadius, + threadId = surrogate.threadId, + businessConnectionId = surrogate.businessConnectionId, + disableNotification = surrogate.disableNotification, + protectContent = surrogate.protectContent, + effectId = surrogate.effectId, + replyParameters = surrogate.replyParameters, + replyMarkup = surrogate.replyMarkup + ) + } + } + + override fun serialize(encoder: Encoder, value: SendLocation<*>) { + val surrogate = with(value) { + Surrogate( + chatId = chatId, + latitude = latitude, + longitude = longitude, + livePeriod = livePeriod, + horizontalAccuracy = horizontalAccuracy, + heading = heading, + proximityAlertRadius = proximityAlertRadius, + threadId = threadId, + businessConnectionId = businessConnectionId, + disableNotification = disableNotification, + protectContent = protectContent, + effectId = effectId, + replyParameters = replyParameters, + replyMarkup = replyMarkup + ) + } + Surrogate.serializer().serialize(encoder, surrogate) + } + } }