From d5f5a0e30b5658844a6ef8e2cac21054bc514e6c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 11 Oct 2023 12:10:36 +0600 Subject: [PATCH 1/3] start 9.2.2 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aceca33db..de2d4a91a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 9.2.2 + ## 9.2.1 * `Version`: diff --git a/gradle.properties b/gradle.properties index 83b46a74a2..038e59522d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,4 +6,4 @@ kotlin.incremental=true kotlin.incremental.js=true library_group=dev.inmo -library_version=9.2.1 +library_version=9.2.2 From 80be86454d6b3b88f16abe62184ac8a4ebb1243d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 11 Oct 2023 13:07:07 +0600 Subject: [PATCH 2/3] fix of #793 --- CHANGELOG.md | 3 + .../dev/inmo/tgbotapi/abstracts/WithChat.kt | 11 +- .../tgbotapi/types/chat/ChatSerializers.kt | 44 ++++++- .../dev/inmo/tgbotapi/types/chat/Impls.kt | 20 ++-- .../tgbotapi/types/chat/PreviewAbstracts.kt | 30 +++++ .../tgbotapi/types/chat/UnknownChatType.kt | 2 +- .../types/chat/member/ChatMemberUpdated.kt | 7 +- .../message/ChannelContentMessageImpl.kt | 3 +- .../types/message/ChannelEventMessage.kt | 3 +- .../types/message/CommonGroupEventMessage.kt | 3 +- .../message/CommonSupergroupEventMessage.kt | 3 +- .../tgbotapi/types/message/GroupMessages.kt | 20 ++-- .../tgbotapi/types/message/PassportMessage.kt | 3 +- .../message/PrivateContentMessageImpl.kt | 2 +- .../types/message/PrivateEventMessage.kt | 3 +- .../inmo/tgbotapi/types/message/RawMessage.kt | 33 +++--- .../abstracts/ChannelContentMessage.kt | 5 +- .../message/abstracts/GroupEventMessage.kt | 6 +- .../types/message/abstracts/GroupMessages.kt | 16 ++- .../types/message/abstracts/Message.kt | 7 +- .../types/message/abstracts/PrivateMessage.kt | 6 +- .../types/message/abstracts/PublicMessage.kt | 3 +- .../abstracts/SupergroupEventMessage.kt | 6 +- .../abstracts/WithSenderChatMessage.kt | 3 +- .../extensions/utils/ClassCastsNew.kt | 110 ++++++++++++++++++ .../extensions/utils/extensions/FlowsSame.kt | 6 +- .../extensions/utils/extensions/Same.kt | 14 ++- 27 files changed, 296 insertions(+), 76 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/PreviewAbstracts.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index de2d4a91a1..74424cfcaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 9.2.2 +* `Core`: + * Fix of [#793](https://github.com/InsanusMokrassar/ktgbotapi/issues/793): Add `PreviewChat` + ## 9.2.1 * `Version`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/WithChat.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/WithChat.kt index 1cdba4b9f3..6cd966d0f5 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/WithChat.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/abstracts/WithChat.kt @@ -1,10 +1,17 @@ package dev.inmo.tgbotapi.abstracts import dev.inmo.tgbotapi.types.chat.Chat +import dev.inmo.tgbotapi.types.chat.PreviewChat /** * All inheritors of this interface have [chat] field and related to this [chat] */ -interface WithChat { - val chat: Chat +interface WithPreviewChat { + val chat: PreviewChat } + +/** + * All inheritors of this interface have [chat] field and related to this [chat] + */ +@Deprecated("Renamed", ReplaceWith("WithPreviewChat", "dev.inmo.tgbotapi.abstracts.WithPreviewChat")) +typealias WithChat = WithPreviewChat diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt index 2d2c175153..989979c3e8 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt @@ -49,7 +49,7 @@ object ChatTypeSerializer : KSerializer { } @RiskFeature -object PreviewChatSerializer : KSerializer { +object ChatSerializer : KSerializer { @OptIn(InternalSerializationApi::class) override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN) @@ -92,6 +92,48 @@ object PreviewChatSerializer : KSerializer { } } +@RiskFeature +object PreviewChatSerializer : KSerializer { + @OptIn(InternalSerializationApi::class) + override val descriptor: SerialDescriptor = buildSerialDescriptor("PreviewChatSerializer", PolymorphicKind.OPEN) + + override fun deserialize(decoder: Decoder): PreviewChat { + val decodedJson = JsonObject.serializer().deserialize(decoder) + + val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson") + val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true + + return when (type) { + ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) + ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) + ChatType.SupergroupChatType -> if (isForum) { + formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) + } else { + formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) + } + ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) + is ChatType.UnknownChatType -> UnknownChatType( + formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), + decodedJson.toString(), + decodedJson + ) + } + } + + override fun serialize(encoder: Encoder, value: PreviewChat) { + when (value) { + is PrivateChatImpl -> PrivateChatImpl.serializer().serialize(encoder, value) + is GroupChatImpl -> GroupChatImpl.serializer().serialize(encoder, value) + is SupergroupChatImpl -> SupergroupChatImpl.serializer().serialize(encoder, value) + is ForumChatImpl -> ForumChatImpl.serializer().serialize(encoder, value) + is ChannelChatImpl -> ChannelChatImpl.serializer().serialize(encoder, value) + is CommonBot -> CommonBot.serializer().serialize(encoder, value) + is CommonUser -> CommonUser.serializer().serialize(encoder, value) + is UnknownChatType -> JsonObject.serializer().serialize(encoder, value.rawJson) + } + } +} + @RiskFeature sealed class ExtendedChatSerializer : KSerializer { @OptIn(InternalSerializationApi::class) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt index 660fe4c554..7588c3f55d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Impls.kt @@ -15,7 +15,7 @@ data class GroupChatImpl( override val id: ChatId, @SerialName(titleField) override val title: String -) : GroupChat +) : PreviewGroupChat @Serializable @RiskFeature("This class is a subject of changes. It is better to use PrivateChat due") @@ -28,7 +28,7 @@ data class PrivateChatImpl( override val firstName: String = "", @SerialName(lastNameField) override val lastName: String = "" -) : PrivateChat +) : PreviewPrivateChat @Serializable @RiskFeature("This class is a subject of changes. It is better to use SupergroupChat due") @@ -39,7 +39,7 @@ data class SupergroupChatImpl( override val title: String, @SerialName(usernameField) override val username: Username? = null -) : SupergroupChat +) : PreviewSupergroupChat @Serializable @RiskFeature("This class is a subject of changes. It is better to use ForumChat due") @@ -50,7 +50,7 @@ data class ForumChatImpl( override val title: String, @SerialName(usernameField) override val username: Username? = null -) : ForumChat +) : PreviewForumChat @Serializable @RiskFeature("This class is a subject of changes. It is better to use ChannelChat due") @@ -61,14 +61,20 @@ data class ChannelChatImpl( override val title: String, @SerialName(usernameField) override val username: Username? = null -) : ChannelChat +) : PreviewChannelChat @Serializable(UserSerializer::class) sealed class User : PrivateChat +@Serializable(UserSerializer::class) +sealed class PreviewUser : PreviewPrivateChat, User() + @Serializable(UserSerializer::class) sealed class Bot : User() +@Serializable(UserSerializer::class) +sealed class PreviewBot : PreviewUser() + @Serializable data class CommonBot( override val id: UserId, @@ -78,7 +84,7 @@ data class CommonBot( override val lastName: String = "", @SerialName(usernameField) override val username: Username? = null, -) : Bot() { +) : PreviewBot() { @SerialName(isBotField) private val isBot = true } @@ -99,7 +105,7 @@ data class CommonUser( override val isPremium: Boolean = false, @SerialName(addedToAttachmentMenuField) override val addedToAttachmentMenu: Boolean = false -) : User(), WithOptionalLanguageCode, PossiblyPremiumChat, AbleToAddInAttachmentMenuChat { +) : PreviewUser(), WithOptionalLanguageCode, PossiblyPremiumChat, AbleToAddInAttachmentMenuChat { constructor( id: UserId, firstName: String, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/PreviewAbstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/PreviewAbstracts.kt new file mode 100644 index 0000000000..c7f89db815 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/PreviewAbstracts.kt @@ -0,0 +1,30 @@ +package dev.inmo.tgbotapi.types.chat + +import kotlinx.serialization.Serializable + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewChat : Chat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewUsernameChat : PreviewChat, UsernameChat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewPrivateChat : PreviewUsernameChat, PrivateChat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewPublicChat : PreviewChat, PublicChat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewSuperPublicChat : PreviewPublicChat, PreviewUsernameChat, SuperPublicChat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewChannelChat : PreviewSuperPublicChat, ChannelChat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewGroupChat : PreviewPublicChat, GroupChat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewSupergroupChat : PreviewGroupChat, PreviewSuperPublicChat, SupergroupChat + +@Serializable(PreviewChatSerializer::class) +sealed interface PreviewForumChat : PreviewSupergroupChat, ForumChat diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt index 87001ee2ca..fd66ac9729 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/UnknownChatType.kt @@ -7,4 +7,4 @@ data class UnknownChatType( override val id: IdChatIdentifier, val raw: String, val rawJson: JsonObject -) : Chat +) : Chat, PreviewChat diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMemberUpdated.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMemberUpdated.kt index 8539a0c76e..2f3166bdf9 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMemberUpdated.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMemberUpdated.kt @@ -1,9 +1,10 @@ package dev.inmo.tgbotapi.types.chat.member -import dev.inmo.tgbotapi.abstracts.WithChat +import dev.inmo.tgbotapi.abstracts.WithPreviewChat import dev.inmo.tgbotapi.abstracts.WithUser import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.Chat +import dev.inmo.tgbotapi.types.chat.PreviewChat import dev.inmo.tgbotapi.types.chat.User import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -11,7 +12,7 @@ import kotlinx.serialization.Serializable @Serializable data class ChatMemberUpdated( @SerialName(chatField) - override val chat: Chat, + override val chat: PreviewChat, @SerialName(fromField) override val user: User, @SerialName(dateField) @@ -24,4 +25,4 @@ data class ChatMemberUpdated( val inviteLink: ChatInviteLink? = null, @SerialName(viaChatFolderInviteLinkField) val viaChatFolderInviteLink: Boolean? = false -) : WithChat, WithUser +) : WithPreviewChat, WithUser diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt index f3bd065547..e3e5f0dcb4 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelContentMessageImpl.kt @@ -5,12 +5,13 @@ import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.chat.ChannelChat import dev.inmo.tgbotapi.types.chat.CommonBot +import dev.inmo.tgbotapi.types.chat.PreviewChannelChat import dev.inmo.tgbotapi.types.message.abstracts.* import dev.inmo.tgbotapi.types.message.content.MessageContent data class ChannelContentMessageImpl( override val messageId: MessageId, - override val chat: ChannelChat, + override val chat: PreviewChannelChat, override val content: T, override val date: DateTime, override val editDate: DateTime?, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt index bcd88ccb31..09c2f456d0 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt @@ -3,12 +3,13 @@ package dev.inmo.tgbotapi.types.message import korlibs.time.DateTime import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.chat.ChannelChat +import dev.inmo.tgbotapi.types.chat.PreviewChannelChat import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ChannelEvent import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage data class ChannelEventMessage( override val messageId: MessageId, - override val chat: ChannelChat, + override val chat: PreviewChannelChat, override val chatEvent: T, override val date: DateTime ) : ChatEventMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt index cedb9f92c4..4286ee4e26 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt @@ -4,13 +4,14 @@ import korlibs.time.DateTime import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.chat.GroupChat +import dev.inmo.tgbotapi.types.chat.PreviewGroupChat import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage data class CommonGroupEventMessage( override val messageId: MessageId, override val from: User, - override val chat: GroupChat, + override val chat: PreviewGroupChat, override val chatEvent: T, override val date: DateTime ) : GroupEventMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt index 670f03991e..6ff705d126 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt @@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.types.message import korlibs.time.DateTime import dev.inmo.tgbotapi.types.MessageId +import dev.inmo.tgbotapi.types.chat.PreviewSupergroupChat import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.chat.SupergroupChat import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.SupergroupEvent @@ -10,7 +11,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.SupergroupEventMessage data class CommonSupergroupEventMessage( override val messageId: MessageId, override val from: User, - override val chat: SupergroupChat, + override val chat: PreviewSupergroupChat, override val chatEvent: T, override val date: DateTime ) : SupergroupEventMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt index 0871416a1e..73bd4a4174 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/GroupMessages.kt @@ -10,8 +10,8 @@ import dev.inmo.tgbotapi.types.message.abstracts.* import dev.inmo.tgbotapi.types.message.content.MessageContent data class ConnectedFromChannelGroupContentMessageImpl( - override val chat: GroupChat, - override val channel: ChannelChat, + override val chat: PreviewGroupChat, + override val channel: PreviewChannelChat, override val messageId: MessageId, override val date: DateTime, override val forwardInfo: ForwardInfo?, @@ -26,8 +26,8 @@ data class ConnectedFromChannelGroupContentMessageImpl( ) : ConnectedFromChannelGroupContentMessage data class UnconnectedFromChannelGroupContentMessageImpl( - override val chat: GroupChat, - override val channel: ChannelChat, + override val chat: PreviewGroupChat, + override val channel: PreviewChannelChat, override val messageId: MessageId, override val date: DateTime, override val forwardInfo: ForwardInfo?, @@ -42,7 +42,7 @@ data class UnconnectedFromChannelGroupContentMessageImpl( ) : UnconnectedFromChannelGroupContentMessage data class AnonymousGroupContentMessageImpl( - override val chat: GroupChat, + override val chat: PreviewGroupChat, override val messageId: MessageId, override val date: DateTime, override val forwardInfo: ForwardInfo?, @@ -57,7 +57,7 @@ data class AnonymousGroupContentMessageImpl( ) : AnonymousGroupContentMessage data class CommonGroupContentMessageImpl( - override val chat: GroupChat, + override val chat: PreviewGroupChat, override val messageId: MessageId, override val from: User, override val date: DateTime, @@ -72,8 +72,8 @@ data class CommonGroupContentMessageImpl( ) : CommonGroupContentMessage data class FromChannelForumContentMessageImpl( - override val chat: ForumChat, - override val channel: ChannelChat, + override val chat: PreviewForumChat, + override val channel: PreviewChannelChat, override val messageId: MessageId, override val threadId: MessageThreadId, override val date: DateTime, @@ -89,7 +89,7 @@ data class FromChannelForumContentMessageImpl( ) : FromChannelForumContentMessage data class AnonymousForumContentMessageImpl( - override val chat: ForumChat, + override val chat: PreviewForumChat, override val messageId: MessageId, override val threadId: MessageThreadId, override val date: DateTime, @@ -105,7 +105,7 @@ data class AnonymousForumContentMessageImpl( ) : AnonymousForumContentMessage data class CommonForumContentMessageImpl( - override val chat: ForumChat, + override val chat: PreviewForumChat, override val messageId: MessageId, override val threadId: MessageThreadId, override val from: User, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt index 431aeecfe5..fd2945c430 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PassportMessage.kt @@ -4,13 +4,14 @@ import korlibs.time.DateTime import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.chat.Chat +import dev.inmo.tgbotapi.types.chat.PreviewChat import dev.inmo.tgbotapi.types.message.abstracts.FromUserMessage import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.passport.PassportData data class PassportMessage( override val messageId: MessageId, - override val chat: Chat, + override val chat: PreviewChat, override val from: User, override val date: DateTime, val passportData: PassportData diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateContentMessageImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateContentMessageImpl.kt index dd1032c96a..a9b4ae9c96 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateContentMessageImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateContentMessageImpl.kt @@ -13,7 +13,7 @@ import dev.inmo.tgbotapi.types.message.content.MessageContent data class PrivateContentMessageImpl( override val messageId: MessageId, override val from: User, - override val chat: Chat, + override val chat: PreviewPrivateChat, override val content: T, override val date: DateTime, override val editDate: DateTime?, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateEventMessage.kt index 4e26eb0ea8..d12a87c8e3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/PrivateEventMessage.kt @@ -2,13 +2,14 @@ package dev.inmo.tgbotapi.types.message import korlibs.time.DateTime import dev.inmo.tgbotapi.types.MessageId +import dev.inmo.tgbotapi.types.chat.PreviewPrivateChat import dev.inmo.tgbotapi.types.chat.PrivateChat import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.PrivateEvent import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage data class PrivateEventMessage( override val messageId: MessageId, - override val chat: PrivateChat, + override val chat: PreviewPrivateChat, override val chatEvent: T, override val date: DateTime ) : ChatEventMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt index 2a4feb665b..fcaf6164b5 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/RawMessage.kt @@ -43,11 +43,11 @@ internal data class RawMessage( @SerialName(messageIdField) val messageId: MessageId, val date: TelegramDate, - private val chat: Chat, + private val chat: PreviewChat, @SerialName(messageThreadIdField) private val messageThreadId: MessageThreadId? = null, private val from: User? = null, - private val sender_chat: PublicChat? = null, + private val sender_chat: PreviewPublicChat? = null, private val forward_from: User? = null, private val forward_from_chat: Chat? = null, private val forward_from_message_id: MessageId? = null, @@ -274,27 +274,27 @@ internal data class RawMessage( try { chatEvent?.let { chatEvent -> when (chat) { - is SupergroupChat -> CommonSupergroupEventMessage( + is PreviewSupergroupChat -> CommonSupergroupEventMessage( messageId, from ?: error("Supergroup events are expected to contain 'from' field"), chat, chatEvent as? SupergroupEvent ?: throwWrongChatEvent(SupergroupEvent::class, chatEvent), date.asDate ) - is GroupChat -> CommonGroupEventMessage( + is PreviewGroupChat -> CommonGroupEventMessage( messageId, from ?: error("Supergroup events are expected to contain 'from' field"), chat, chatEvent as? GroupEvent ?: throwWrongChatEvent(GroupChat::class, chatEvent), date.asDate ) - is ChannelChat -> ChannelEventMessage( + is PreviewChannelChat -> ChannelEventMessage( messageId, chat, chatEvent as? ChannelEvent ?: throwWrongChatEvent(ChannelEvent::class, chatEvent), date.asDate ) - is PrivateChat -> PrivateEventMessage( + is PreviewPrivateChat -> PrivateEventMessage( messageId, chat, chatEvent as? PrivateEvent ?: throwWrongChatEvent(PrivateEvent::class, chatEvent), @@ -303,8 +303,8 @@ internal data class RawMessage( else -> error("Expected one of the public chats, but was $chat (in extracting of chat event message)") } } ?: content?.let { content -> when (chat) { - is PublicChat -> when (chat) { - is ChannelChat -> ChannelContentMessageImpl( + is PreviewPublicChat -> when (chat) { + is PreviewChannelChat -> ChannelContentMessageImpl( messageId, chat, content, @@ -318,17 +318,16 @@ internal data class RawMessage( author_signature, media_group_id ) - is ForumChat -> if (messageThreadId != null) { + is PreviewForumChat -> if (messageThreadId != null) { val chatId = ChatIdWithThreadId( chat.id.chatId, messageThreadId ) val actualForumChat = when (chat) { - is ExtendedForumChatImpl -> chat.copy(id = chatId) is ForumChatImpl -> chat.copy(id = chatId) } when (sender_chat) { - is ChannelChat -> FromChannelForumContentMessageImpl( + is PreviewChannelChat -> FromChannelForumContentMessageImpl( actualForumChat, sender_chat, messageId, @@ -344,7 +343,7 @@ internal data class RawMessage( author_signature, media_group_id ) - is GroupChat -> AnonymousForumContentMessageImpl( + is PreviewGroupChat -> AnonymousForumContentMessageImpl( actualForumChat, messageId, messageThreadId, @@ -377,7 +376,7 @@ internal data class RawMessage( } } else { when (sender_chat) { - is ChannelChat -> if (is_automatic_forward == true) { + is PreviewChannelChat -> if (is_automatic_forward == true) { ConnectedFromChannelGroupContentMessageImpl( chat, sender_chat, @@ -440,8 +439,8 @@ internal data class RawMessage( ) } } - is GroupChat -> when (sender_chat) { - is ChannelChat -> if (is_automatic_forward == true) { + is PreviewGroupChat -> when (sender_chat) { + is PreviewChannelChat -> if (is_automatic_forward == true) { ConnectedFromChannelGroupContentMessageImpl( chat, sender_chat, @@ -474,7 +473,7 @@ internal data class RawMessage( media_group_id ) } - is GroupChat -> AnonymousGroupContentMessageImpl( + is PreviewGroupChat -> AnonymousGroupContentMessageImpl( chat, messageId, date.asDate, @@ -504,7 +503,7 @@ internal data class RawMessage( ) } } - is PrivateChat -> PrivateContentMessageImpl( + is PreviewPrivateChat -> PrivateContentMessageImpl( messageId, from ?: error("Was detected common message, but owner (sender) of the message was not found"), chat, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelContentMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelContentMessage.kt index 985da07ae0..1cb54a4762 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelContentMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChannelContentMessage.kt @@ -1,10 +1,11 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.chat.ChannelChat +import dev.inmo.tgbotapi.types.chat.PreviewChannelChat import dev.inmo.tgbotapi.types.message.content.MessageContent interface ChannelContentMessage : PossiblySentViaBotCommonMessage, SignedMessage, WithSenderChatMessage { - override val chat: ChannelChat - override val senderChat: ChannelChat + override val chat: PreviewChannelChat + override val senderChat: PreviewChannelChat get() = chat } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt index f8988d489d..296777cb43 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt @@ -1,5 +1,9 @@ package dev.inmo.tgbotapi.types.message.abstracts +import dev.inmo.tgbotapi.types.chat.PreviewChat +import dev.inmo.tgbotapi.types.chat.PreviewGroupChat import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent -interface GroupEventMessage : ChatEventMessage, FromUserMessage +interface GroupEventMessage : ChatEventMessage, FromUserMessage { + override val chat: PreviewGroupChat +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupMessages.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupMessages.kt index 7553560cb3..0ea78e8887 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupMessages.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupMessages.kt @@ -1,24 +1,22 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.MessageThreadId -import dev.inmo.tgbotapi.types.chat.ChannelChat -import dev.inmo.tgbotapi.types.chat.ForumChat -import dev.inmo.tgbotapi.types.chat.GroupChat +import dev.inmo.tgbotapi.types.chat.* import dev.inmo.tgbotapi.types.message.content.MessageContent sealed interface GroupContentMessage : PublicContentMessage { - override val chat: GroupChat + override val chat: PreviewGroupChat } sealed interface ForumContentMessage : GroupContentMessage, PossiblyTopicMessage { - override val chat: ForumChat + override val chat: PreviewForumChat override val threadId: MessageThreadId } sealed interface FromChannelGroupContentMessage : GroupContentMessage, SignedMessage, WithSenderChatMessage { - val channel: ChannelChat - override val senderChat: ChannelChat + val channel: PreviewChannelChat + override val senderChat: PreviewChannelChat get() = channel } @@ -26,7 +24,7 @@ interface ConnectedFromChannelGroupContentMessage : FromChann interface UnconnectedFromChannelGroupContentMessage : FromChannelGroupContentMessage interface AnonymousGroupContentMessage : GroupContentMessage, SignedMessage, WithSenderChatMessage { - override val senderChat: GroupChat + override val senderChat: PreviewGroupChat get() = chat } @@ -35,7 +33,7 @@ interface CommonGroupContentMessage : GroupContentMessage interface FromChannelForumContentMessage : FromChannelGroupContentMessage, ForumContentMessage interface AnonymousForumContentMessage : ForumContentMessage, SignedMessage, WithSenderChatMessage { - override val senderChat: GroupChat + override val senderChat: PreviewGroupChat get() = chat } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/Message.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/Message.kt index eb922ce0db..0b34ea85a1 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/Message.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/Message.kt @@ -1,10 +1,11 @@ package dev.inmo.tgbotapi.types.message.abstracts import korlibs.time.DateTime -import dev.inmo.tgbotapi.abstracts.WithChat +import dev.inmo.tgbotapi.abstracts.WithPreviewChat import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.chat.Chat +import dev.inmo.tgbotapi.types.chat.PreviewChat import dev.inmo.tgbotapi.types.message.RawMessage import kotlinx.serialization.* import kotlinx.serialization.descriptors.* @@ -12,14 +13,14 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder @ClassCastsIncluded(excludeRegex = ".*Impl") -interface Message : WithChat { +interface Message : WithPreviewChat { val messageId: MessageId val date: DateTime } data class UnknownMessageType( override val messageId: MessageId, - override val chat: Chat, + override val chat: PreviewChat, override val date: DateTime, val insideException: Exception ) : Message diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PrivateMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PrivateMessage.kt index 26c58cb8f7..590a72737f 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PrivateMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PrivateMessage.kt @@ -1,5 +1,9 @@ package dev.inmo.tgbotapi.types.message.abstracts +import dev.inmo.tgbotapi.types.chat.PreviewChat +import dev.inmo.tgbotapi.types.chat.PreviewPrivateChat import dev.inmo.tgbotapi.types.message.content.MessageContent -interface PrivateContentMessage : PossiblySentViaBotCommonMessage, FromUserMessage +interface PrivateContentMessage : PossiblySentViaBotCommonMessage, FromUserMessage { + override val chat: PreviewPrivateChat +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PublicMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PublicMessage.kt index 21264b6db0..e8af0d9fbd 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PublicMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PublicMessage.kt @@ -1,8 +1,9 @@ package dev.inmo.tgbotapi.types.message.abstracts +import dev.inmo.tgbotapi.types.chat.PreviewPublicChat import dev.inmo.tgbotapi.types.chat.PublicChat import dev.inmo.tgbotapi.types.message.content.MessageContent sealed interface PublicContentMessage : PossiblySentViaBotCommonMessage { - override val chat: PublicChat + override val chat: PreviewPublicChat } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt index 0e111cec2c..a19641390c 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt @@ -1,5 +1,9 @@ package dev.inmo.tgbotapi.types.message.abstracts +import dev.inmo.tgbotapi.types.chat.PreviewGroupChat +import dev.inmo.tgbotapi.types.chat.PreviewSupergroupChat import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.SupergroupEvent -interface SupergroupEventMessage : GroupEventMessage +interface SupergroupEventMessage : GroupEventMessage { + override val chat: PreviewSupergroupChat +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/WithSenderChatMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/WithSenderChatMessage.kt index fd2934c636..75a9bb3417 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/WithSenderChatMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/WithSenderChatMessage.kt @@ -1,7 +1,8 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.chat.Chat +import dev.inmo.tgbotapi.types.chat.PreviewChat interface WithSenderChatMessage { - val senderChat: Chat + val senderChat: PreviewChat } diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt index e0fcb89557..5dadba2ded 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCastsNew.kt @@ -126,6 +126,17 @@ import dev.inmo.tgbotapi.types.chat.ExtendedSupergroupChat import dev.inmo.tgbotapi.types.chat.ForumChat import dev.inmo.tgbotapi.types.chat.GroupChat import dev.inmo.tgbotapi.types.chat.PossiblyPremiumChat +import dev.inmo.tgbotapi.types.chat.PreviewBot +import dev.inmo.tgbotapi.types.chat.PreviewChannelChat +import dev.inmo.tgbotapi.types.chat.PreviewChat +import dev.inmo.tgbotapi.types.chat.PreviewForumChat +import dev.inmo.tgbotapi.types.chat.PreviewGroupChat +import dev.inmo.tgbotapi.types.chat.PreviewPrivateChat +import dev.inmo.tgbotapi.types.chat.PreviewPublicChat +import dev.inmo.tgbotapi.types.chat.PreviewSuperPublicChat +import dev.inmo.tgbotapi.types.chat.PreviewSupergroupChat +import dev.inmo.tgbotapi.types.chat.PreviewUser +import dev.inmo.tgbotapi.types.chat.PreviewUsernameChat import dev.inmo.tgbotapi.types.chat.PrivateChat import dev.inmo.tgbotapi.types.chat.PublicChat import dev.inmo.tgbotapi.types.chat.SuperPublicChat @@ -1969,12 +1980,30 @@ public inline fun Chat.userOrThrow(): User = this as dev.inmo.tgbotapi.types.cha public inline fun Chat.ifUser(block: (User) -> T): T? = userOrNull() ?.let(block) +public inline fun Chat.previewUserOrNull(): PreviewUser? = this as? + dev.inmo.tgbotapi.types.chat.PreviewUser + +public inline fun Chat.previewUserOrThrow(): PreviewUser = this as + dev.inmo.tgbotapi.types.chat.PreviewUser + +public inline fun Chat.ifPreviewUser(block: (PreviewUser) -> T): T? = previewUserOrNull() + ?.let(block) + public inline fun Chat.botOrNull(): Bot? = this as? dev.inmo.tgbotapi.types.chat.Bot public inline fun Chat.botOrThrow(): Bot = this as dev.inmo.tgbotapi.types.chat.Bot public inline fun Chat.ifBot(block: (Bot) -> T): T? = botOrNull() ?.let(block) +public inline fun Chat.previewBotOrNull(): PreviewBot? = this as? + dev.inmo.tgbotapi.types.chat.PreviewBot + +public inline fun Chat.previewBotOrThrow(): PreviewBot = this as + dev.inmo.tgbotapi.types.chat.PreviewBot + +public inline fun Chat.ifPreviewBot(block: (PreviewBot) -> T): T? = previewBotOrNull() + ?.let(block) + public inline fun Chat.commonBotOrNull(): CommonBot? = this as? dev.inmo.tgbotapi.types.chat.CommonBot @@ -1992,6 +2021,87 @@ public inline fun Chat.commonUserOrThrow(): CommonUser = this as public inline fun Chat.ifCommonUser(block: (CommonUser) -> T): T? = commonUserOrNull() ?.let(block) +public inline fun Chat.previewChatOrNull(): PreviewChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewChat + +public inline fun Chat.previewChatOrThrow(): PreviewChat = this as + dev.inmo.tgbotapi.types.chat.PreviewChat + +public inline fun Chat.ifPreviewChat(block: (PreviewChat) -> T): T? = previewChatOrNull() + ?.let(block) + +public inline fun Chat.previewUsernameChatOrNull(): PreviewUsernameChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewUsernameChat + +public inline fun Chat.previewUsernameChatOrThrow(): PreviewUsernameChat = this as + dev.inmo.tgbotapi.types.chat.PreviewUsernameChat + +public inline fun Chat.ifPreviewUsernameChat(block: (PreviewUsernameChat) -> T): T? = + previewUsernameChatOrNull() ?.let(block) + +public inline fun Chat.previewPrivateChatOrNull(): PreviewPrivateChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewPrivateChat + +public inline fun Chat.previewPrivateChatOrThrow(): PreviewPrivateChat = this as + dev.inmo.tgbotapi.types.chat.PreviewPrivateChat + +public inline fun Chat.ifPreviewPrivateChat(block: (PreviewPrivateChat) -> T): T? = + previewPrivateChatOrNull() ?.let(block) + +public inline fun Chat.previewPublicChatOrNull(): PreviewPublicChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewPublicChat + +public inline fun Chat.previewPublicChatOrThrow(): PreviewPublicChat = this as + dev.inmo.tgbotapi.types.chat.PreviewPublicChat + +public inline fun Chat.ifPreviewPublicChat(block: (PreviewPublicChat) -> T): T? = + previewPublicChatOrNull() ?.let(block) + +public inline fun Chat.previewSuperPublicChatOrNull(): PreviewSuperPublicChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewSuperPublicChat + +public inline fun Chat.previewSuperPublicChatOrThrow(): PreviewSuperPublicChat = this as + dev.inmo.tgbotapi.types.chat.PreviewSuperPublicChat + +public inline fun Chat.ifPreviewSuperPublicChat(block: (PreviewSuperPublicChat) -> T): T? = + previewSuperPublicChatOrNull() ?.let(block) + +public inline fun Chat.previewChannelChatOrNull(): PreviewChannelChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewChannelChat + +public inline fun Chat.previewChannelChatOrThrow(): PreviewChannelChat = this as + dev.inmo.tgbotapi.types.chat.PreviewChannelChat + +public inline fun Chat.ifPreviewChannelChat(block: (PreviewChannelChat) -> T): T? = + previewChannelChatOrNull() ?.let(block) + +public inline fun Chat.previewGroupChatOrNull(): PreviewGroupChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewGroupChat + +public inline fun Chat.previewGroupChatOrThrow(): PreviewGroupChat = this as + dev.inmo.tgbotapi.types.chat.PreviewGroupChat + +public inline fun Chat.ifPreviewGroupChat(block: (PreviewGroupChat) -> T): T? = + previewGroupChatOrNull() ?.let(block) + +public inline fun Chat.previewSupergroupChatOrNull(): PreviewSupergroupChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewSupergroupChat + +public inline fun Chat.previewSupergroupChatOrThrow(): PreviewSupergroupChat = this as + dev.inmo.tgbotapi.types.chat.PreviewSupergroupChat + +public inline fun Chat.ifPreviewSupergroupChat(block: (PreviewSupergroupChat) -> T): T? = + previewSupergroupChatOrNull() ?.let(block) + +public inline fun Chat.previewForumChatOrNull(): PreviewForumChat? = this as? + dev.inmo.tgbotapi.types.chat.PreviewForumChat + +public inline fun Chat.previewForumChatOrThrow(): PreviewForumChat = this as + dev.inmo.tgbotapi.types.chat.PreviewForumChat + +public inline fun Chat.ifPreviewForumChat(block: (PreviewForumChat) -> T): T? = + previewForumChatOrNull() ?.let(block) + public inline fun Chat.unknownChatTypeOrNull(): UnknownChatType? = this as? dev.inmo.tgbotapi.types.chat.UnknownChatType diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FlowsSame.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FlowsSame.kt index a45ecd1cb5..291f4e770d 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FlowsSame.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/FlowsSame.kt @@ -1,7 +1,7 @@ package dev.inmo.tgbotapi.extensions.utils.extensions import dev.inmo.tgbotapi.abstracts.FromUser -import dev.inmo.tgbotapi.abstracts.WithChat +import dev.inmo.tgbotapi.abstracts.WithPreviewChat import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.UserId import dev.inmo.tgbotapi.types.chat.Chat @@ -15,13 +15,13 @@ import kotlinx.coroutines.flow.transform * Will pass only those [T] which have [sameChat] as [chatId] */ @Suppress("NOTHING_TO_INLINE") -inline fun Flow.fromChat(chatId: ChatIdentifier): Flow = filter { it.sameChat(chatId) } +inline fun Flow.fromChat(chatId: ChatIdentifier): Flow = filter { it.sameChat(chatId) } /** * Will pass only those [T] which have [sameChat] as [chatId] */ @Suppress("NOTHING_TO_INLINE") -inline fun Flow.fromChat(chat: Chat): Flow = fromChat(chat.id) +inline fun Flow.fromChat(chat: Chat): Flow = fromChat(chat.id) /** * @return [Flow] with the [FromUser.user] field [User.id] the same as [userId] diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt index 8ef2b69166..360d71c6f4 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt @@ -1,6 +1,6 @@ package dev.inmo.tgbotapi.extensions.utils.extensions -import dev.inmo.tgbotapi.abstracts.WithChat +import dev.inmo.tgbotapi.abstracts.WithPreviewChat import dev.inmo.tgbotapi.extensions.utils.usernameChatOrNull import dev.inmo.tgbotapi.extensions.utils.whenUsernameChat import dev.inmo.tgbotapi.types.ChatIdentifier @@ -16,21 +16,23 @@ import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull * @return true in case if [this] message is placed in the chat with id == [chatId] */ @Suppress("NOTHING_TO_INLINE") -inline fun WithChat.sameChat(chatId: ChatIdentifier) = chat.id == chatId || (chatId is Username && chat.whenUsernameChat { - it.username == chatId -} ?: false) +inline fun WithPreviewChat.sameChat(chatId: ChatIdentifier) = + chat.id == chatId || (chatId is Username && chat.whenUsernameChat { + it.username == chatId + } ?: false) /** * @return true in case if [this] message is placed in the [chat] */ @Suppress("NOTHING_TO_INLINE") -inline fun WithChat.sameChat(chat: Chat) = sameChat(chat.id) || chat.usernameChatOrNull() ?.username ?.let { sameChat(it) } ?: false +inline fun WithPreviewChat.sameChat(chat: Chat) = + sameChat(chat.id) || chat.usernameChatOrNull()?.username?.let { sameChat(it) } ?: false /** * @return true in case if [this] message is placed in the same chat that [other] */ @Suppress("NOTHING_TO_INLINE") -inline fun WithChat.sameChat(other: Message) = sameChat(other.chat) +inline fun WithPreviewChat.sameChat(other: Message) = sameChat(other.chat) /** * @return true in case if [this] message is from the same chat (with id == [chatId]) and [this] [Message.messageId] From 46e6eeca9d643d57354455ff55fe98c6e0965fa4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 11 Oct 2023 13:38:09 +0600 Subject: [PATCH 3/3] improve serializers --- .../dev/inmo/tgbotapi/types/chat/Abstracts.kt | 22 +++++----- .../tgbotapi/types/chat/ChatSerializers.kt | 44 +++++++++---------- .../dev/inmo/tgbotapi/types/chat/Extended.kt | 6 ++- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt index d0d497e768..e2e85645ef 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Abstracts.kt @@ -4,51 +4,51 @@ import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded import dev.inmo.tgbotapi.types.* import kotlinx.serialization.Serializable -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface UsernameChat : Chat { val username: Username? } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface PrivateChat : Chat, UsernameChat { override val id: UserId val firstName: String val lastName: String } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface PublicChat : Chat { val title: String } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface SuperPublicChat : PublicChat, UsernameChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface ChannelChat : SuperPublicChat { override val id: ChatId } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface GroupChat : PublicChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface SupergroupChat : GroupChat, SuperPublicChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface ForumChat : SupergroupChat -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface PossiblyPremiumChat : Chat { val isPremium: Boolean } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) sealed interface AbleToAddInAttachmentMenuChat : Chat { val addedToAttachmentMenu: Boolean } -@Serializable(PreviewChatSerializer::class) +@Serializable(ChatSerializer::class) @ClassCastsIncluded(excludeRegex = ".*Impl") sealed interface Chat { val id: IdChatIdentifier diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt index 989979c3e8..80433c7420 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/ChatSerializers.kt @@ -56,38 +56,35 @@ object ChatSerializer : KSerializer { override fun deserialize(decoder: Decoder): Chat { val decodedJson = JsonObject.serializer().deserialize(decoder) - val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson") - val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true + return try { + formatter.decodeFromJsonElement(ExtendedChatSerializer, decodedJson) + } catch (e: SerializationException) { + val type = decodedJson[typeField] ?.jsonPrimitive ?.content ?.asChatType ?: error("Field $typeField must be presented, but absent in $decodedJson") + val isForum = decodedJson[isForumField] ?.jsonPrimitive ?.booleanOrNull == true - return when (type) { - ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) - ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) - ChatType.SupergroupChatType -> if (isForum) { - formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) - } else { - formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) + when (type) { + ChatType.PrivateChatType -> formatter.decodeFromJsonElement(PrivateChatImpl.serializer(), decodedJson) + ChatType.GroupChatType -> formatter.decodeFromJsonElement(GroupChatImpl.serializer(), decodedJson) + ChatType.SupergroupChatType -> if (isForum) { + formatter.decodeFromJsonElement(ForumChatImpl.serializer(), decodedJson) + } else { + formatter.decodeFromJsonElement(SupergroupChatImpl.serializer(), decodedJson) + } + ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) + is ChatType.UnknownChatType -> UnknownChatType( + formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), + decodedJson.toString(), + decodedJson + ) } - ChatType.ChannelChatType -> formatter.decodeFromJsonElement(ChannelChatImpl.serializer(), decodedJson) - is ChatType.UnknownChatType -> UnknownChatType( - formatter.decodeFromJsonElement(Long.serializer(), decodedJson[chatIdField] ?: JsonPrimitive(-1)).toChatId(), - decodedJson.toString(), - decodedJson - ) } } override fun serialize(encoder: Encoder, value: Chat) { when (value) { is ExtendedChat -> ExtendedChatSerializer.serialize(encoder, value) - is PrivateChatImpl -> PrivateChatImpl.serializer().serialize(encoder, value) - is GroupChatImpl -> GroupChatImpl.serializer().serialize(encoder, value) - is SupergroupChatImpl -> SupergroupChatImpl.serializer().serialize(encoder, value) - is ForumChatImpl -> ForumChatImpl.serializer().serialize(encoder, value) - is ChannelChatImpl -> ChannelChatImpl.serializer().serialize(encoder, value) - is CommonBot -> CommonBot.serializer().serialize(encoder, value) + is PreviewChat -> PreviewChatSerializer.serialize(encoder, value) is ExtendedBot -> ExtendedBot.serializer().serialize(encoder, value) - is CommonUser -> CommonUser.serializer().serialize(encoder, value) - is UnknownChatType -> JsonObject.serializer().serialize(encoder, value.rawJson) } } } @@ -170,6 +167,7 @@ sealed class ExtendedChatSerializer : KSerializer { is ExtendedSupergroupChatImpl -> ExtendedSupergroupChatImpl.serializer().serialize(encoder, value) is ExtendedForumChatImpl -> ExtendedForumChatImpl.serializer().serialize(encoder, value) is ExtendedChannelChatImpl -> ExtendedChannelChatImpl.serializer().serialize(encoder, value) + is ExtendedBot -> ExtendedBot.serializer().serialize(encoder, value) is UnknownExtendedChat -> JsonObject.serializer().serialize(encoder, value.rawJson) } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt index 1cc41715e5..91d502f931 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/Extended.kt @@ -184,8 +184,10 @@ data class ExtendedBot( @SerialName(canReadAllGroupMessagesField) val canReadAllGroupMessages: Boolean = false, @SerialName(supportInlineQueriesField) - val supportsInlineQueries: Boolean = false -) : Bot() { + val supportsInlineQueries: Boolean = false, + @SerialName(photoField) + override val chatPhoto: ChatPhoto? = null +) : Bot(), ExtendedChat { @SerialName(isBotField) private val isBot = true }