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

RawChat boilerplate was removed and replaced by serializers

This commit is contained in:
InsanusMokrassar 2019-08-12 15:41:07 +06:00
parent d6bb14e342
commit 1f62c8cf98
17 changed files with 168 additions and 138 deletions

View File

@ -23,6 +23,7 @@ was replaced into `abstracts` package and available permissions was updated
Other important changes:
* Totally reworked chats hierarchy. `Extended` abstractions was added for cases when called `GetChat` request
* `RawChat` boilerplate was removed and replaced by serializers
* `RequestsExecutor` now is `Closeable`
* `TelegramAPIUrlsKeeper` was added to provide more comfortable work with file urls and other things
like this

View File

@ -1,15 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.RawChat
sealed class HandleException (
message: String
) : IllegalArgumentException(
message
)
class IllegalChatRawObjectException(
rawChat: RawChat
) : HandleException(
"One of the fields in raw chat object is incorrect"
)

View File

@ -3,7 +3,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.chat.get
import com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts.types.ChatRequest
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.RawChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ExtendedChatSerializer
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chatIdField
import kotlinx.serialization.*
@ -11,7 +12,7 @@ import kotlinx.serialization.*
data class GetChat(
@SerialName(chatIdField)
override val chatId: ChatIdentifier
): ChatRequest, SimpleRequest<RawChat> {
): ChatRequest, SimpleRequest<ExtendedChat> {
override fun method(): String = "getChat"
override fun resultSerializer(): KSerializer<RawChat> = RawChat.serializer()
override fun resultSerializer(): KSerializer<ExtendedChat> = ExtendedChatSerializer
}

View File

@ -56,6 +56,7 @@ const val lastNameField = "last_name"
const val languageCodeField = "language_code"
const val textEntitiesField = "text_entities"
const val stickerSetNameField = "set_name"
const val stickerSetNameFullField = "sticker_set_name"
const val maskPositionField = "mask_position"
const val phoneNumberField = "phone_number"
const val userIdField = "user_id"
@ -91,6 +92,8 @@ const val botUsernameField = "bot_username"
const val switchInlineQueryCurrentChatField = "switch_inline_query_current_chat"
const val switchInlineQueryField = "switch_inline_query"
const val isAnimatedField = "is_animated"
const val inviteLinkField = "invite_link"
const val pinnedMessageField = "pinned_message"
const val requestWriteAccessField = "request_write_access"
@ -149,6 +152,7 @@ const val canSendMediaMessagesField = "can_send_media_messages"
const val canSendOtherMessagesField = "can_send_other_messages"
const val canSendPollsField = "can_send_polls"
const val canAddWebPagePreviewsField = "can_add_web_page_previews"
const val canSetStickerSetField = "can_set_sticker_set"
const val canBeEditedField = "can_be_edited"
const val canChangeInfoField = "can_change_info"
@ -205,6 +209,7 @@ const val questionField = "question"
const val optionsField = "options"
const val payField = "pay"
const val permissionsField = "permissions"
const val typeField = "type"
const val pointField = "point"
const val xShiftField = "x_shift"

View File

@ -4,9 +4,15 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PublicChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class ChannelChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(titleField)
override val title: String,
@SerialName(usernameField)
override val username: Username? = null
) : ChannelChat

View File

