From 8c51ca9d7262c09e137824bb6a6cee26ec630000 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 4 Aug 2022 22:13:02 +0600 Subject: [PATCH] fixes in forward from public chat info --- .../tgbotapi/types/message/ForwardInfo.kt | 63 ++++++++++++------- .../inmo/tgbotapi/types/message/RawMessage.kt | 22 ++++--- .../extensions/utils/ClassCastsNew.kt | 21 +++++-- 3 files changed, 72 insertions(+), 34 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt index 225bd1e3c1..4466d95500 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ForwardInfo.kt @@ -5,38 +5,59 @@ import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.* import dev.inmo.tgbotapi.types.chat.User -sealed class ForwardInfo { +sealed interface ForwardInfo { abstract val dateOfOriginal: TelegramDate } data class AnonymousForwardInfo( override val dateOfOriginal: TelegramDate, val senderName: String -) : ForwardInfo() +) : ForwardInfo data class UserForwardInfo( override val dateOfOriginal: TelegramDate, override val from: User -) : ForwardInfo(), FromUser +) : ForwardInfo, FromUser -sealed class ForwardFromPublicChatInfo : ForwardInfo() { - abstract val chat: PublicChat +sealed interface ForwardFromPublicChatInfo : ForwardInfo { + val chat: PublicChat + + /** + * Represent forward info for the message sent by [channelChat] into some group + */ + data class SentByChannel( + override val dateOfOriginal: TelegramDate, + val channelChat: ChannelChat, + val signature: String? = null + ) : ForwardFromPublicChatInfo { + override val chat: PublicChat + get() = channelChat + } + + /** + * Represent forward info for the message sent by [channelChat] into that channel + */ + data class FromChannel( + override val dateOfOriginal: TelegramDate, + val messageId: MessageIdentifier, + val channelChat: ChannelChat, + val signature: String? = null + ) : ForwardFromPublicChatInfo { + override val chat: PublicChat + get() = channelChat + } + + data class FromSupergroup( + override val dateOfOriginal: TelegramDate, + val group: SupergroupChat + ) : ForwardFromPublicChatInfo { + override val chat: PublicChat + get() = group + } } -data class ForwardFromChannelInfo( - override val dateOfOriginal: TelegramDate, - val messageId: MessageIdentifier, - val channelChat: ChannelChat, - val signature: String? = null -) : ForwardFromPublicChatInfo() { - override val chat: PublicChat - get() = channelChat -} +@Deprecated("Replaced", ReplaceWith("ForwardFromPublicChatInfo.FromChannel", "dev.inmo.tgbotapi.types.message.FromChannel")) +typealias ForwardFromChannelInfo = ForwardFromPublicChatInfo.FromChannel -data class ForwardFromSupergroupInfo( - override val dateOfOriginal: TelegramDate, - val group: SupergroupChat -) : ForwardFromPublicChatInfo() { - override val chat: PublicChat - get() = group -} +@Deprecated("Replaced", ReplaceWith("ForwardFromPublicChatInfo.FromSupergroup", "dev.inmo.tgbotapi.types.message.ForwardFromPublicChatInfo")) +typealias ForwardFromSupergroupInfo = ForwardFromPublicChatInfo.FromSupergroup 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 1262958af1..49960119e8 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 @@ -162,13 +162,21 @@ internal data class RawMessage( forward_date, forward_sender_name ) - forward_from_chat is ChannelChat -> ForwardFromChannelInfo( - forward_date, - forward_from_message_id ?: error("Channel forwarded message must contain message id, but was not"), - forward_from_chat, - forward_signature - ) - forward_from_chat is SupergroupChat -> ForwardFromSupergroupInfo( + forward_from_chat is ChannelChat -> if (forward_from_message_id == null) { + ForwardFromPublicChatInfo.SentByChannel( + forward_date, + forward_from_chat, + forward_signature + ) + } else { + ForwardFromPublicChatInfo.FromChannel( + forward_date, + forward_from_message_id, + forward_from_chat, + forward_signature + ) + } + forward_from_chat is SupergroupChat -> ForwardFromPublicChatInfo.FromSupergroup( forward_date, forward_from_chat ) 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 125b1da116..2f5d0b7f28 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 @@ -3415,22 +3415,31 @@ inline fun ForwardInfo.forwardFromPublicChatInfoOrNull(): ForwardFromPublicChatI inline fun ForwardInfo.forwardFromPublicChatInfoOrThrow(): ForwardFromPublicChatInfo = this as ForwardFromPublicChatInfo @PreviewFeature -inline fun ForwardInfo.ifForwardFromChannelInfo(block: (ForwardFromChannelInfo) -> T) = forwardFromChannelInfoOrNull() ?.let(block) +inline fun ForwardInfo.ifForwardFromChannelInfo(block: (ForwardFromPublicChatInfo.FromChannel) -> T) = forwardFromChannelInfoOrNull() ?.let(block) @PreviewFeature -inline fun ForwardInfo.forwardFromChannelInfoOrNull(): ForwardFromChannelInfo? = this as? ForwardFromChannelInfo +inline fun ForwardInfo.forwardFromChannelInfoOrNull(): ForwardFromPublicChatInfo.FromChannel? = this as? ForwardFromPublicChatInfo.FromChannel @PreviewFeature -inline fun ForwardInfo.forwardFromChannelInfoOrThrow(): ForwardFromChannelInfo = this as ForwardFromChannelInfo +inline fun ForwardInfo.forwardFromChannelInfoOrThrow(): ForwardFromPublicChatInfo.FromChannel = this as ForwardFromPublicChatInfo.FromChannel @PreviewFeature -inline fun ForwardInfo.ifForwardFromSupergroupInfo(block: (ForwardFromSupergroupInfo) -> T) = forwardFromSupergroupInfoOrNull() ?.let(block) +inline fun ForwardInfo.ifForwardSentByChannelInfo(block: (ForwardFromPublicChatInfo.SentByChannel) -> T) = forwardSentByChannelInfoOrNull() ?.let(block) @PreviewFeature -inline fun ForwardInfo.forwardFromSupergroupInfoOrNull(): ForwardFromSupergroupInfo? = this as? ForwardFromSupergroupInfo +inline fun ForwardInfo.forwardSentByChannelInfoOrNull(): ForwardFromPublicChatInfo.SentByChannel? = this as? ForwardFromPublicChatInfo.SentByChannel @PreviewFeature -inline fun ForwardInfo.forwardFromSupergroupInfoOrThrow(): ForwardFromSupergroupInfo = this as ForwardFromSupergroupInfo +inline fun ForwardInfo.forwardSentByChannelInfoOrThrow(): ForwardFromPublicChatInfo.SentByChannel = this as ForwardFromPublicChatInfo.SentByChannel + +@PreviewFeature +inline fun ForwardInfo.ifForwardFromSupergroupInfo(block: (ForwardFromPublicChatInfo.FromSupergroup) -> T) = forwardFromSupergroupInfoOrNull() ?.let(block) + +@PreviewFeature +inline fun ForwardInfo.forwardFromSupergroupInfoOrNull(): ForwardFromPublicChatInfo.FromSupergroup? = this as? ForwardFromPublicChatInfo.FromSupergroup + +@PreviewFeature +inline fun ForwardInfo.forwardFromSupergroupInfoOrThrow(): ForwardFromPublicChatInfo.FromSupergroup = this as ForwardFromPublicChatInfo.FromSupergroup @PreviewFeature inline fun MessageContent.ifTextedInput(block: (TextedInput) -> T) = textedInputOrNull() ?.let(block)