1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

small fixes/refactors in SendLocation

This commit is contained in:
InsanusMokrassar 2024-07-15 08:55:08 +06:00
parent e5ea980347
commit a8ed162c9c

View File

@ -11,6 +11,9 @@ import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializ
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.utils.throwRangeError import dev.inmo.tgbotapi.utils.throwRangeError
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
private val commonResultDeserializer: DeserializationStrategy<ContentMessage<LocationContent>> private val commonResultDeserializer: DeserializationStrategy<ContentMessage<LocationContent>>
@ -102,7 +105,7 @@ fun SendLiveLocation(
replyMarkup = replyMarkup replyMarkup = replyMarkup
) )
@Serializable @Serializable(SendLocation.Companion::class)
sealed interface SendLocation<T : LocationContent> : SendContentMessageRequest<ContentMessage<T>>, sealed interface SendLocation<T : LocationContent> : SendContentMessageRequest<ContentMessage<T>>,
ReplyingMarkupSendMessageRequest<ContentMessage<T>>, ReplyingMarkupSendMessageRequest<ContentMessage<T>>,
PositionedSendMessageRequest<ContentMessage<T>>, PositionedSendMessageRequest<ContentMessage<T>>,
@ -121,6 +124,7 @@ sealed interface SendLocation<T : LocationContent> : SendContentMessageRequest<C
@SerialName(longitudeField) @SerialName(longitudeField)
override val longitude: Double, override val longitude: Double,
@SerialName(livePeriodField) @SerialName(livePeriodField)
@OptIn(ExperimentalSerializationApi::class)
@EncodeDefault @EncodeDefault
override val livePeriod: Seconds = LiveLocation.INDEFINITE_LIVE_PERIOD, override val livePeriod: Seconds = LiveLocation.INDEFINITE_LIVE_PERIOD,
@SerialName(horizontalAccuracyField) @SerialName(horizontalAccuracyField)
@ -130,9 +134,11 @@ sealed interface SendLocation<T : LocationContent> : SendContentMessageRequest<C
@SerialName(proximityAlertRadiusField) @SerialName(proximityAlertRadiusField)
override val proximityAlertRadius: Meters? = null, override val proximityAlertRadius: Meters? = null,
@SerialName(messageThreadIdField) @SerialName(messageThreadIdField)
@OptIn(ExperimentalSerializationApi::class)
@EncodeDefault @EncodeDefault
override val threadId: MessageThreadId? = chatId.threadId, override val threadId: MessageThreadId? = chatId.threadId,
@SerialName(businessConnectionIdField) @SerialName(businessConnectionIdField)
@OptIn(ExperimentalSerializationApi::class)
@EncodeDefault @EncodeDefault
override val businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, override val businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,
@SerialName(disableNotificationField) @SerialName(disableNotificationField)
@ -149,7 +155,7 @@ sealed interface SendLocation<T : LocationContent> : SendContentMessageRequest<C
override val resultDeserializer: DeserializationStrategy<ContentMessage<LiveLocationContent>> override val resultDeserializer: DeserializationStrategy<ContentMessage<LiveLocationContent>>
get() = liveResultDeserializer get() = liveResultDeserializer
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = Live.serializer() get() = serializer()
init { init {
if (livePeriod !in livePeriodLimit) { if (livePeriod !in livePeriodLimit) {
@ -170,9 +176,11 @@ sealed interface SendLocation<T : LocationContent> : SendContentMessageRequest<C
@SerialName(longitudeField) @SerialName(longitudeField)
override val longitude: Double, override val longitude: Double,
@SerialName(messageThreadIdField) @SerialName(messageThreadIdField)
@OptIn(ExperimentalSerializationApi::class)
@EncodeDefault @EncodeDefault
override val threadId: MessageThreadId? = chatId.threadId, override val threadId: MessageThreadId? = chatId.threadId,
@SerialName(businessConnectionIdField) @SerialName(businessConnectionIdField)
@OptIn(ExperimentalSerializationApi::class)
@EncodeDefault @EncodeDefault
override val businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, override val businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,
@SerialName(disableNotificationField) @SerialName(disableNotificationField)
@ -199,4 +207,98 @@ sealed interface SendLocation<T : LocationContent> : SendContentMessageRequest<C
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
} }
companion object : KSerializer<SendLocation<*>> {
@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)
}
}
} }