1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 12:08:43 +00:00

rework of stickers and stickersets

This commit is contained in:
InsanusMokrassar 2022-08-13 11:58:42 +06:00
parent 3fc1058491
commit c72dccc0f9
3 changed files with 388 additions and 75 deletions

View File

@ -59,6 +59,8 @@ sealed interface StickerType {
object Mask : StickerType { override val type: String = "mask" } object Mask : StickerType { override val type: String = "mask" }
@Serializable @Serializable
object CustomEmoji : StickerType { override val type: String = "custom_emoji" } object CustomEmoji : StickerType { override val type: String = "custom_emoji" }
@Serializable
data class Unknown(override val type: String = "custom_emoji") : StickerType
object Serializer : KSerializer<StickerType> { object Serializer : KSerializer<StickerType> {
override val descriptor: SerialDescriptor = String.serializer().descriptor override val descriptor: SerialDescriptor = String.serializer().descriptor
@ -68,7 +70,7 @@ sealed interface StickerType {
Regular.type -> Regular Regular.type -> Regular
Mask.type -> Regular Mask.type -> Regular
CustomEmoji.type -> Regular CustomEmoji.type -> Regular
else -> error("Unknown type of emoji $type") else -> Unknown(type)
} }
} }

View File

@ -4,10 +4,12 @@ import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.stickers.MaskPosition import dev.inmo.tgbotapi.types.stickers.MaskPosition
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement
@Serializable @Serializable
@RiskFeature("This class is used for serialization/deserialization of Sticker interface") @RiskFeature("This class is used for serialization/deserialization of Sticker interface")
@ -44,7 +46,8 @@ object StickerSerializer : KSerializer<Sticker> {
override val descriptor: SerialDescriptor = StickerSurrogate.serializer().descriptor override val descriptor: SerialDescriptor = StickerSurrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): Sticker { override fun deserialize(decoder: Decoder): Sticker {
val surrogate = StickerSurrogate.serializer().deserialize(decoder) val json = JsonElement.serializer().deserialize(decoder)
val surrogate = nonstrictJsonFormat.decodeFromJsonElement(StickerSurrogate.serializer(), json)
return when (surrogate.type) { return when (surrogate.type) {
StickerType.Regular -> when { StickerType.Regular -> when {
@ -82,19 +85,41 @@ object StickerSerializer : KSerializer<Sticker> {
surrogate.file_size surrogate.file_size
) )
} }
StickerType.Mask -> MaskSticker( StickerType.Mask -> when {
surrogate.is_animated == true -> MaskAnimatedSticker(
surrogate.file_id, surrogate.file_id,
surrogate.file_unique_id, surrogate.file_unique_id,
surrogate.width, surrogate.width,
surrogate.height, surrogate.height,
surrogate.mask_position ?: error("For mask stickers field mask_position should be presented"), surrogate.mask_position ?: error("For mask stickers field mask_position should be presented"),
surrogate.is_animated == true,
surrogate.is_video == true,
surrogate.thumb, surrogate.thumb,
surrogate.emoji, surrogate.emoji,
surrogate.set_name, surrogate.set_name,
surrogate.file_size surrogate.file_size
) )
surrogate.is_video == true -> MaskVideoSticker(
surrogate.file_id,
surrogate.file_unique_id,
surrogate.width,
surrogate.height,
surrogate.mask_position ?: error("For mask stickers field mask_position should be presented"),
surrogate.thumb,
surrogate.emoji,
surrogate.set_name,
surrogate.file_size
)
else -> MaskSimpleSticker(
surrogate.file_id,
surrogate.file_unique_id,
surrogate.width,
surrogate.height,
surrogate.mask_position ?: error("For mask stickers field mask_position should be presented"),
surrogate.thumb,
surrogate.emoji,
surrogate.set_name,
surrogate.file_size
)
}
StickerType.CustomEmoji -> when { StickerType.CustomEmoji -> when {
surrogate.is_animated == true -> CustomEmojiAnimatedSticker( surrogate.is_animated == true -> CustomEmojiAnimatedSticker(
surrogate.file_id, surrogate.file_id,
@ -130,6 +155,17 @@ object StickerSerializer : KSerializer<Sticker> {
surrogate.file_size surrogate.file_size
) )
} }
is StickerType.Unknown -> UnknownSticker(
surrogate.file_id,
surrogate.file_unique_id,
surrogate.width,
surrogate.height,
surrogate.thumb,
surrogate.emoji,
surrogate.set_name,
surrogate.file_size,
json
)
} }
} }
@ -139,48 +175,22 @@ object StickerSerializer : KSerializer<Sticker> {
} }
@Serializable(StickerSerializer::class) @Serializable
sealed interface VideoSticker : Sticker { sealed interface VideoSticker : Sticker {
override val isVideo: Boolean override val isVideo: Boolean
get() = true get() = true
} }
@Serializable(StickerSerializer::class) @Serializable
sealed interface AnimatedSticker : Sticker { sealed interface AnimatedSticker : Sticker {
override val isAnimated: Boolean override val isAnimated: Boolean
get() = true get() = true
} }
@Serializable(StickerSerializer::class) @Serializable
sealed interface RegularSticker : Sticker { sealed interface RegularSticker : Sticker {
val premiumAnimationFile: File? val premiumAnimationFile: File?
} }
@Serializable
data class MaskSticker(
@SerialName(fileIdField)
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(widthField)
override val width: Int,
@SerialName(heightField)
override val height: Int,
@SerialName(maskPositionField)
val maskPosition: MaskPosition,
@SerialName(isAnimatedField)
override val isAnimated: Boolean,
@SerialName(isVideoField)
override val isVideo: Boolean,
@SerialName(thumbField)
override val thumb: PhotoSize? = null,
@SerialName(emojiField)
override val emoji: String? = null,
@SerialName(stickerSetNameField)
override val stickerSetName: StickerSetName? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null,
) : Sticker
@Serializable @Serializable
data class RegularSimpleSticker( data class RegularSimpleSticker(
@SerialName(fileIdField) @SerialName(fileIdField)
@ -247,7 +257,76 @@ data class RegularVideoSticker(
override val fileSize: Long? = null, override val fileSize: Long? = null,
) : RegularSticker, VideoSticker ) : RegularSticker, VideoSticker
@Serializable(StickerSerializer::class)
@Serializable
sealed interface MaskSticker : Sticker {
val maskPosition: MaskPosition
}
@Serializable
data class MaskSimpleSticker(
@SerialName(fileIdField)
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(widthField)
override val width: Int,
@SerialName(heightField)
override val height: Int,
@SerialName(maskPositionField)
override val maskPosition: MaskPosition,
@SerialName(thumbField)
override val thumb: PhotoSize? = null,
@SerialName(emojiField)
override val emoji: String? = null,
@SerialName(stickerSetNameField)
override val stickerSetName: StickerSetName? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null,
) : MaskSticker
@Serializable
data class MaskAnimatedSticker(
@SerialName(fileIdField)
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(widthField)
override val width: Int,
@SerialName(heightField)
override val height: Int,
@SerialName(maskPositionField)
override val maskPosition: MaskPosition,
@SerialName(thumbField)
override val thumb: PhotoSize? = null,
@SerialName(emojiField)
override val emoji: String? = null,
@SerialName(stickerSetNameField)
override val stickerSetName: StickerSetName? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null,
) : MaskSticker, AnimatedSticker
@Serializable
data class MaskVideoSticker(
@SerialName(fileIdField)
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(widthField)
override val width: Int,
@SerialName(heightField)
override val height: Int,
@SerialName(maskPositionField)
override val maskPosition: MaskPosition,
@SerialName(thumbField)
override val thumb: PhotoSize? = null,
@SerialName(emojiField)
override val emoji: String? = null,
@SerialName(stickerSetNameField)
override val stickerSetName: StickerSetName? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null,
) : MaskSticker, VideoSticker
@Serializable
sealed interface CustomEmojiSticker : Sticker { sealed interface CustomEmojiSticker : Sticker {
val customEmojiId: CustomEmojiId val customEmojiId: CustomEmojiId
} }
@ -315,3 +394,24 @@ data class CustomEmojiVideoSticker(
@SerialName(fileSizeField) @SerialName(fileSizeField)
override val fileSize: Long? = null, override val fileSize: Long? = null,
) : CustomEmojiSticker, VideoSticker ) : CustomEmojiSticker, VideoSticker
@Serializable
data class UnknownSticker(
@SerialName(fileIdField)
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(widthField)
override val width: Int,
@SerialName(heightField)
override val height: Int,
@SerialName(thumbField)
override val thumb: PhotoSize? = null,
@SerialName(emojiField)
override val emoji: String? = null,
@SerialName(stickerSetNameField)
override val stickerSetName: StickerSetName? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null,
val raw: JsonElement
) : Sticker

