1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

RawGame added

This commit is contained in:
InsanusMokrassar 2019-08-18 12:31:33 +06:00
parent 44751d94b3
commit 704457715d
4 changed files with 41 additions and 55 deletions

View File

@ -1,6 +1,6 @@
# TelegramBotAPI changelog # TelegramBotAPI changelog
## 0.18.0 ## 0.18.0 Raws cleaning
* Made internal and not available outside of library: * Made internal and not available outside of library:
* `RawMessage` * `RawMessage`
@ -17,8 +17,9 @@
* `TelegramBotAPIMessageDeserializeOnlySerializer` was created. It **MUST NOT** be used to serialize messages * `TelegramBotAPIMessageDeserializeOnlySerializer` was created. It **MUST NOT** be used to serialize messages
* Update of description * Update of description
* Make `Game` object a little bit more standartizated * Make `Game` object a little bit more standartizated
* `Game` now is not serializable and have no additional trash, related to serialization
## 0.17.0 ## 0.17.0 July 29, 2019 API Update
Libraries updates: Libraries updates:

View File

@ -9,23 +9,14 @@ import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
import java.lang.UnsupportedOperationException import java.lang.UnsupportedOperationException
@Serializable(GameSerializer::class)
data class Game( data class Game(
@SerialName(titleField)
override val title: String, override val title: String,
@SerialName(descriptionField)
val description: String, val description: String,
@Serializable(PhotoSerializer::class)
@SerialName(photoField)
val photo: Photo, val photo: Photo,
@SerialName(textField)
override val caption: String? = null, override val caption: String? = null,
@SerialName(textEntitiesField)
override val captionEntities: List<MessageEntity> = emptyList(), override val captionEntities: List<MessageEntity> = emptyList(),
@SerialName(animationField)
val animation: AnimationFile? = null val animation: AnimationFile? = null
) : Titled, CaptionedInput { ) : Titled, CaptionedInput {
@Deprecated( @Deprecated(
"Missinterfaced field", "Missinterfaced field",
ReplaceWith("caption") ReplaceWith("caption")
@ -42,43 +33,3 @@ data class Game(
val textEntities: List<MessageEntity>? val textEntities: List<MessageEntity>?
get() = captionEntities get() = captionEntities
} }
internal object GameSerializer : KSerializer<Game> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("GameSerializer")
override fun deserialize(decoder: Decoder): Game {
val json = JsonObjectSerializer.deserialize(decoder)
val text = json.getPrimitiveOrNull(textField) ?.content
return Game(
json.getPrimitive(titleField).content,
json.getPrimitive(descriptionField).content,
Json.nonstrict.fromJson(
PhotoSerializer,
json.getObject(photoField)
),
text,
text ?.let {
Json.nonstrict.fromJson(
RawMessageEntitiesSerializer,
json.getArray(textEntitiesField)
).map {
it.asMessageEntity(text)
}
} ?: emptyList(),
json.getObjectOrNull(
animationField
) ?.let { animatedFileJson ->
Json.nonstrict.fromJson(
AnimationFile.serializer(),
animatedFileJson
)
}
)
}
override fun serialize(encoder: Encoder, obj: Game) = throw UnsupportedOperationException(
"Objects of class Game can't be serialized for now"
)
}

View File

@ -0,0 +1,35 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.games
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.*
import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
internal data class RawGame(
@SerialName(titleField)
private val title: String,
@SerialName(descriptionField)
private val description: String,
@Serializable(PhotoSerializer::class)
@SerialName(photoField)
private val photo: Photo,
@SerialName(textField)
private val caption: String? = null,
@SerialName(textEntitiesField)
private val captionEntities: RawMessageEntities = emptyList(),
@SerialName(animationField)
private val animation: AnimationFile? = null
) {
@Transient
val asGame = Game(
title,
description,
photo,
caption,
caption ?.let { _ -> captionEntities.map { it.asMessageEntity(caption) } } ?: emptyList(),
animation
)
}

View File

@ -7,6 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMa
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.files.* import com.github.insanusmokrassar.TelegramBotAPI.types.files.*
import com.github.insanusmokrassar.TelegramBotAPI.types.games.Game import com.github.insanusmokrassar.TelegramBotAPI.types.games.Game
import com.github.insanusmokrassar.TelegramBotAPI.types.games.RawGame
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
@ -44,15 +45,13 @@ internal data class RawMessage(
private val media_group_id: MediaGroupIdentifier? = null, private val media_group_id: MediaGroupIdentifier? = null,
private val author_signature: AuthorSignature? = null, private val author_signature: AuthorSignature? = null,
private val text: String? = null, private val text: String? = null,
@Serializable(RawMessageEntitiesSerializer::class)
private val entities: RawMessageEntities? = null, private val entities: RawMessageEntities? = null,
private val caption: String? = null, private val caption: String? = null,
@Serializable(RawMessageEntitiesSerializer::class)
private val caption_entities: RawMessageEntities? = null, private val caption_entities: RawMessageEntities? = null,
private val audio: AudioFile? = null, private val audio: AudioFile? = null,
private val document: DocumentFile? = null, private val document: DocumentFile? = null,
private val animation: AnimationFile? = null, private val animation: AnimationFile? = null,
private val game: Game? = null, private val game: RawGame? = null,
@Serializable(PhotoSerializer::class) @Serializable(PhotoSerializer::class)
private val photo: Photo? = null, private val photo: Photo? = null,
private val sticker: Sticker? = null, private val sticker: Sticker? = null,
@ -128,7 +127,7 @@ internal data class RawMessage(
adaptedCaptionEntities adaptedCaptionEntities
) )
sticker != null -> StickerContent(sticker) sticker != null -> StickerContent(sticker)
game != null -> GameContent(game) game != null -> GameContent(game.asGame)
video_note != null -> VideoNoteContent(video_note) video_note != null -> VideoNoteContent(video_note)
contact != null -> ContactContent(contact) contact != null -> ContactContent(contact)
location != null -> LocationContent(location) location != null -> LocationContent(location)