1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

start rework of stickers and stickersets

This commit is contained in:
InsanusMokrassar 2022-08-13 00:02:48 +06:00
parent c3668e978b
commit 3fc1058491
7 changed files with 336 additions and 59 deletions

View File

@ -0,0 +1,20 @@
package dev.inmo.tgbotapi.requests.get
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.CustomEmojiSticker
import dev.inmo.tgbotapi.types.files.StickerSerializer
import dev.inmo.tgbotapi.types.stickers.StickerSet
import kotlinx.serialization.*
@Serializable
data class GetCustomEmojiStickers(
@SerialName(customEmojiIdsField)
val customEmojiIds: List<CustomEmojiId>
): SimpleRequest<CustomEmojiSticker> {
override fun method(): String = "getCustomEmojiStickers"
override val resultDeserializer: DeserializationStrategy<CustomEmojiSticker>
get() = StickerSerializer as DeserializationStrategy<CustomEmojiSticker>
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@ -40,6 +40,7 @@ data class CreateNewAnimatedStickerSet internal constructor(
@SerialName(tgsStickerField) @SerialName(tgsStickerField)
val sticker: FileId? = null, val sticker: FileId? = null,
@SerialName(containsMasksField) @SerialName(containsMasksField)
@Deprecated("Will be removed soon due to its redundancy")
val containsMasks: Boolean? = null, val containsMasks: Boolean? = null,
@SerialName(maskPositionField) @SerialName(maskPositionField)
override val maskPosition: MaskPosition? = null override val maskPosition: MaskPosition? = null

View File

@ -50,6 +50,7 @@ data class CreateNewStaticStickerSet internal constructor(
@SerialName(pngStickerField) @SerialName(pngStickerField)
val sticker: FileId? = null, val sticker: FileId? = null,
@SerialName(containsMasksField) @SerialName(containsMasksField)
@Deprecated("Will be removed soon due to its redundancy")
val containsMasks: Boolean? = null, val containsMasks: Boolean? = null,
@SerialName(maskPositionField) @SerialName(maskPositionField)
override val maskPosition: MaskPosition? = null override val maskPosition: MaskPosition? = null

View File

@ -40,6 +40,7 @@ data class CreateNewVideoStickerSet internal constructor(
@SerialName(webmStickerField) @SerialName(webmStickerField)
val sticker: FileId? = null, val sticker: FileId? = null,
@SerialName(containsMasksField) @SerialName(containsMasksField)
@Deprecated("Will be removed soon due to its redundancy")
val containsMasks: Boolean? = null, val containsMasks: Boolean? = null,
@SerialName(maskPositionField) @SerialName(maskPositionField)
override val maskPosition: MaskPosition? = null override val maskPosition: MaskPosition? = null

View File

@ -1,7 +1,12 @@
package dev.inmo.tgbotapi.types package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.utils.BuiltinMimeTypes import dev.inmo.tgbotapi.utils.BuiltinMimeTypes
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.jvm.JvmInline import kotlin.jvm.JvmInline
typealias Identifier = Long typealias Identifier = Long
@ -44,6 +49,36 @@ typealias UnixTimeStamp = LongSeconds
typealias Meters = Float typealias Meters = Float
typealias Degrees = Int typealias Degrees = Int
@Serializable(StickerType.Serializer::class)
sealed interface StickerType {
val type: String
@Serializable
object Regular : StickerType { override val type: String = "regular" }
@Serializable
object Mask : StickerType { override val type: String = "mask" }
@Serializable
object CustomEmoji : StickerType { override val type: String = "custom_emoji" }
object Serializer : KSerializer<StickerType> {
override val descriptor: SerialDescriptor = String.serializer().descriptor
override fun deserialize(decoder: Decoder): StickerType {
return when (val type = decoder.decodeString()) {
Regular.type -> Regular
Mask.type -> Regular
CustomEmoji.type -> Regular
else -> error("Unknown type of emoji $type")
}
}
override fun serialize(encoder: Encoder, value: StickerType) {
encoder.encodeString(value.type)
}
}
}
val degreesLimit = 1 .. 360 val degreesLimit = 1 .. 360
val horizontalAccuracyLimit = 0F .. 1500F val horizontalAccuracyLimit = 0F .. 1500F
@ -133,6 +168,8 @@ const val supportInlineQueriesField = "supports_inline_queries"
const val textEntitiesField = "text_entities" const val textEntitiesField = "text_entities"
const val entitiesField = "entities" const val entitiesField = "entities"
const val stickerSetNameField = "set_name" const val stickerSetNameField = "set_name"
const val customEmojiIdField = "custom_emoji_id"
const val customEmojiIdsField = "custom_emoji_ids"
const val premiumAnimationField = "premium_animation" const val premiumAnimationField = "premium_animation"
const val stickerSetNameFullField = "sticker_set_name" const val stickerSetNameFullField = "sticker_set_name"
const val slowModeDelayField = "slow_mode_delay" const val slowModeDelayField = "slow_mode_delay"
@ -297,6 +334,7 @@ const val tgsStickerField = "tgs_sticker"
const val webmStickerField = "webm_sticker" const val webmStickerField = "webm_sticker"
const val oldChatMemberField = "old_chat_member" const val oldChatMemberField = "old_chat_member"
const val newChatMemberField = "new_chat_member" const val newChatMemberField = "new_chat_member"
const val stickerTypeField = "sticker_type"
const val okField = "ok" const val okField = "ok"
const val captionField = "caption" const val captionField = "caption"

View File

@ -14,6 +14,7 @@ import kotlinx.serialization.encoding.Encoder
data class StickerSurrogate( data class StickerSurrogate(
val file_id: FileId, val file_id: FileId,
val file_unique_id: FileUniqueId, val file_unique_id: FileUniqueId,
val type: StickerType,
val width: Int, val width: Int,
val height: Int, val height: Int,
val is_animated: Boolean? = null, val is_animated: Boolean? = null,
@ -21,23 +22,22 @@ data class StickerSurrogate(
val thumb: PhotoSize? = null, val thumb: PhotoSize? = null,
val emoji: String? = null, val emoji: String? = null,
val set_name: StickerSetName? = null, val set_name: StickerSetName? = null,
val premium_animation: File? = null,
val mask_position: MaskPosition? = null, val mask_position: MaskPosition? = null,
val file_size: Long? = null, val custom_emoji_id: CustomEmojiId? = null,
val premium_animation: File? = null val file_size: Long? = null
) )
// TODO:: Serializer // TODO:: Serializer
@Serializable(StickerSerializer::class) @Serializable(StickerSerializer::class)
sealed interface Sticker : TelegramMediaFile, SizedMediaFile, ThumbedMediaFile { sealed interface Sticker : TelegramMediaFile, SizedMediaFile, ThumbedMediaFile {
val emoji: String? val emoji: String?
val maskPosition: MaskPosition?
val stickerSetName: StickerSetName? val stickerSetName: StickerSetName?
val premiumAnimationFile: File?
val isAnimated val isAnimated
get() = this is AnimatedSticker get() = false
val isVideo val isVideo
get() = this is VideoSticker get() = false
} }
object StickerSerializer : KSerializer<Sticker> { object StickerSerializer : KSerializer<Sticker> {
@ -46,8 +46,9 @@ object StickerSerializer : KSerializer<Sticker> {
override fun deserialize(decoder: Decoder): Sticker { override fun deserialize(decoder: Decoder): Sticker {
val surrogate = StickerSurrogate.serializer().deserialize(decoder) val surrogate = StickerSurrogate.serializer().deserialize(decoder)
return when { return when (surrogate.type) {
surrogate.is_animated == true -> AnimatedSticker( StickerType.Regular -> when {
surrogate.is_animated == true -> RegularAnimatedSticker(
surrogate.file_id, surrogate.file_id,
surrogate.file_unique_id, surrogate.file_unique_id,
surrogate.width, surrogate.width,
@ -56,10 +57,9 @@ object StickerSerializer : KSerializer<Sticker> {
surrogate.emoji, surrogate.emoji,
surrogate.set_name, surrogate.set_name,
surrogate.premium_animation, surrogate.premium_animation,
surrogate.mask_position,
surrogate.file_size surrogate.file_size
) )
surrogate.is_video == true -> VideoSticker( surrogate.is_video == true -> RegularVideoSticker(
surrogate.file_id, surrogate.file_id,
surrogate.file_unique_id, surrogate.file_unique_id,
surrogate.width, surrogate.width,
@ -68,10 +68,9 @@ object StickerSerializer : KSerializer<Sticker> {
surrogate.emoji, surrogate.emoji,
surrogate.set_name, surrogate.set_name,
surrogate.premium_animation, surrogate.premium_animation,
surrogate.mask_position,
surrogate.file_size surrogate.file_size
) )
else -> SimpleSticker( else -> RegularSimpleSticker(
surrogate.file_id, surrogate.file_id,
surrogate.file_unique_id, surrogate.file_unique_id,
surrogate.width, surrogate.width,
@ -80,10 +79,58 @@ object StickerSerializer : KSerializer<Sticker> {
surrogate.emoji, surrogate.emoji,
surrogate.set_name, surrogate.set_name,
surrogate.premium_animation, surrogate.premium_animation,
surrogate.mask_position,
surrogate.file_size surrogate.file_size
) )
} }
StickerType.Mask -> MaskSticker(
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.is_animated == true,
surrogate.is_video == true,
surrogate.thumb,
surrogate.emoji,
surrogate.set_name,
surrogate.file_size
)
StickerType.CustomEmoji -> when {
surrogate.is_animated == true -> CustomEmojiAnimatedSticker(
surrogate.file_id,
surrogate.file_unique_id,
surrogate.width,
surrogate.height,
surrogate.custom_emoji_id ?: error("For mask stickers field mask_position should be presented"),
surrogate.thumb,
surrogate.emoji,
surrogate.set_name,
surrogate.file_size
)
surrogate.is_video == true -> CustomEmojiVideoSticker(
surrogate.file_id,
surrogate.file_unique_id,
surrogate.width,
surrogate.height,
surrogate.custom_emoji_id ?: error("For mask stickers field mask_position should be presented"),
surrogate.thumb,
surrogate.emoji,
surrogate.set_name,
surrogate.file_size
)
else -> CustomEmojiSimpleSticker(
surrogate.file_id,
surrogate.file_unique_id,
surrogate.width,
surrogate.height,
surrogate.custom_emoji_id ?: error("For mask stickers field mask_position should be presented"),
surrogate.thumb,
surrogate.emoji,
surrogate.set_name,
surrogate.file_size
)
}
}
} }
override fun serialize(encoder: Encoder, value: Sticker) { override fun serialize(encoder: Encoder, value: Sticker) {
@ -92,8 +139,50 @@ object StickerSerializer : KSerializer<Sticker> {
} }
@Serializable(StickerSerializer::class)
sealed interface VideoSticker : Sticker {
override val isVideo: Boolean
get() = true
}
@Serializable(StickerSerializer::class)
sealed interface AnimatedSticker : Sticker {
override val isAnimated: Boolean
get() = true
}
@Serializable(StickerSerializer::class)
sealed interface RegularSticker : Sticker {
val premiumAnimationFile: File?
}
@Serializable @Serializable
data class SimpleSticker( 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
data class RegularSimpleSticker(
@SerialName(fileIdField) @SerialName(fileIdField)
override val fileId: FileId, override val fileId: FileId,
@SerialName(fileUniqueIdField) @SerialName(fileUniqueIdField)
@ -110,13 +199,13 @@ data class SimpleSticker(
override val stickerSetName: StickerSetName? = null, override val stickerSetName: StickerSetName? = null,
@SerialName(premiumAnimationField) @SerialName(premiumAnimationField)
override val premiumAnimationFile: File?, override val premiumAnimationFile: File?,
@SerialName(maskPositionField)
override val maskPosition: MaskPosition? = null,
@SerialName(fileSizeField) @SerialName(fileSizeField)
override val fileSize: Long? = null, override val fileSize: Long? = null,
) : Sticker ) : RegularSticker
@Deprecated("Renamed", ReplaceWith("SimpleRegularSticker", "dev.inmo.tgbotapi.types.files.SimpleRegularSticker"))
typealias SimpleSticker = RegularSimpleSticker
@Serializable @Serializable
data class AnimatedSticker( data class RegularAnimatedSticker(
@SerialName(fileIdField) @SerialName(fileIdField)
override val fileId: FileId, override val fileId: FileId,
@SerialName(fileUniqueIdField) @SerialName(fileUniqueIdField)
@ -133,13 +222,11 @@ data class AnimatedSticker(
override val stickerSetName: StickerSetName? = null, override val stickerSetName: StickerSetName? = null,
@SerialName(premiumAnimationField) @SerialName(premiumAnimationField)
override val premiumAnimationFile: File?, override val premiumAnimationFile: File?,
@SerialName(maskPositionField)
override val maskPosition: MaskPosition? = null,
@SerialName(fileSizeField) @SerialName(fileSizeField)
override val fileSize: Long? = null, override val fileSize: Long? = null,
) : Sticker ) : RegularSticker, AnimatedSticker
@Serializable @Serializable
data class VideoSticker( data class RegularVideoSticker(
@SerialName(fileIdField) @SerialName(fileIdField)
override val fileId: FileId, override val fileId: FileId,
@SerialName(fileUniqueIdField) @SerialName(fileUniqueIdField)
@ -156,8 +243,75 @@ data class VideoSticker(
override val stickerSetName: StickerSetName? = null, override val stickerSetName: StickerSetName? = null,
@SerialName(premiumAnimationField) @SerialName(premiumAnimationField)
override val premiumAnimationFile: File?, override val premiumAnimationFile: File?,
@SerialName(maskPositionField)
override val maskPosition: MaskPosition? = null,
@SerialName(fileSizeField) @SerialName(fileSizeField)
override val fileSize: Long? = null, override val fileSize: Long? = null,
) : Sticker ) : RegularSticker, VideoSticker
@Serializable(StickerSerializer::class)
sealed interface CustomEmojiSticker : Sticker {
val customEmojiId: CustomEmojiId
}
@Serializable
data class CustomEmojiSimpleSticker(
@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(customEmojiIdField)
override val customEmojiId: CustomEmojiId,
@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,
) : CustomEmojiSticker
@Serializable
data class CustomEmojiAnimatedSticker(
@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(customEmojiIdField)
override val customEmojiId: CustomEmojiId,
@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,
) : CustomEmojiSticker, AnimatedSticker
@Serializable
data class CustomEmojiVideoSticker(
@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(customEmojiIdField)
override val customEmojiId: CustomEmojiId,
@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,
) : CustomEmojiSticker, VideoSticker

View File

@ -1,25 +1,87 @@
package dev.inmo.tgbotapi.types.stickers package dev.inmo.tgbotapi.types.stickers
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.PhotoSize import dev.inmo.tgbotapi.types.files.*
import dev.inmo.tgbotapi.types.files.Sticker import kotlinx.serialization.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable @Serializable
data class StickerSet( sealed interface StickerSet {
@SerialName(nameField) @SerialName(nameField)
val name: String, val name: String
@SerialName(titleField) @SerialName(titleField)
val title: String, val title: String
@SerialName(stickerTypeField)
val stickerType: StickerType
@SerialName(stickersField) @SerialName(stickersField)
val stickers: List<Sticker>, val stickers: List<Sticker>
@SerialName(isAnimatedField) @SerialName(isAnimatedField)
val isAnimated: Boolean = false, val isAnimated: Boolean
@SerialName(isVideoField) @SerialName(isVideoField)
val isVideo: Boolean = false, val isVideo: Boolean
@SerialName(containsMasksField) @SerialName(containsMasksField)
val containsMasks: Boolean = false, @Deprecated("Will be removed soon due to its redundancy")
val containsMasks: Boolean
get() = this is MaskStickerSet
@SerialName(thumbField) @SerialName(thumbField)
val thumb: PhotoSize? = null val thumb: PhotoSize?
) }
@Serializable
data class RegularStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<RegularSticker>,
@SerialName(isAnimatedField)
override val isAnimated: Boolean = false,
@SerialName(isVideoField)
override val isVideo: Boolean = false,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : StickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Regular
}
@Serializable
data class MaskStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<MaskSticker>,
@SerialName(isAnimatedField)
override val isAnimated: Boolean = false,
@SerialName(isVideoField)
override val isVideo: Boolean = false,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : StickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.Mask
}
@Serializable
data class CustomEmojiStickerSet(
@SerialName(nameField)
override val name: String,
@SerialName(titleField)
override val title: String,
@SerialName(stickersField)
override val stickers: List<CustomEmojiSticker>,
@SerialName(isAnimatedField)
override val isAnimated: Boolean = false,
@SerialName(isVideoField)
override val isVideo: Boolean = false,
@SerialName(thumbField)
override val thumb: PhotoSize? = null
) : StickerSet {
@SerialName(stickerTypeField)
@EncodeDefault
override val stickerType: StickerType = StickerType.CustomEmoji
}