From ed87c0ad95836d163ed67cdad3bb9043c96cd5ac Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Nov 2020 11:19:53 +0600 Subject: [PATCH 1/4] start 0.30.5 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd46ba7a2d..f3b67a44ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.30.5 + ## 0.30.4 * `Common`: diff --git a/gradle.properties b/gradle.properties index 59e7102e0a..3bce1e13dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ micro_utils_version=0.3.3 javax_activation_version=1.1.1 library_group=dev.inmo -library_version=0.30.4 +library_version=0.30.5 gradle_bintray_plugin_version=1.8.5 github_release_plugin_version=2.2.12 From 3b08bc6dd294b61abc60efe29cf8c1595d6303c1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Nov 2020 14:38:54 +0600 Subject: [PATCH 2/4] serialization of ChatMember and changing of id type in User --- CHANGELOG.md | 6 ++ .../chat/get/GetChatAdministrators.kt | 4 +- .../requests/chat/members/GetChatMember.kt | 4 +- .../ChatMember/AdministratorChatMemberImpl.kt | 44 +++++++--- .../types/ChatMember/CreatorChatMember.kt | 22 ++++- .../types/ChatMember/KickedChatMember.kt | 15 +++- .../types/ChatMember/LeftChatMember.kt | 11 ++- .../types/ChatMember/MemberChatMember.kt | 11 ++- .../types/ChatMember/RawChatMember.kt | 87 ------------------- .../types/ChatMember/RestrictedChatMember.kt | 42 ++++++--- .../abstracts/AdministratorChatMember.kt | 17 +++- .../ChatMember/abstracts/BannedChatMember.kt | 3 + .../types/ChatMember/abstracts/ChatMember.kt | 47 +++++++--- .../abstracts/SpecialRightsChatMember.kt | 4 + .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 1 + .../kotlin/dev/inmo/tgbotapi/types/User.kt | 10 ++- 16 files changed, 179 insertions(+), 149 deletions(-) delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RawChatMember.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index f3b67a44ac..7400dfcbce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.30.5 +* `Core`: + * Mechanism of `ChatMember` serialization has been changed + * Since this version any `ChatMember` can be serialized (even outside in case it marked by `@Serializable`) + * Since this version any `ChatMember` (included in this project) can be deserialized in common way + * `User` property `id` has changed its type: now it is `UserId` (under the hood it is the same as `ChatId`) + ## 0.30.4 * `Common`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/get/GetChatAdministrators.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/get/GetChatAdministrators.kt index 413e71d424..4ab2d546fc 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/get/GetChatAdministrators.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/get/GetChatAdministrators.kt @@ -4,13 +4,13 @@ import dev.inmo.tgbotapi.CommonAbstracts.types.ChatRequest import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember -import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMemberSerializerWithoutDeserialization +import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMemberSerializer import dev.inmo.tgbotapi.types.chatIdField import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer private val chatMembersListSerializer = ListSerializer( - AdministratorChatMemberSerializerWithoutDeserialization + AdministratorChatMemberSerializer ) @Serializable diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt index 1a6ab610ca..efde1577c3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt @@ -2,8 +2,8 @@ package dev.inmo.tgbotapi.requests.chat.members import dev.inmo.tgbotapi.requests.chat.abstracts.ChatMemberRequest import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMember -import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMemberDeserializationStrategy import kotlinx.serialization.* @Serializable @@ -15,7 +15,7 @@ data class GetChatMember( ) : ChatMemberRequest { override fun method(): String = "getChatMember" override val resultDeserializer: DeserializationStrategy - get() = ChatMemberDeserializationStrategy + get() = ChatMemberSerializer override val requestSerializer: SerializationStrategy<*> get() = serializer() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt index 3b734ef904..dd16ba76de 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt @@ -1,19 +1,37 @@ package dev.inmo.tgbotapi.types.ChatMember +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember -import dev.inmo.tgbotapi.types.User +import kotlinx.serialization.* +@Serializable data class AdministratorChatMemberImpl( + @SerialName(userField) override val user: User, - override val canBeEdited: Boolean, - override val canChangeInfo: Boolean, - override val canPostMessages: Boolean, - override val canEditMessages: Boolean, - override val canRemoveMessages: Boolean, - override val canInviteUsers: Boolean, - override val canRestrictMembers: Boolean, - override val canPinMessages: Boolean, - override val canPromoteMembers: Boolean, - override val isAnonymous: Boolean, - override val customTitle: String? -) : AdministratorChatMember + @SerialName(canBeEditedField) + override val canBeEdited: Boolean = false, + @SerialName(canChangeInfoField) + override val canChangeInfo: Boolean = false, + @SerialName(canPostMessagesField) + override val canPostMessages: Boolean = false, + @SerialName(canEditMessagesField) + override val canEditMessages: Boolean = false, + @SerialName(canDeleteMessagesField) + override val canRemoveMessages: Boolean = false, + @SerialName(canInviteUsersField) + override val canInviteUsers: Boolean = false, + @SerialName(canRestrictMembersField) + override val canRestrictMembers: Boolean = false, + @SerialName(canPinMessagesField) + override val canPinMessages: Boolean = false, + @SerialName(canPromoteMembersField) + override val canPromoteMembers: Boolean = false, + @SerialName(isAnonymousField) + override val isAnonymous: Boolean = false, + @SerialName(customTitleField) + override val customTitle: String? = null +) : AdministratorChatMember { + @SerialName(statusField) + @Required + private val type: String = "administrator" +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt index af981b2312..014ac9c952 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt @@ -1,20 +1,36 @@ package dev.inmo.tgbotapi.types.ChatMember +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember -import dev.inmo.tgbotapi.types.User +import kotlinx.serialization.* +@Serializable data class CreatorChatMember( override val user: User, - override val isAnonymous: Boolean, - override val customTitle: String? + @SerialName(isAnonymousField) + override val isAnonymous: Boolean = false, + @SerialName(customTitleField) + override val customTitle: String? = null ) : AdministratorChatMember { + @Transient override val canBeEdited: Boolean = true + @Transient override val canChangeInfo: Boolean = true + @Transient override val canPostMessages: Boolean = true + @Transient override val canEditMessages: Boolean = true + @Transient override val canRemoveMessages: Boolean = true + @Transient override val canInviteUsers: Boolean = true + @Transient override val canRestrictMembers: Boolean = true + @Transient override val canPinMessages: Boolean = true + @Transient override val canPromoteMembers: Boolean = true + @SerialName(statusField) + @Required + private val type: String = "creator" } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/KickedChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/KickedChatMember.kt index 4c74996a96..df8b87734c 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/KickedChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/KickedChatMember.kt @@ -1,10 +1,17 @@ package dev.inmo.tgbotapi.types.ChatMember +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.BannedChatMember -import dev.inmo.tgbotapi.types.TelegramDate -import dev.inmo.tgbotapi.types.User +import kotlinx.serialization.* +@Serializable data class KickedChatMember( + @SerialName(userField) override val user: User, - override val untilDate: TelegramDate? -) : BannedChatMember \ No newline at end of file + @SerialName(untilDateField) + override val untilDate: TelegramDate? = null +) : BannedChatMember { + @SerialName(statusField) + @Required + private val type: String = "kicked" +} \ No newline at end of file diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/LeftChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/LeftChatMember.kt index e9e70060d2..762f240f1b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/LeftChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/LeftChatMember.kt @@ -1,7 +1,12 @@ package dev.inmo.tgbotapi.types.ChatMember +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMember -import dev.inmo.tgbotapi.types.User +import kotlinx.serialization.* -data class LeftChatMember(override val user: User) : - ChatMember +@Serializable +data class LeftChatMember(@SerialName(userField) override val user: User) : ChatMember { + @SerialName(statusField) + @Required + private val type: String = "left" +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/MemberChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/MemberChatMember.kt index 03aa9da492..13625f4077 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/MemberChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/MemberChatMember.kt @@ -1,7 +1,12 @@ package dev.inmo.tgbotapi.types.ChatMember +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMember -import dev.inmo.tgbotapi.types.User +import kotlinx.serialization.* -data class MemberChatMember(override val user: User) : - ChatMember +@Serializable +data class MemberChatMember(@SerialName(userField) override val user: User) : ChatMember { + @SerialName(statusField) + @Required + private val type: String = "member" +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RawChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RawChatMember.kt deleted file mode 100644 index 4f8bd51b66..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RawChatMember.kt +++ /dev/null @@ -1,87 +0,0 @@ -package dev.inmo.tgbotapi.types.ChatMember - -import dev.inmo.tgbotapi.types.* -import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMember -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -internal data class RawChatMember( - val user: User, - private val status: String, - private val until_date: TelegramDate? = null, - @SerialName(canBeEditedField) - private val canBeEdited: Boolean = false, - @SerialName(canChangeInfoField) - private val canChangeInfo: Boolean = false, - @SerialName(canPostMessagesField) - private val canPostMessages: Boolean = false, - @SerialName(canEditMessagesField) - private val canEditMessages: Boolean = false, - @SerialName(canDeleteMessagesField) - private val canDeleteMessages: Boolean = false, - @SerialName(canInviteUsersField) - private val canInviteUsers: Boolean = false, - @SerialName(canRestrictMembersField) - private val canRestrictMembers: Boolean = false, - @SerialName(canPinMessagesField) - private val canPinMessages: Boolean = false, - @SerialName(canPromoteMembersField) - private val canPromoteMembers: Boolean = false, - @SerialName(isMemberField) - private val isMember: Boolean = false, - @SerialName(canSendMessagesField) - private val canSendMessages: Boolean = false, - @SerialName(canSendMediaMessagesField) - private val canSendMediaMessages: Boolean = false, - @SerialName(canSendPollsField) - private val canSendPolls: Boolean = false, - @SerialName(canSendOtherMessagesField) - private val canSendOtherMessages: Boolean = false, - @SerialName(canAddWebPagePreviewsField) - private val canAddWebPagePreviews: Boolean = false, - @SerialName(isAnonymousField) - private val isAnonymous: Boolean = false, - @SerialName(customTitleField) - private val customTitle: String? = null -) { - val asChatMember: ChatMember by lazy { - when (status) { - "creator" -> CreatorChatMember(user, isAnonymous, customTitle) - "administrator" -> AdministratorChatMemberImpl( - user, - canBeEdited, - canChangeInfo, - canPostMessages, - canEditMessages, - canDeleteMessages, - canInviteUsers, - canRestrictMembers, - canPinMessages, - canPromoteMembers, - isAnonymous, - customTitle - ) - "member" -> MemberChatMember(user) - "restricted" -> RestrictedChatMember( - user, - until_date, - isMember, - canSendMessages, - canSendMediaMessages, - canSendPolls, - canSendOtherMessages, - canAddWebPagePreviews, - canChangeInfo, - canInviteUsers, - canPinMessages - ) - "left" -> LeftChatMember(user) - "kicked" -> KickedChatMember( - user, - until_date - ) - else -> throw IllegalStateException("Can't understand type of user: $status") - } - } -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RestrictedChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RestrictedChatMember.kt index fcb02d1aff..dab888e2cc 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RestrictedChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/RestrictedChatMember.kt @@ -1,20 +1,36 @@ package dev.inmo.tgbotapi.types.ChatMember +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.BannedChatMember import dev.inmo.tgbotapi.types.ChatMember.abstracts.SpecialRightsChatMember -import dev.inmo.tgbotapi.types.TelegramDate -import dev.inmo.tgbotapi.types.User +import kotlinx.serialization.* +@Serializable data class RestrictedChatMember( + @SerialName(userField) override val user: User, - override val untilDate: TelegramDate?, - val isMember: Boolean, - val canSendMessages: Boolean, - val canSendMediaMessages: Boolean, - val canSendPolls: Boolean, - val canSendOtherMessages: Boolean, - val canAddWebpagePreviews: Boolean, - override val canChangeInfo: Boolean, - override val canInviteUsers: Boolean, - override val canPinMessages: Boolean -) : BannedChatMember, SpecialRightsChatMember \ No newline at end of file + @SerialName(untilDateField) + override val untilDate: TelegramDate? = null, + @SerialName(isMemberField) + val isMember: Boolean = false, + @SerialName(canSendMessagesField) + val canSendMessages: Boolean = false, + @SerialName(canSendMediaMessagesField) + val canSendMediaMessages: Boolean = false, + @SerialName(canSendPollsField) + val canSendPolls: Boolean = false, + @SerialName(canSendOtherMessagesField) + val canSendOtherMessages: Boolean = false, + @SerialName(canAddWebPagePreviewsField) + val canAddWebpagePreviews: Boolean = false, + @SerialName(canChangeInfoField) + override val canChangeInfo: Boolean = false, + @SerialName(canInviteUsersField) + override val canInviteUsers: Boolean = false, + @SerialName(canPinMessagesField) + override val canPinMessages: Boolean = false +) : BannedChatMember, SpecialRightsChatMember { + @SerialName(statusField) + @Required + private val type: String = "restricted" +} \ No newline at end of file diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt index ba3bb2ac00..279d286346 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt @@ -1,5 +1,12 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts +import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer +import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +@Serializable(AdministratorChatMemberSerializer::class) interface AdministratorChatMember : SpecialRightsChatMember { val canBeEdited: Boolean val canPostMessages: Boolean @@ -9,4 +16,12 @@ interface AdministratorChatMember : SpecialRightsChatMember { val canPromoteMembers: Boolean val isAnonymous: Boolean val customTitle: String? -} \ No newline at end of file +} + +@Serializer(AdministratorChatMember::class) +internal object AdministratorChatMemberSerializer : KSerializer { + override val descriptor: SerialDescriptor = ChatMemberSerializer.descriptor + + override fun deserialize(decoder: Decoder): AdministratorChatMember = ChatMemberSerializer.deserialize(decoder) as AdministratorChatMember + override fun serialize(encoder: Encoder, value: AdministratorChatMember) = ChatMemberSerializer.serialize(encoder, value) +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt index 42698eeb89..e13c513f9e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt @@ -1,5 +1,8 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts import dev.inmo.tgbotapi.CommonAbstracts.types.UntilDate +import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer +import kotlinx.serialization.Serializable +@Serializable(ChatMemberSerializer::class) interface BannedChatMember : ChatMember, UntilDate \ No newline at end of file diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt index 21d8324165..8f4b9a88d1 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatMember.kt @@ -1,27 +1,46 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts -import dev.inmo.tgbotapi.types.ChatMember.RawChatMember +import dev.inmo.tgbotapi.types.ChatMember.* import dev.inmo.tgbotapi.types.User -import kotlinx.serialization.DeserializationStrategy -import kotlinx.serialization.KSerializer +import dev.inmo.tgbotapi.types.statusField +import dev.inmo.tgbotapi.utils.nonstrictJsonFormat +import kotlinx.serialization.* import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.jsonPrimitive +@Serializable(ChatMemberSerializer::class) interface ChatMember { val user: User } -internal object AdministratorChatMemberSerializerWithoutDeserialization : KSerializer { - override val descriptor: SerialDescriptor = ChatMemberDeserializationStrategy.descriptor +@Serializer(ChatMember::class) +internal object ChatMemberSerializer : KSerializer { + override val descriptor: SerialDescriptor = JsonObject.serializer().descriptor - override fun deserialize(decoder: Decoder): AdministratorChatMember - = ChatMemberDeserializationStrategy.deserialize(decoder) as AdministratorChatMember - override fun serialize(encoder: Encoder, value: AdministratorChatMember) = throw UnsupportedOperationException() -} - -internal object ChatMemberDeserializationStrategy : DeserializationStrategy { - override val descriptor: SerialDescriptor = RawChatMember.serializer().descriptor - - override fun deserialize(decoder: Decoder): ChatMember = RawChatMember.serializer().deserialize(decoder).asChatMember + override fun deserialize(decoder: Decoder): ChatMember { + val json = JsonObject.serializer().deserialize(decoder) + return when (json[statusField] ?.jsonPrimitive ?.content ?: error("Status field of chat member must be specified, but incoming json contains next: $json")) { + "creator" -> nonstrictJsonFormat.decodeFromJsonElement(CreatorChatMember.serializer(), json) + "administrator" -> nonstrictJsonFormat.decodeFromJsonElement(AdministratorChatMemberImpl.serializer(), json) + "member" -> nonstrictJsonFormat.decodeFromJsonElement(MemberChatMember.serializer(), json) + "restricted" -> nonstrictJsonFormat.decodeFromJsonElement(RestrictedChatMember.serializer(), json) + "left" -> nonstrictJsonFormat.decodeFromJsonElement(LeftChatMember.serializer(), json) + "kicked" -> nonstrictJsonFormat.decodeFromJsonElement(KickedChatMember.serializer(), json) + else -> error("Unknown type of chat member in json: $json") + } + } + + override fun serialize(encoder: Encoder, value: ChatMember) { + when (value) { + is CreatorChatMember -> CreatorChatMember.serializer() + is AdministratorChatMemberImpl -> AdministratorChatMemberImpl.serializer() + is MemberChatMember -> MemberChatMember.serializer() + is RestrictedChatMember -> RestrictedChatMember.serializer() + is LeftChatMember -> LeftChatMember.serializer() + is KickedChatMember -> KickedChatMember.serializer() + } + } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt index e72f7ed255..6bd6ecce14 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt @@ -1,5 +1,9 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts +import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer +import kotlinx.serialization.Serializable + +@Serializable(ChatMemberSerializer::class) interface SpecialRightsChatMember : ChatMember { val canChangeInfo: Boolean val canInviteUsers: Boolean diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 1ededf321e..7ca41d5a20 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -232,6 +232,7 @@ const val canSendPollsField = "can_send_polls" const val canAddWebPagePreviewsField = "can_add_web_page_previews" const val canSetStickerSetField = "can_set_sticker_set" +const val statusField = "status" const val canBeEditedField = "can_be_edited" const val canChangeInfoField = "can_change_info" const val canPostMessagesField = "can_post_messages" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/User.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/User.kt index c89704ee63..f9d1e0ef05 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/User.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/User.kt @@ -10,11 +10,13 @@ import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.* @Serializable(UserSerializer::class) -sealed class User : PrivateChat +sealed class User : PrivateChat { + abstract override val id: UserId +} @Serializable data class CommonUser( - override val id: ChatId, + override val id: UserId, @SerialName(firstNameField) override val firstName: String, @SerialName(lastNameField) @@ -35,7 +37,7 @@ sealed class Bot : User() { @Serializable data class CommonBot( - override val id: ChatId, + override val id: UserId, @SerialName(usernameField) override val username: Username, @SerialName(firstNameField) @@ -49,7 +51,7 @@ data class CommonBot( @Serializable data class ExtendedBot( - override val id: ChatId, + override val id: UserId, @SerialName(usernameField) override val username: Username, @SerialName(firstNameField) From 7c4f034a6ca7217070a1744b6890cacfa0c459c2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Nov 2020 14:41:16 +0600 Subject: [PATCH 3/4] fixes in imports --- .../dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt | 2 +- .../types/ChatMember/abstracts/AdministratorChatMember.kt | 1 - .../tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt | 1 - .../types/ChatMember/abstracts/SpecialRightsChatMember.kt | 1 - 4 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt index efde1577c3..b5a1e58e30 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/GetChatMember.kt @@ -2,8 +2,8 @@ package dev.inmo.tgbotapi.requests.chat.members import dev.inmo.tgbotapi.requests.chat.abstracts.ChatMemberRequest import dev.inmo.tgbotapi.types.* -import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMember +import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMemberSerializer import kotlinx.serialization.* @Serializable diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt index 279d286346..7a7c9d0944 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt @@ -1,6 +1,5 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts -import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer import kotlinx.serialization.* import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt index e13c513f9e..52da711ddf 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/BannedChatMember.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts import dev.inmo.tgbotapi.CommonAbstracts.types.UntilDate -import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer import kotlinx.serialization.Serializable @Serializable(ChatMemberSerializer::class) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt index 6bd6ecce14..796c6fa69b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt @@ -1,6 +1,5 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts -import dev.inmo.tgbotapi.types.ChatMember.ChatMemberSerializer import kotlinx.serialization.Serializable @Serializable(ChatMemberSerializer::class) From b36f80a6d3b73e80f7be09fa053f179eb2566a23 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Nov 2020 19:41:26 +0600 Subject: [PATCH 4/4] update micro_utils --- CHANGELOG.md | 3 +++ gradle.properties | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7400dfcbce..119bb7644c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.30.5 +* `Common`: + * `Version`: + * `MicroUtils`: `0.3.3` -> `0.4.0` * `Core`: * Mechanism of `ChatMember` serialization has been changed * Since this version any `ChatMember` can be serialized (even outside in case it marked by `@Serializable`) diff --git a/gradle.properties b/gradle.properties index 3bce1e13dc..0753fb19a2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ klock_version=1.12.1 uuid_version=0.2.2 ktor_version=1.4.2 -micro_utils_version=0.3.3 +micro_utils_version=0.4.0 javax_activation_version=1.1.1