diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/LinkPreviewOptions.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/LinkPreviewOptions.kt index 3db380420b..d553b676e6 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/LinkPreviewOptions.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/LinkPreviewOptions.kt @@ -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 { + 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) + } + } + + } } \ No newline at end of file