View File

@ -2,86 +2,297 @@ package dev.inmo.tgbotapi.types.stickers
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.* import dev.inmo.tgbotapi.types.files.*
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
@Serializable @Serializable
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 { sealed interface StickerSet {
@SerialName(nameField)
val name: String val name: String
@SerialName(titleField)
val title: String val title: String
@SerialName(stickerTypeField)
val stickerType: StickerType val stickerType: StickerType
@SerialName(stickersField)
val stickers: List<Sticker> val stickers: List<Sticker>
@SerialName(isAnimatedField)
val isAnimated: Boolean val isAnimated: Boolean
@SerialName(isVideoField) get() = false
val isVideo: Boolean val isVideo: Boolean
@SerialName(containsMasksField) get() = false
val thumb: PhotoSize?
@Deprecated("Will be removed soon due to its redundancy") @Deprecated("Will be removed soon due to its redundancy")
val containsMasks: Boolean val containsMasks: Boolean
get() = this is MaskStickerSet get() = this is MaskStickerSet
@SerialName(thumbField)
val thumb: PhotoSize? 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 @Serializable
data class RegularStickerSet( sealed interface AnimatedStickerSet : StickerSet {
override val isAnimated: Boolean
get() = true
}
@Serializable
sealed interface VideoStickerSet : StickerSet {
override val isVideo: Boolean
get() = true
}
@Serializable
sealed interface RegularStickerSet : StickerSet
@Serializable
sealed interface MaskStickerSet : StickerSet
@Serializable
sealed interface CustomEmojiStickerSet : StickerSet
@Serializable
data class RegularSimpleStickerSet(
@SerialName(nameField) @SerialName(nameField)
override val name: String, override val name: String,
@SerialName(titleField) @SerialName(titleField)
override val title: String, override val title: String,
@SerialName(stickersField) @SerialName(stickersField)
override val stickers: List<RegularSticker>, override val stickers: List<RegularSimpleSticker>,
@SerialName(isAnimatedField)
override val isAnimated: Boolean = false,
@SerialName(isVideoField)
override val isVideo: Boolean = false,
@SerialName(thumbField) @SerialName(thumbField)
override val thumb: PhotoSize? = null override val thumb: PhotoSize? = null
) : StickerSet { ) : RegularStickerSet {
@SerialName(stickerTypeField) @SerialName(stickerTypeField)
@EncodeDefault @EncodeDefault
override val stickerType: StickerType = StickerType.Regular override val stickerType: StickerType = StickerType.Regular
} }
@Serializable @Serializable
data class MaskStickerSet( data class RegularAnimatedStickerSet(
@SerialName(nameField) @SerialName(nameField)
override val name: String, override val name: String,
@SerialName(titleField) @SerialName(titleField)
override val title: String, override val title: String,
@SerialName(stickersField) @SerialName(stickersField)
override val stickers: List<MaskSticker>, override val stickers: List<RegularAnimatedSticker>,
@SerialName(isAnimatedField)
override val isAnimated: Boolean = false,
@SerialName(isVideoField)
override val isVideo: Boolean = false,
@SerialName(thumbField) @SerialName(thumbField)
override val thumb: PhotoSize? = null override val thumb: PhotoSize? = null
) : StickerSet { ) : RegularStickerSet, AnimatedStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
}
@Serializable
data class RegularVideoStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<RegularVideoSticker>,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : RegularStickerSet, VideoStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
}
@Serializable
data class MaskSimpleStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<MaskSimpleSticker>,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : MaskStickerSet {
@SerialName(stickerTypeField) @SerialName(stickerTypeField)
@EncodeDefault @EncodeDefault
override val stickerType: StickerType = StickerType.Mask override val stickerType: StickerType = StickerType.Mask
} }
@Serializable @Serializable
data class CustomEmojiStickerSet( data class MaskAnimatedStickerSet(
@SerialName(nameField) @SerialName(nameField)
override val name: String, override val name: String,
@SerialName(titleField) @SerialName(titleField)
override val title: String, override val title: String,
@SerialName(stickersField) @SerialName(stickersField)
override val stickers: List<CustomEmojiSticker>, override val stickers: List<MaskAnimatedSticker>,
@SerialName(isAnimatedField)
override val isAnimated: Boolean = false,
@SerialName(isVideoField)
override val isVideo: Boolean = false,
@SerialName(thumbField) @SerialName(thumbField)
override val thumb: PhotoSize? = null override val thumb: PhotoSize? = null
) : StickerSet { ) : 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>,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : 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>,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : CustomEmojiStickerSet {
@SerialName(stickerTypeField) @SerialName(stickerTypeField)
@EncodeDefault @EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji override val stickerType: StickerType = StickerType.CustomEmoji
} }
@Serializable
data class CustomEmojiAnimatedStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiAnimatedSticker>,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : 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>,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : CustomEmojiStickerSet, VideoStickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
}
@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,
@SerialName(thumbField)
override val thumb: PhotoSize? = null,
val raw: JsonElement
) : CustomEmojiStickerSet, VideoStickerSet