tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/stickers/StickerSet.kt

315 lines
11 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.types.stickers
2018-12-26 08:07:24 +00:00
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.*
2022-08-13 05:58:42 +00:00
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.*
2022-08-13 05:58:42 +00:00
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
2018-12-26 08:07:24 +00:00
@Serializable
2022-08-13 05:58:42 +00:00
data class SurrogateStickerSet(
val name: String,
val title: String,
val sticker_type: StickerType,
val is_animated: Boolean? = false,
val is_video: Boolean? = false,
val stickers: List<@Serializable(StickerSerializer::class) Sticker> = emptyList(),
val thumb: PhotoSize? = null
)
@Serializable(StickerSet.Serializer::class)
sealed interface StickerSet {
val name: String
val title: String
val stickerType: StickerType
2023-03-10 18:53:36 +00:00
val stickerFormat: StickerFormat
val stickers: List<Sticker>
val isAnimated: Boolean
2022-08-13 05:58:42 +00:00
get() = false
val isVideo: Boolean
2022-08-13 05:58:42 +00:00
get() = false
2023-03-10 08:00:57 +00:00
val thumbnail: PhotoSize?
2022-08-13 05:58:42 +00:00
object Serializer : KSerializer<StickerSet> {
override val descriptor: SerialDescriptor = JsonElement.serializer().descriptor
override fun deserialize(decoder: Decoder): StickerSet {
val json = JsonElement.serializer().deserialize(decoder)
val surrogate = nonstrictJsonFormat.decodeFromJsonElement(SurrogateStickerSet.serializer(), json)
return when (surrogate.sticker_type) {
StickerType.CustomEmoji -> when {
surrogate.is_animated == true -> CustomEmojiAnimatedStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<CustomEmojiAnimatedSticker>(),
surrogate.thumb
)
surrogate.is_video == true -> CustomEmojiVideoStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<CustomEmojiVideoSticker>(),
surrogate.thumb
)
else -> CustomEmojiSimpleStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<CustomEmojiSimpleSticker>(),
surrogate.thumb
)
}
StickerType.Mask -> when {
surrogate.is_animated == true -> MaskAnimatedStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<MaskAnimatedSticker>(),
surrogate.thumb
)
surrogate.is_video == true -> MaskVideoStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<MaskVideoSticker>(),
surrogate.thumb
)
else -> MaskSimpleStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<MaskSimpleSticker>(),
surrogate.thumb
)
}
StickerType.Regular -> when {
surrogate.is_animated == true -> RegularAnimatedStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularAnimatedSticker>(),
surrogate.thumb
)
surrogate.is_video == true -> RegularVideoStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularVideoSticker>(),
surrogate.thumb
)
else -> RegularSimpleStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularSimpleSticker>(),
surrogate.thumb
)
}
is StickerType.Unknown -> UnknownStickerSet(
surrogate.name,
surrogate.title,
surrogate.stickers.filterIsInstance<RegularSimpleSticker>(),
surrogate.sticker_type,
surrogate.thumb,
json
)
}
}
override fun serialize(encoder: Encoder, value: StickerSet) {
TODO("Not yet implemented")
}
}
}
@Serializable
sealed interface AnimatedStickerSet : StickerSet {
override val isAnimated: Boolean
get() = true
2023-03-10 18:53:36 +00:00
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat
get() = StickerFormat.Animated
2022-08-13 05:58:42 +00:00
}
@Serializable
sealed interface VideoStickerSet : StickerSet {
override val isVideo: Boolean
get() = true
2023-03-10 18:53:36 +00:00
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat
get() = StickerFormat.Video
2022-08-13 05:58:42 +00:00
}
@Serializable
sealed interface RegularStickerSet : StickerSet
@Serializable
sealed interface MaskStickerSet : StickerSet
@Serializable
sealed interface CustomEmojiStickerSet : StickerSet
@Serializable
data class RegularSimpleStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<RegularSimpleSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : RegularStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
2023-03-10 18:53:36 +00:00
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat = StickerFormat.Static
2022-08-13 05:58:42 +00:00
}
@Serializable
data class RegularAnimatedStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<RegularAnimatedSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : RegularStickerSet, AnimatedStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
}
@Serializable
2022-08-13 05:58:42 +00:00
data class RegularVideoStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
2022-08-13 05:58:42 +00:00
override val stickers: List<RegularVideoSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : RegularStickerSet, VideoStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
}
@Serializable
2022-08-13 05:58:42 +00:00
data class MaskSimpleStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
2022-08-13 05:58:42 +00:00
override val stickers: List<MaskSimpleSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : MaskStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Mask
2023-03-10 18:53:36 +00:00
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat = StickerFormat.Static
}
@Serializable
2022-08-13 05:58:42 +00:00
data class MaskAnimatedStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
2022-08-13 05:58:42 +00:00
override val stickers: List<MaskAnimatedSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : MaskStickerSet, AnimatedStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Mask
}
@Serializable
data class MaskVideoStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<MaskVideoSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : MaskStickerSet, VideoStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Mask
}
@Serializable
data class CustomEmojiSimpleStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiSimpleSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : CustomEmojiStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
2023-03-10 18:53:36 +00:00
@SerialName(stickerFormatField)
@EncodeDefault
override val stickerFormat: StickerFormat = StickerFormat.Static
2022-08-13 05:58:42 +00:00
}
@Serializable
data class CustomEmojiAnimatedStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiAnimatedSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : CustomEmojiStickerSet, AnimatedStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
}
@Serializable
data class CustomEmojiVideoStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiVideoSticker>,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null
2022-08-13 05:58:42 +00:00
) : CustomEmojiStickerSet, VideoStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
}
2022-08-13 05:58:42 +00:00
@Serializable
data class UnknownStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<Sticker>,
@SerialName(stickerTypeField)
override val stickerType: StickerType,
2023-03-10 06:01:08 +00:00
@SerialName(thumbnailField)
2023-03-10 08:00:57 +00:00
override val thumbnail: PhotoSize? = null,
2022-08-13 05:58:42 +00:00
val raw: JsonElement
) : CustomEmojiStickerSet, VideoStickerSet