@ -0,0 +1,68 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.*
import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObjectSerializer
object PreviewChatSerializer : KSerializer<Chat> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("PreviewChatSerializer")
override fun deserialize(decoder: Decoder): Chat {
val decodedJson = JsonObjectSerializer.deserialize(decoder)
val type = decodedJson.getPrimitive(typeField).content
return when (type) {
"private" -> Json.nonstrict.fromJson(PrivateChatImpl.serializer(), decodedJson)
"group" -> Json.nonstrict.fromJson(GroupChatImpl.serializer(), decodedJson)
"supergroup" -> Json.nonstrict.fromJson(SupergroupChatImpl.serializer(), decodedJson)
"channel" -> Json.nonstrict.fromJson(ChannelChatImpl.serializer(), decodedJson)
else -> throw IllegalArgumentException("Unknown type of chat")
}
}
override fun serialize(encoder: Encoder, obj: Chat) {
when (obj) {
is ExtendedChat -> ExtendedChatSerializer.serialize(encoder, obj)
is PrivateChatImpl -> PrivateChatImpl.serializer().serialize(encoder, obj)
is GroupChatImpl -> GroupChatImpl.serializer().serialize(encoder, obj)
is SupergroupChatImpl -> SupergroupChatImpl.serializer().serialize(encoder, obj)
is ChannelChatImpl -> ChannelChatImpl.serializer().serialize(encoder, obj)
}
}
}
object ExtendedChatSerializer : KSerializer<ExtendedChat> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("PreviewChatSerializer")
override fun deserialize(decoder: Decoder): ExtendedChat {
val decodedJson = JsonObjectSerializer.deserialize(decoder)
val type = decodedJson.getPrimitive(typeField).content
return when (type) {
"private" -> Json.nonstrict.fromJson(ExtendedPrivateChatImpl.serializer(), decodedJson)
"group" -> Json.nonstrict.fromJson(ExtendedGroupChatImpl.serializer(), decodedJson)
"supergroup" -> Json.nonstrict.fromJson(ExtendedSupergroupChatImpl.serializer(), decodedJson)
"channel" -> Json.nonstrict.fromJson(ExtendedChannelChatImpl.serializer(), decodedJson)
else -> throw IllegalArgumentException("Unknown type of chat")
}
}
override fun serialize(encoder: Encoder, obj: ExtendedChat) {
when (obj) {
is ExtendedPrivateChatImpl -> ExtendedPrivateChatImpl.serializer().serialize(encoder, obj)
is ExtendedGroupChatImpl -> ExtendedGroupChatImpl.serializer().serialize(encoder, obj)
is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, obj)
is ExtendedChannelChatImpl -> ExtendedChannelChatImpl.serializer().serialize(encoder, obj)
}
}
}

View File

@ -1,11 +1,15 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatPhoto
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class GroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(titleField)
override val title: String
) : GroupChat

View File

@ -2,10 +2,17 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class PrivateChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(usernameField)
override val username: Username? = null,
@SerialName(firstNameField)
override val firstName: String = "",
@SerialName(lastNameField)
override val lastName: String = ""
) : PrivateChat

View File

@ -1,97 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.IllegalChatRawObjectException
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class RawChat(
override val id: ChatId,
private val type: String,
private val title: String? = null,
private val username: Username? = null,
private val first_name: String? = null,
private val last_name: String? = null,
private val description: String? = null,
private val invite_link: String? = null,
private val pinned_message: RawMessage? = null,
private val sticker_set_name: String? = null,
private val can_set_sticker_set: Boolean? = null,
@SerialName("photo")
private val chatPhoto: ChatPhoto? = null,
private val permissions: ChatPermissions? = null
) : Chat {
private fun extractExtendedChat(): ExtendedChat {
return when (type) {
"private" -> ExtendedPrivateChatImpl(id, username, first_name ?: "", last_name ?: "", chatPhoto!!)
"group" -> ExtendedGroupChatImpl(
id,
title!!,
chatPhoto!!,
description ?: "",
invite_link,
permissions!!,
pinned_message
)
"supergroup" -> ExtendedSupergroupChatImpl(
id,
title!!,
username,
chatPhoto!!,
description ?: "",
invite_link,
permissions!!,
pinned_message,
sticker_set_name,
can_set_sticker_set ?: false
)
"channel" -> ExtendedChannelChatImpl(
id,
title!!,
username,
chatPhoto!!,
description ?: "",
invite_link,
pinned_message
)
else -> throw IllegalArgumentException("Unknown type of chat")
}
}
private fun extractPreviewChat(): Chat {
return when (type) {
"private" -> PrivateChatImpl(id, username, first_name ?: "", last_name ?: "")
"group" -> GroupChatImpl(
id,
title!!
)
"supergroup" -> SupergroupChatImpl(
id,
title!!,
username
)
"channel" -> ChannelChatImpl(
id,
title!!,
username
)
else -> throw IllegalArgumentException("Unknown type of chat")
}
}
fun extractChat(): Chat {
return try {
when (chatPhoto) {
null -> extractPreviewChat()
else -> extractExtendedChat()
}
} catch (e: NullPointerException) {
throw IllegalChatRawObjectException(this)
}
}
}

View File

@ -5,9 +5,15 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.SupergroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class SupergroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(titleField)
override val title: String,
@SerialName(usernameField)
override val username: Username? = null
) : SupergroupChat

