1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-23 00:57:13 +00:00

fixes in LinkPreviewOptions

This commit is contained in:
InsanusMokrassar 2024-01-08 19:16:17 +06:00
parent f0448485c8
commit eac45b86a6

View File

@ -1,10 +1,14 @@
package dev.inmo.tgbotapi.types
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Required
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@Serializable
@Serializable(LinkPreviewOptions.Companion::class)
sealed interface LinkPreviewOptions {
val isDisabled: Boolean
val url: String?
@ -76,4 +80,47 @@ sealed interface LinkPreviewOptions {
override val preferLargeMedia: Boolean
get() = false
}
@Serializable
private data class Surrogate(
@Required
@SerialName(isDisabledField)
val isDisabled: Boolean = true,
@SerialName(urlField)
val url: String? = null,
@SerialName(preferSmallMediaField)
val preferSmallMedia: Boolean = false,
@SerialName(preferLargeMediaField)
val preferLargeMedia: Boolean = false,
@SerialName(showAboveTextField)
val showAboveText: Boolean = false,
) {
}
companion object : KSerializer<LinkPreviewOptions> {
override val descriptor: SerialDescriptor
get() = Surrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): LinkPreviewOptions {
val surrogate = Surrogate.serializer().deserialize(decoder)
return when {
surrogate.isDisabled -> Disabled
surrogate.preferLargeMedia -> Large(surrogate.url, surrogate.showAboveText)
surrogate.preferSmallMedia -> Small(surrogate.url, surrogate.showAboveText)
else -> Medium(surrogate.url, surrogate.showAboveText)
}
}
override fun serialize(encoder: Encoder, value: LinkPreviewOptions) {
when (value) {
is Disabled -> Disabled.serializer().serialize(encoder, value)
is Large -> Large.serializer().serialize(encoder, value)
is Medium -> Medium.serializer().serialize(encoder, value)
is Small -> Small.serializer().serialize(encoder, value)
}
}
}
}