View File

@ -2,7 +2,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatPhoto
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.PreviewChatSerializer
import kotlinx.serialization.Serializable
@Serializable(PreviewChatSerializer::class)
interface Chat {
val id: ChatId
}

View File

@ -1,8 +1,11 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatPhoto
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ExtendedChatSerializer
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import kotlinx.serialization.Serializable
@Serializable(ExtendedChatSerializer::class)
interface ExtendedChat : Chat {
val chatPhoto: ChatPhoto
}

View File

@ -3,13 +3,23 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChannelChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class ExtendedChannelChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(titleField)
override val title: String,
override val username: Username?,
@SerialName(usernameField)
override val username: Username? = null,
@SerialName(photoField)
override val chatPhoto: ChatPhoto,
override val description: String,
override val inviteLink: String?,
override val pinnedMessage: RawMessage?
@SerialName(descriptionField)
override val description: String = "",
@SerialName(inviteLinkField)
override val inviteLink: String? = null,
@SerialName(pinnedMessageField)
override val pinnedMessage: RawMessage? = null
) : ExtendedChannelChat

View File

@ -1,17 +1,26 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatPhoto
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChatPermissions
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedGroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class ExtendedGroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(titleField)
override val title: String,
@SerialName(photoField)
override val chatPhoto: ChatPhoto,
override val description: String,
override val inviteLink: String?,
@SerialName(permissionsField)
override val permissions: ChatPermissions,
override val pinnedMessage: RawMessage?
@SerialName(descriptionField)
override val description: String = "",
@SerialName(inviteLinkField)
override val inviteLink: String? = null,
@SerialName(pinnedMessageField)
override val pinnedMessage: RawMessage? = null
) : ExtendedGroupChat

View File

@ -2,11 +2,19 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedPrivateChat
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class ExtendedPrivateChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(photoField)
override val chatPhoto: ChatPhoto,
@SerialName(usernameField)
override val username: Username? = null,
@SerialName(firstNameField)
override val firstName: String = "",
override val lastName: String = "",
override val chatPhoto: ChatPhoto
@SerialName(lastNameField)
override val lastName: String = ""
) : ExtendedPrivateChat

View File

@ -4,16 +4,29 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.ChatPermissions
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedSupergroupChat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.RawMessage
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class ExtendedSupergroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@SerialName(titleField)
override val title: String,
@SerialName(usernameField)
override val username: Username? = null,
@SerialName(photoField)
override val chatPhoto: ChatPhoto,
override val description: String,
override val inviteLink: String?,
@SerialName(permissionsField)
override val permissions: ChatPermissions,
override val pinnedMessage: RawMessage?,
override val stickerSetName: StickerSetName?,
override val canSetStickerSet: Boolean
@SerialName(descriptionField)
override val description: String = "",
@SerialName(inviteLinkField)
override val inviteLink: String? = null,
@SerialName(pinnedMessageField)
override val pinnedMessage: RawMessage? = null,
@SerialName(stickerSetNameFullField)
override val stickerSetName: StickerSetName? = null,
@SerialName(canSetStickerSetField)
override val canSetStickerSet: Boolean = false
) : ExtendedSupergroupChat

View File

@ -31,11 +31,11 @@ data class RawMessage(
@SerialName(dateField)
val date: TelegramDate,
@SerialName(chatField)
private val chat: RawChat,
private val chat: Chat,
@SerialName(fromField)
private val from: User? = null,
private val forward_from: User? = null,
private val forward_from_chat: RawChat? = null,
private val forward_from_chat: Chat? = null,
private val forward_from_message_id: MessageIdentifier? = null,
private val forward_signature: ForwardSignature? = null,
private val forward_sender_name: ForwardSenderName? = null,
@ -150,7 +150,7 @@ data class RawMessage(
forward_from_chat != null -> ForwardedFromChannelMessage(
forward_date,
forward_from_message_id ?: throw IllegalStateException("Channel forwarded message must contain message id, but was not"),
forward_from_chat.extractChat(),
forward_from_chat,
forward_signature
)
forward_from != null -> UserForwardedMessage(
@ -194,8 +194,6 @@ data class RawMessage(
@Transient
val asMessage: Message by lazy {
val chat = chat.extractChat()
chatEvent ?.let {
chatEvent ->
when (chat) {