From 541e00fa03d93fed604733bf88b0a5eccaf7e892 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 6 Nov 2022 14:07:31 +0600 Subject: [PATCH 1/5] start rework --- .../api/InternalUtils/UpdatesUtils.kt | 24 +++ .../extensions/api/send/CopyMessages.kt | 14 +- .../tgbotapi/extensions/api/send/Replies.kt | 24 ++- .../api/send/RepliesWithChatsAndMessages.kt | 4 +- .../tgbotapi/extensions/api/send/Sends.kt | 8 +- .../api/send/media/SendMediaGroup.kt | 14 +- .../expectations/WaitContent.kt | 8 +- .../expectations/WaitContentMessage.kt | 8 +- .../expectations/WaitEditedContent.kt | 8 +- .../expectations/WaitEditedContentMessage.kt | 8 +- .../expectations/WaitMediaGroup.kt | 10 +- .../expectations/WaitMediaGroupMessages.kt | 10 +- .../triggers_handling/ContentTriggers.kt | 4 +- .../EditedContentTriggers.kt | 4 +- .../triggers_handling/MediaGroupTriggers.kt | 42 ++--- .../requests/send/media/SendMediaGroup.kt | 34 +++- .../types/message/ChannelMediaGroupMessage.kt | 4 +- .../types/message/CommonMediaGroupMessage.kt | 4 +- .../types/message/abstracts/CommonMessage.kt | 2 +- .../message/abstracts/MediaGroupMessage.kt | 4 +- .../PossiblySentViaBotCommonMessage.kt | 2 +- .../types/message/abstracts/PublicMessage.kt | 2 +- .../types/message/content/Abstracts.kt | 18 +- .../types/message/content/AbstractsMedia.kt | 28 ++- .../types/message/content/AudioContent.kt | 2 +- .../types/message/content/DocumentContent.kt | 2 +- .../message/content/MediaGroupContent.kt | 48 +++++ .../types/message/content/PhotoContent.kt | 2 +- .../types/message/content/Typealiases.kt | 8 +- .../types/message/content/VideoContent.kt | 2 +- .../ChannelPostMediaGroupUpdate.kt | 4 +- .../EditChannelPostMediaGroupUpdate.kt | 4 +- .../EditMessageMediaGroupUpdate.kt | 4 +- .../update/media_group/MediaGroupUpdate.kt | 6 +- .../media_group/MessageMediaGroupUpdate.kt | 4 +- .../MediaGroupContentMessageCreator.kt | 164 ++++++++++++++++++ .../tgbotapi/extensions/utils/ClassCasts.kt | 58 +++---- .../extensions/utils/ClassCastsNew.kt | 88 +++++----- .../extensions/utils/ContentCastsNew.kt | 6 +- .../tgbotapi/extensions/utils/WithContent.kt | 6 +- .../utils/shortcuts/FlowsUpdatesFilter.kt | 12 +- .../utils/shortcuts/MediaGroupsShortcuts.kt | 14 +- 42 files changed, 495 insertions(+), 227 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt index 3bc9a03bfa..01e9f0ff62 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt @@ -2,7 +2,31 @@ package dev.inmo.tgbotapi.extensions.api.InternalUtils import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.UpdateIdentifier +import dev.inmo.tgbotapi.types.message.AnonymousForumContentMessageImpl +import dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl +import dev.inmo.tgbotapi.types.message.CommonForumContentMessageImpl +import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.ConnectedFromChannelGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.FromChannelForumContentMessageImpl +import dev.inmo.tgbotapi.types.message.PrivateContentMessageImpl +import dev.inmo.tgbotapi.types.message.UnconnectedFromChannelGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.abstracts.AnonymousForumContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.AnonymousGroupContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.ChannelContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.CommonForumContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.ConnectedFromChannelGroupContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.FromChannelForumContentMessage import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupContentMessage +import dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.abstracts.* import dev.inmo.tgbotapi.types.update.media_group.* diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt index 1673fd81ea..181213ca56 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt @@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.message.ParseMode import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate /** @@ -18,14 +18,14 @@ import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate */ suspend inline fun TelegramBot.copyMessages( toChatId: ChatIdentifier, - messages: List>, + messages: List>, text: String? = null, parseMode: ParseMode? = null, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, allowSendingWithoutReply: Boolean? = null -): List> { +): List> { val first = messages.first().content.toMediaGroupMemberTelegramMedia().let { if (text != null) { when (it) { @@ -57,7 +57,7 @@ suspend inline fun TelegramBot.copyMessages( */ suspend inline fun TelegramBot.copyMessages( toChat: Chat, - messages: List>, + messages: List>, text: String? = null, parseMode: ParseMode? = null, disableNotification: Boolean = false, @@ -102,13 +102,13 @@ suspend inline fun TelegramBot.copyMessages( */ suspend inline fun TelegramBot.copyMessages( toChatId: ChatIdentifier, - messages: List>, + messages: List>, entities: TextSourcesList, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, allowSendingWithoutReply: Boolean? = null -): List> { +): List> { val first = messages.first().content.toMediaGroupMemberTelegramMedia().let { when (it) { is TelegramMediaAudio -> TelegramMediaAudio(it.file, entities, it.duration, it.performer, it.title, it.thumb) @@ -136,7 +136,7 @@ suspend inline fun TelegramBot.copyMessages( */ suspend inline fun TelegramBot.copyMessages( toChat: Chat, - messages: List>, + messages: List>, entities: TextSourcesList, disableNotification: Boolean = false, protectContent: Boolean = false, diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Replies.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Replies.kt index e9f4fdeebe..d45ac32771 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Replies.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Replies.kt @@ -1027,18 +1027,16 @@ suspend fun TelegramBot.reply( protectContent: Boolean = false, allowSendingWithoutReply: Boolean? = null, replyMarkup: KeyboardMarkup? = null -) { - execute( - content.createResend( - to.chat.id, - disableNotification, - protectContent, - to.messageId, - allowSendingWithoutReply, - replyMarkup - ) +) = execute( + content.createResend( + to.chat.id, + disableNotification, + protectContent, + to.messageId, + allowSendingWithoutReply, + replyMarkup ) -} +) /** * Will use [handleLiveLocation] with replying to [message] each time new message will be sent by live location update @@ -1219,7 +1217,7 @@ suspend fun TelegramBot.reply( allowSendingWithoutReply = allowSendingWithoutReply, replyMarkup = replyMarkup ) - is AudioMediaGroupContent -> reply( + is AudioMediaGroupPartContent -> reply( to = to, audio = content.media, text = text, @@ -1291,7 +1289,7 @@ suspend fun TelegramBot.reply( allowSendingWithoutReply = allowSendingWithoutReply, replyMarkup = replyMarkup ) - is AudioMediaGroupContent -> reply( + is AudioMediaGroupPartContent -> reply( to = to, audio = content.media, entities = entities, diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt index 0a754e2c52..d88ffa5ad7 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/RepliesWithChatsAndMessages.kt @@ -1298,7 +1298,7 @@ suspend fun TelegramBot.reply( allowSendingWithoutReply = allowSendingWithoutReply, replyMarkup = replyMarkup ) - is AudioMediaGroupContent -> reply( + is AudioMediaGroupPartContent -> reply( toChatId = toChatId, toMessageId = toMessageId, audio = content.media, @@ -1377,7 +1377,7 @@ suspend fun TelegramBot.reply( allowSendingWithoutReply = allowSendingWithoutReply, replyMarkup = replyMarkup ) - is AudioMediaGroupContent -> reply( + is AudioMediaGroupPartContent -> reply( toChatId = toChatId, toMessageId = toMessageId, audio = content.media, diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Sends.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Sends.kt index 46305fbd0a..6eeeaaaaa1 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Sends.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/Sends.kt @@ -1379,7 +1379,7 @@ suspend fun TelegramBot.send( @JvmName("sendMedaGroupByContent") suspend fun TelegramBot.send( chatId: ChatIdentifier, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, @@ -1393,7 +1393,7 @@ suspend fun TelegramBot.send( @JvmName("sendMedaGroupByContent") suspend fun TelegramBot.send( chat: Chat, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, @@ -1536,7 +1536,7 @@ suspend fun TelegramBot.send( @JvmName("sendVisualMediaGroupByContent") suspend fun TelegramBot.send( chatId: ChatIdentifier, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, @@ -1549,7 +1549,7 @@ suspend fun TelegramBot.send( @JvmName("sendVisualMediaGroupByContent") suspend fun TelegramBot.send( chat: Chat, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/media/SendMediaGroup.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/media/SendMediaGroup.kt index 70392452d5..d2dddf9e02 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/media/SendMediaGroup.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/media/SendMediaGroup.kt @@ -6,8 +6,8 @@ import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.media.* import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.chat.Chat -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent -import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent +import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.AudioContent import dev.inmo.tgbotapi.types.message.content.DocumentContent import dev.inmo.tgbotapi.utils.RiskFeature @@ -25,7 +25,7 @@ suspend fun TelegramBot.sendMediaGroup( replyToMessageId: MessageId? = null, allowSendingWithoutReply: Boolean? = null ) = execute( - SendMediaGroup( + SendMediaGroup( chatId, media, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply ) ) @@ -52,7 +52,7 @@ suspend fun TelegramBot.sendMediaGroup( @JvmName("sendMedaGroupByContent") suspend fun TelegramBot.sendMediaGroup( chatId: ChatIdentifier, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, @@ -68,7 +68,7 @@ suspend fun TelegramBot.sendMediaGroup( @JvmName("sendMedaGroupByContent") suspend fun TelegramBot.sendMediaGroup( chat: Chat, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, @@ -233,7 +233,7 @@ suspend fun TelegramBot.sendVisualMediaGroup( @JvmName("sendVisualMediaGroupByContent") suspend fun TelegramBot.sendVisualMediaGroup( chatId: ChatIdentifier, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, @@ -248,7 +248,7 @@ suspend fun TelegramBot.sendVisualMediaGroup( @JvmName("sendVisualMediaGroupByContent") suspend fun TelegramBot.sendVisualMediaGroup( chat: Chat, - media: List, + media: List, disableNotification: Boolean = false, protectContent: Boolean = false, replyToMessageId: MessageId? = null, diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt index 91bce4c17e..86028c62a8 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContent.kt @@ -66,12 +66,12 @@ suspend fun BehaviourContext.waitAudioMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContent(initRequest, includeMediaGroups, errorFactory) +) = waitContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitDocumentMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContent(initRequest, includeMediaGroups, errorFactory) +) = waitContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitMedia( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, @@ -81,12 +81,12 @@ suspend fun BehaviourContext.waitAnyMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContent(initRequest, includeMediaGroups, errorFactory) +) = waitContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitVisualMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContent(initRequest, includeMediaGroups, errorFactory) +) = waitContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitTextedMediaContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt index 9620795bf2..818da8ce2b 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt @@ -103,12 +103,12 @@ suspend fun BehaviourContext.waitAudioMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitDocumentMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitMediaMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, @@ -118,12 +118,12 @@ suspend fun BehaviourContext.waitAnyMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitVisualMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitTextedMediaContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt index a552348dd8..7bac69114d 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt @@ -64,12 +64,12 @@ suspend fun BehaviourContext.waitEditedAudioMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedDocumentMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedMedia( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, @@ -79,12 +79,12 @@ suspend fun BehaviourContext.waitEditedAnyMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedVisualMediaGroupContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContent(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedTextedMediaContent( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt index c5204f68ec..f7c201374f 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt @@ -81,12 +81,12 @@ suspend fun BehaviourContext.waitEditedAudioMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedDocumentMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedMediaMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, @@ -96,12 +96,12 @@ suspend fun BehaviourContext.waitEditedAnyMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedVisualMediaGroupContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) suspend fun BehaviourContext.waitEditedTextedMediaContentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt index 37260e9340..bc614d6962 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt @@ -14,7 +14,7 @@ typealias MediaGroupFilter = SimpleFilter>> @RiskFeature(lowLevelRiskFeatureMessage) -suspend inline fun BehaviourContext.buildMediaGroupWaiter( +suspend inline fun BehaviourContext.buildMediaGroupWaiter( initRequest: Request<*>? = null, noinline errorFactory: NullableRequestBuilder<*> = { null } ): Flow> = buildMediaGroupMessagesWaiter(initRequest, errorFactory).map { it.map { it.content } } @@ -22,19 +22,19 @@ suspend inline fun BehaviourContext.buildMediaGr suspend fun BehaviourContext.waitMediaGroup( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) +) = buildMediaGroupWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitPlaylist( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) +) = buildMediaGroupWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitDocumentsGroup( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) +) = buildMediaGroupWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitVisualGallery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) +) = buildMediaGroupWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitPhotoGallery( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt index f75f8767b7..889e63de09 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt @@ -11,7 +11,7 @@ import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage import kotlinx.coroutines.flow.Flow @RiskFeature(lowLevelRiskFeatureMessage) -suspend inline fun BehaviourContext.buildMediaGroupMessagesWaiter( +suspend inline fun BehaviourContext.buildMediaGroupMessagesWaiter( initRequest: Request<*>? = null, noinline errorFactory: NullableRequestBuilder<*> = { null } ): Flow>> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update -> @@ -26,19 +26,19 @@ suspend inline fun BehaviourContext.buildMediaGr suspend fun BehaviourContext.waitMediaGroupMessages( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitPlaylistMessages( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitDocumentsGroupMessages( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitVisualGalleryMessages( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) suspend fun BehaviourContext.waitPhotoGalleryMessages( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt index e9a19d4d7d..4971de586e 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt @@ -287,7 +287,7 @@ suspend fun BC.onVenue( * data */ suspend fun BC.onAudioMediaGroup( - initialFilter: CommonMessageFilter? = null, + initialFilter: CommonMessageFilter? = null, subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTypeReceiver @@ -311,7 +311,7 @@ suspend fun BC.onAudioMediaGroup( * data */ suspend fun BC.onDocumentMediaGroupContent( - initialFilter: CommonMessageFilter? = null, + initialFilter: CommonMessageFilter? = null, subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTypeReceiver diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt index e339c6f66f..0b6337d018 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/EditedContentTriggers.kt @@ -222,7 +222,7 @@ suspend fun BC.onEditedVenue( * data */ suspend fun BC.onEditedAudioMediaGroup( - initialFilter: CommonMessageFilter? = null, + initialFilter: CommonMessageFilter? = null, subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTypeReceiver @@ -246,7 +246,7 @@ suspend fun BC.onEditedAudioMediaGroup( * data */ suspend fun BC.onEditedDocumentMediaGroupContent( - initialFilter: CommonMessageFilter? = null, + initialFilter: CommonMessageFilter? = null, subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver = MessageFilterByChat, markerFactory: MarkerFactory = ByChatMessageMarkerFactory, scenarioReceiver: CustomBehaviourContextAndTypeReceiver diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt index 58efa0cee3..29a2cdad86 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt @@ -14,7 +14,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.utils.PreviewFeature @PreviewFeature -internal suspend inline fun BC.buildMediaGroupTrigger( +internal suspend inline fun BC.buildMediaGroupTrigger( initialFilter: SimpleFilter>>? = null, noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, @@ -40,10 +40,10 @@ internal suspend inline fun BC.onMediaGroup( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> ) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) /** @@ -59,10 +59,10 @@ suspend fun BC.onMediaGroup( * data */ suspend fun BC.onPlaylist( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> ) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) /** @@ -78,10 +78,10 @@ suspend fun BC.onPlaylist( * data */ suspend fun BC.onDocumentsGroup( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> ) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) /** @@ -97,10 +97,10 @@ suspend fun BC.onDocumentsGroup( * data */ suspend fun BC.onVisualGallery( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> ) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) /** @@ -116,10 +116,10 @@ suspend fun BC.onVisualGallery( * data */ suspend fun BC.onVisualMediaGroup( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> + initialFilter: SimpleFilter>>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, + markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> ) = onVisualGallery(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) /** diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt index c19ff31c0f..f8b0dd4e0d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt @@ -7,21 +7,27 @@ import dev.inmo.tgbotapi.requests.send.media.base.* import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.media.* import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent -import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent +import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.AudioContent import dev.inmo.tgbotapi.types.message.content.DocumentContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupContent import dev.inmo.tgbotapi.utils.* +import dev.inmo.tgbotapi.utils.extensions.asMediaGroupContent import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.buildJsonArray const val rawSendingMediaGroupsWarning = "Media groups contains restrictions related to combinations of media" + " types. Currently it is possible to combine photo + video OR audio OR documents" @RiskFeature(rawSendingMediaGroupsWarning) -fun SendMediaGroup( +fun SendMediaGroup( chatId: ChatIdentifier, media: List, disableNotification: Boolean = false, @@ -107,10 +113,22 @@ inline fun SendVisualMediaGroup( protectContent: Boolean = false, replyToMessageId: MessageId? = null, allowSendingWithoutReply: Boolean? = null -) = SendMediaGroup(chatId, media, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) +) = SendMediaGroup(chatId, media, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) -private val messagesListSerializer: KSerializer>> - = ListSerializer(TelegramBotAPIMessageDeserializeOnlySerializerClass()) +private object MessagesListSerializer: KSerializer> { + private val serializer = ListSerializer(TelegramBotAPIMessageDeserializeOnlySerializerClass>()) + override val descriptor: SerialDescriptor = serializer.descriptor + + override fun deserialize(decoder: Decoder): PossiblySentViaBotCommonMessage { + val messages = serializer.deserialize(decoder) + return messages.asMediaGroupContent() + } + + override fun serialize(encoder: Encoder, value: PossiblySentViaBotCommonMessage) { + serializer.serialize(encoder, value.content.group.map { it.sourceMessage }) + } + +} @Serializable data class SendMediaGroupData internal constructor( @@ -125,7 +143,7 @@ data class SendMediaGroupData internal constructor( override val replyToMessageId: MessageId? = null, @SerialName(allowSendingWithoutReplyField) override val allowSendingWithoutReply: Boolean? = null -) : DataRequest>>, SendMessageRequest>> { +) : DataRequest>>, SendMessageRequest>> { @SerialName(mediaField) private val convertedMedia: String get() = buildJsonArray { @@ -138,7 +156,7 @@ data class SendMediaGroupData internal constructor( override fun method(): String = "sendMediaGroup" override val requestSerializer: SerializationStrategy<*> get() = serializer() - override val resultDeserializer: DeserializationStrategy>> + override val resultDeserializer: DeserializationStrategy>> get() = messagesListSerializer } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt index 859e4415b3..b700a98ba2 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt @@ -7,9 +7,9 @@ import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.Message -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -data class ChannelMediaGroupMessage( +data class ChannelMediaGroupMessage( override val messageId: MessageId, override val chat: Chat, override val date: DateTime, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt index ea544158d6..e1a2f98172 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt @@ -6,9 +6,9 @@ import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.message.abstracts.* -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -data class CommonMediaGroupMessage( +data class CommonMediaGroupMessage( override val messageId: MessageId, override val from: User, override val chat: Chat, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt index bca2700613..2068ab6c33 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt @@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.message.content.MessageContent -interface CommonMessage : Message, +sealed interface CommonMessage : Message, PossiblyForwardedMessage, PossiblyEditedMessage, PossiblyReplyMessage, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt index faac6c32eb..44a8fc7591 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt @@ -1,8 +1,8 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.MediaGroupIdentifier -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -interface MediaGroupMessage : CommonMessage { +interface MediaGroupMessage : CommonMessage { val mediaGroupId: MediaGroupIdentifier } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblySentViaBotCommonMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblySentViaBotCommonMessage.kt index 155b0fdc17..5b7574457d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblySentViaBotCommonMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblySentViaBotCommonMessage.kt @@ -2,4 +2,4 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.message.content.MessageContent -interface PossiblySentViaBotCommonMessage : CommonMessage, PossiblySentViaBot +sealed interface PossiblySentViaBotCommonMessage : CommonMessage, PossiblySentViaBot 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 18a4b904b6..21264b6db0 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 @@ -3,6 +3,6 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.chat.PublicChat import dev.inmo.tgbotapi.types.message.content.MessageContent -interface PublicContentMessage : PossiblySentViaBotCommonMessage { +sealed interface PublicContentMessage : PossiblySentViaBotCommonMessage { override val chat: PublicChat } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Abstracts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Abstracts.kt index c1cc0c0a44..305561fe2a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Abstracts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Abstracts.kt @@ -15,10 +15,10 @@ sealed interface MessageContent: ResendableContent { companion object { @RiskFeature("This serialization module can be changed in near releases") fun serializationModule( - visualMediaGroupContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, - documentMediaGroupContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, - audioMediaGroupContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, - mediaGroupContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, + visualMediaGroupContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, + documentMediaGroupContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, + audioMediaGroupContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, + mediaGroupPartContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, textedMediaContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, mediaContentAdditionalBuilder: PolymorphicModuleBuilder.() -> Unit = {}, mediaCollectionContentAdditionalBuilder: PolymorphicModuleBuilder>.() -> Unit = {}, @@ -73,24 +73,24 @@ sealed interface MessageContent: ResendableContent { textedMediaContentAdditionalBuilder() } - polymorphic(MediaGroupContent::class) { + polymorphic(MediaGroupPartContent::class) { subclass(PhotoContent::class) subclass(AudioContent::class) subclass(DocumentContent::class) - mediaGroupContentAdditionalBuilder() + mediaGroupPartContentAdditionalBuilder() } - polymorphic(AudioMediaGroupContent::class) { + polymorphic(AudioMediaGroupPartContent::class) { subclass(AudioContent::class) audioMediaGroupContentAdditionalBuilder() } - polymorphic(DocumentMediaGroupContent::class) { + polymorphic(DocumentMediaGroupPartContent::class) { subclass(DocumentContent::class) documentMediaGroupContentAdditionalBuilder() } - polymorphic(VisualMediaGroupContent::class) { + polymorphic(VisualMediaGroupPartContent::class) { subclass(PhotoContent::class) subclass(VideoContent::class) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt index 3d31a732c1..3f5cc6adcc 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt @@ -1,28 +1,44 @@ package dev.inmo.tgbotapi.types.message.content import dev.inmo.tgbotapi.abstracts.TextedInput +import dev.inmo.tgbotapi.types.MediaGroupIdentifier +import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.files.AudioFile import dev.inmo.tgbotapi.types.files.DocumentFile import dev.inmo.tgbotapi.types.media.* +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage +import kotlinx.serialization.Serializable -sealed interface AudioMediaGroupContent : MediaGroupContent { +sealed interface AudioMediaGroupPartContent : MediaGroupPartContent { override val media: AudioFile override fun toMediaGroupMemberTelegramMedia(): AudioMediaGroupMemberTelegramMedia } -sealed interface DocumentMediaGroupContent : MediaGroupContent { +sealed interface DocumentMediaGroupPartContent : MediaGroupPartContent { override val media: DocumentFile override fun toMediaGroupMemberTelegramMedia(): DocumentMediaGroupMemberTelegramMedia } -sealed interface MediaGroupContent : TextedMediaContent { +sealed interface TextedMediaContent : MediaContent, TextedInput + +sealed interface MediaGroupCollectionContent : TextedMediaContent { + @Serializable + data class PartWrapper( + val messageId: MessageId, + val content: MediaGroupPartContent, + val sourceMessage: PossiblySentViaBotCommonMessage + ) + val group: List + val mediaGroupId: MediaGroupIdentifier +} + +sealed interface MediaGroupPartContent : TextedMediaContent { fun toMediaGroupMemberTelegramMedia(): MediaGroupMemberTelegramMedia } -sealed interface TextedMediaContent : MediaContent, TextedInput - -sealed interface VisualMediaGroupContent : MediaGroupContent { +sealed interface VisualMediaGroupPartContent : MediaGroupPartContent { override fun toMediaGroupMemberTelegramMedia(): VisualMediaGroupMemberTelegramMedia } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AudioContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AudioContent.kt index 27498e173f..18e43a5d4a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AudioContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AudioContent.kt @@ -17,7 +17,7 @@ data class AudioContent( override val media: AudioFile, override val text: String? = null, override val textSources: TextSourcesList = emptyList() -) : AudioMediaGroupContent { +) : AudioMediaGroupPartContent { override fun createResend( chatId: ChatIdentifier, disableNotification: Boolean, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/DocumentContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/DocumentContent.kt index 56c82a6028..2098468dae 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/DocumentContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/DocumentContent.kt @@ -19,7 +19,7 @@ data class DocumentContent( override val media: DocumentFile, override val text: String? = null, override val textSources: TextSourcesList = emptyList() -) : DocumentMediaGroupContent { +) : DocumentMediaGroupPartContent { override fun createResend( chatId: ChatIdentifier, disableNotification: Boolean, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt new file mode 100644 index 0000000000..6f2f59fbc1 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt @@ -0,0 +1,48 @@ +package dev.inmo.tgbotapi.types.message.content + +import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.requests.send.media.SendMediaGroup +import dev.inmo.tgbotapi.types.ChatIdentifier +import dev.inmo.tgbotapi.types.MediaGroupIdentifier +import dev.inmo.tgbotapi.types.MessageId +import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup +import dev.inmo.tgbotapi.types.files.TelegramMediaFile +import dev.inmo.tgbotapi.types.media.TelegramMedia +import dev.inmo.tgbotapi.types.message.abstracts.Message +import dev.inmo.tgbotapi.types.message.textsources.TextSource +import kotlinx.serialization.Serializable + +@Serializable +data class MediaGroupContent( + override val group: List, + override val mediaGroupId: MediaGroupIdentifier +) : MediaGroupCollectionContent { + val mainContent: MediaGroupPartContent + get() = group.first().content + override val media: TelegramMediaFile + get() = mainContent.media + + override val textSources: List + get() = mainContent.textSources + override val text: String? + get() = mainContent.text + + override fun asTelegramMedia(): TelegramMedia = mainContent.asTelegramMedia() + + override fun createResend( + chatId: ChatIdentifier, + disableNotification: Boolean, + protectContent: Boolean, + replyToMessageId: MessageId?, + allowSendingWithoutReply: Boolean?, + replyMarkup: KeyboardMarkup? + ): Request = SendMediaGroup( + chatId, + group.map { it.content.toMediaGroupMemberTelegramMedia() }, + disableNotification, + protectContent, + replyToMessageId, + allowSendingWithoutReply, + replyMarkup + ) +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/PhotoContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/PhotoContent.kt index 2d134a3c61..b97b852e10 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/PhotoContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/PhotoContent.kt @@ -17,7 +17,7 @@ data class PhotoContent( override val mediaCollection: Photo, override val text: String? = null, override val textSources: TextSourcesList = emptyList() -) : MediaCollectionContent, VisualMediaGroupContent { +) : MediaCollectionContent, VisualMediaGroupPartContent { override val media: PhotoSize = mediaCollection.biggest() ?: throw IllegalStateException("Can't locate any photo size for this content") override fun createResend( diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt index 397ffde08e..f14628d24e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt @@ -20,12 +20,12 @@ typealias VideoNoteMessage = CommonMessage typealias StickerMessage = CommonMessage typealias TextedMediaMessage = CommonMessage typealias VoiceMessage = CommonMessage -typealias MediaGroupMessage = CommonMessage -typealias AudioMediaGroupMessage = CommonMessage +typealias MediaGroupMessage = CommonMessage +typealias AudioMediaGroupMessage = CommonMessage typealias AudioMessage = CommonMessage -typealias DocumentMediaGroupMessage = CommonMessage +typealias DocumentMediaGroupMessage = CommonMessage typealias DocumentMessage = CommonMessage -typealias VisualMediaGroupMessage = CommonMessage +typealias VisualMediaGroupMessage = CommonMessage typealias VideoMessage = CommonMessage typealias PhotoMessage = CommonMessage typealias AnimationMessage = CommonMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/VideoContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/VideoContent.kt index ef160c455f..76e6a8d2b3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/VideoContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/VideoContent.kt @@ -17,7 +17,7 @@ data class VideoContent( override val media: VideoFile, override val text: String? = null, override val textSources: TextSourcesList = emptyList() -) : VisualMediaGroupContent { +) : VisualMediaGroupPartContent { override fun createResend( chatId: ChatIdentifier, disableNotification: Boolean, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt index 85414d9489..21e6f13a0d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt @@ -2,12 +2,12 @@ package dev.inmo.tgbotapi.types.update.media_group import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate data class ChannelPostMediaGroupUpdate( override val origins: List ) : SentMediaGroupUpdate { override val updateId: UpdateIdentifier = origins.last().updateId - override val data: List> = origins.mapNotNull { it.data as? MediaGroupMessage } + override val data: List> = origins.mapNotNull { it.data as? MediaGroupMessage } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt index eaee526558..9af30060a1 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt @@ -2,12 +2,12 @@ package dev.inmo.tgbotapi.types.update.media_group import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.EditChannelPostUpdate data class EditChannelPostMediaGroupUpdate( override val origin: EditChannelPostUpdate ) : EditMediaGroupUpdate { override val updateId: UpdateIdentifier = origin.updateId - override val data: MediaGroupMessage = origin.data as MediaGroupMessage + override val data: MediaGroupMessage = origin.data as MediaGroupMessage } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt index cf9dddbeb3..ebed49e6c5 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt @@ -2,12 +2,12 @@ package dev.inmo.tgbotapi.types.update.media_group import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.EditMessageUpdate data class EditMessageMediaGroupUpdate( override val origin: EditMessageUpdate ) : EditMediaGroupUpdate { override val updateId: UpdateIdentifier = origin.updateId - override val data: MediaGroupMessage = origin.data as MediaGroupMessage + override val data: MediaGroupMessage = origin.data as MediaGroupMessage } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt index 7f9b13358b..0b8fd8ce15 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt @@ -1,7 +1,7 @@ package dev.inmo.tgbotapi.types.update.media_group import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.abstracts.* /** @@ -14,11 +14,11 @@ import dev.inmo.tgbotapi.types.update.abstracts.* sealed interface MediaGroupUpdate : Update sealed interface SentMediaGroupUpdate: MediaGroupUpdate { - override val data: List> + override val data: List> val origins: List } sealed interface EditMediaGroupUpdate : BaseEditMessageUpdate, MediaGroupUpdate { - override val data: MediaGroupMessage + override val data: MediaGroupMessage val origin: BaseMessageUpdate } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt index 329d75c8d3..d36b7f50d8 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt @@ -2,12 +2,12 @@ package dev.inmo.tgbotapi.types.update.media_group import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate data class MessageMediaGroupUpdate( override val origins: List ) : SentMediaGroupUpdate { override val updateId: UpdateIdentifier = origins.last().updateId - override val data: List> = origins.mapNotNull { it.data as? MediaGroupMessage } + override val data: List> = origins.mapNotNull { it.data as? MediaGroupMessage } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt new file mode 100644 index 0000000000..06cb6aaaaa --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt @@ -0,0 +1,164 @@ +package dev.inmo.tgbotapi.utils.extensions + +import dev.inmo.tgbotapi.types.MediaGroupIdentifier +import dev.inmo.tgbotapi.types.message.AnonymousForumContentMessageImpl +import dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl +import dev.inmo.tgbotapi.types.message.CommonForumContentMessageImpl +import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.ConnectedFromChannelGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.FromChannelForumContentMessageImpl +import dev.inmo.tgbotapi.types.message.PrivateContentMessageImpl +import dev.inmo.tgbotapi.types.message.UnconnectedFromChannelGroupContentMessageImpl +import dev.inmo.tgbotapi.types.message.abstracts.AnonymousForumContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.AnonymousGroupContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.ChannelContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.CommonForumContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.ConnectedFromChannelGroupContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.FromChannelForumContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupContentMessage +import dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent +import dev.inmo.tgbotapi.utils.RiskFeature + +@RiskFeature("This API is experimental and can be changed without any notice, use with caution") +fun List>.asMediaGroupContent( + mediaGroupIdentifier: MediaGroupIdentifier +): PossiblySentViaBotCommonMessage { + val content = MediaGroupContent( + map { MediaGroupCollectionContent.PartWrapper(it.messageId, it.content, it) }, + mediaGroupIdentifier + ) + return when (val sourceMessage = first()) { + is MediaGroupMessage -> TODO() + is ChannelContentMessage -> ChannelContentMessageImpl( + sourceMessage.messageId, + sourceMessage.chat, + content, + sourceMessage.date, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.forwardInfo, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + sourceMessage.senderBot, + sourceMessage.authorSignature + ) + is PrivateContentMessage -> PrivateContentMessageImpl( + sourceMessage.messageId, + sourceMessage.user, + sourceMessage.chat, + content, + sourceMessage.date, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.forwardInfo, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + sourceMessage.senderBot + ) + is AnonymousGroupContentMessage -> AnonymousGroupContentMessageImpl( + sourceMessage.chat, + sourceMessage.messageId, + sourceMessage.date, + sourceMessage.forwardInfo, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + content, + sourceMessage.senderBot, + sourceMessage.authorSignature + ) + is CommonGroupContentMessage -> CommonGroupContentMessageImpl( + sourceMessage.chat, + sourceMessage.messageId, + sourceMessage.user, + sourceMessage.date, + sourceMessage.forwardInfo, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + content, + sourceMessage.senderBot + ) + is ConnectedFromChannelGroupContentMessage -> ConnectedFromChannelGroupContentMessageImpl( + sourceMessage.chat, + sourceMessage.channel, + sourceMessage.messageId, + sourceMessage.date, + sourceMessage.forwardInfo, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + content, + sourceMessage.senderBot, + sourceMessage.authorSignature + ) + is UnconnectedFromChannelGroupContentMessage -> UnconnectedFromChannelGroupContentMessageImpl( + sourceMessage.chat, + sourceMessage.channel, + sourceMessage.messageId, + sourceMessage.date, + sourceMessage.forwardInfo, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + content, + sourceMessage.senderBot, + sourceMessage.authorSignature + ) + is AnonymousForumContentMessage -> AnonymousForumContentMessageImpl( + sourceMessage.chat, + sourceMessage.messageId, + sourceMessage.threadId, + sourceMessage.date, + sourceMessage.forwardInfo, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + content, + sourceMessage.senderBot, + sourceMessage.authorSignature + ) + is CommonForumContentMessage -> CommonForumContentMessageImpl( + sourceMessage.chat, + sourceMessage.messageId, + sourceMessage.threadId, + sourceMessage.user, + sourceMessage.date, + sourceMessage.forwardInfo, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + content, + sourceMessage.senderBot + ) + is FromChannelForumContentMessage -> FromChannelForumContentMessageImpl( + sourceMessage.chat, + sourceMessage.channel, + sourceMessage.messageId, + sourceMessage.threadId, + sourceMessage.date, + sourceMessage.forwardInfo, + sourceMessage.editDate, + sourceMessage.hasProtectedContent, + sourceMessage.replyTo, + sourceMessage.replyMarkup, + content, + sourceMessage.senderBot, + sourceMessage.authorSignature + ) + } +} diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index 0a9cfa3e83..293aaf0788 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -1038,16 +1038,16 @@ inline fun Message.requireChannelEventMessage(): ChannelEventMessage @PreviewFeature -inline fun Message.whenChannelMediaGroupMessage(block: (ChannelMediaGroupMessage) -> T) = +inline fun Message.whenChannelMediaGroupMessage(block: (ChannelMediaGroupMessage) -> T) = asChannelMediaGroupMessage()?.let(block) @PreviewFeature -inline fun Message.asChannelMediaGroupMessage(): ChannelMediaGroupMessage? = - this as? ChannelMediaGroupMessage +inline fun Message.asChannelMediaGroupMessage(): ChannelMediaGroupMessage? = + this as? ChannelMediaGroupMessage @PreviewFeature -inline fun Message.requireChannelMediaGroupMessage(): ChannelMediaGroupMessage = - this as ChannelMediaGroupMessage +inline fun Message.requireChannelMediaGroupMessage(): ChannelMediaGroupMessage = + this as ChannelMediaGroupMessage @PreviewFeature inline fun Message.whenCommonGroupEventMessage(block: (CommonGroupEventMessage) -> T) = @@ -1062,16 +1062,16 @@ inline fun Message.requireCommonGroupEventMessage(): CommonGroupEventMessage @PreviewFeature -inline fun Message.whenCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T) = +inline fun Message.whenCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T) = asCommonMediaGroupMessage()?.let(block) @PreviewFeature -inline fun Message.asCommonMediaGroupMessage(): CommonMediaGroupMessage? = - this as? CommonMediaGroupMessage +inline fun Message.asCommonMediaGroupMessage(): CommonMediaGroupMessage? = + this as? CommonMediaGroupMessage @PreviewFeature -inline fun Message.requireCommonMediaGroupMessage(): CommonMediaGroupMessage = - this as CommonMediaGroupMessage +inline fun Message.requireCommonMediaGroupMessage(): CommonMediaGroupMessage = + this as CommonMediaGroupMessage @PreviewFeature inline fun Message.whenCommonSupergroupEventMessage(block: (CommonSupergroupEventMessage) -> T) = @@ -1220,16 +1220,16 @@ inline fun Message.requireGroupContentMessage(): GroupContentMessage @PreviewFeature -inline fun Message.whenMediaGroupMessage(block: (MediaGroupMessage) -> T) = +inline fun Message.whenMediaGroupMessage(block: (MediaGroupMessage) -> T) = asMediaGroupMessage()?.let(block) @PreviewFeature -inline fun Message.asMediaGroupMessage(): MediaGroupMessage? = - this as? MediaGroupMessage +inline fun Message.asMediaGroupMessage(): MediaGroupMessage? = + this as? MediaGroupMessage @PreviewFeature -inline fun Message.requireMediaGroupMessage(): MediaGroupMessage = - this as MediaGroupMessage +inline fun Message.requireMediaGroupMessage(): MediaGroupMessage = + this as MediaGroupMessage @PreviewFeature inline fun Message.whenPossiblyEditedMessage(block: (PossiblyEditedMessage) -> T) = @@ -2836,26 +2836,26 @@ inline fun ResendableContent.asVenueContent(): VenueContent? = this as? VenueCon inline fun ResendableContent.requireVenueContent(): VenueContent = this as VenueContent @PreviewFeature -inline fun ResendableContent.whenAudioMediaGroupContent(block: (AudioMediaGroupContent) -> T) = +inline fun ResendableContent.whenAudioMediaGroupContent(block: (AudioMediaGroupPartContent) -> T) = asAudioMediaGroupContent()?.let(block) @PreviewFeature -inline fun ResendableContent.asAudioMediaGroupContent(): AudioMediaGroupContent? = this as? AudioMediaGroupContent +inline fun ResendableContent.asAudioMediaGroupContent(): AudioMediaGroupPartContent? = this as? AudioMediaGroupPartContent @PreviewFeature -inline fun ResendableContent.requireAudioMediaGroupContent(): AudioMediaGroupContent = this as AudioMediaGroupContent +inline fun ResendableContent.requireAudioMediaGroupContent(): AudioMediaGroupPartContent = this as AudioMediaGroupPartContent @PreviewFeature -inline fun ResendableContent.whenDocumentMediaGroupContent(block: (DocumentMediaGroupContent) -> T) = +inline fun ResendableContent.whenDocumentMediaGroupContent(block: (DocumentMediaGroupPartContent) -> T) = asDocumentMediaGroupContent()?.let(block) @PreviewFeature -inline fun ResendableContent.asDocumentMediaGroupContent(): DocumentMediaGroupContent? = - this as? DocumentMediaGroupContent +inline fun ResendableContent.asDocumentMediaGroupContent(): DocumentMediaGroupPartContent? = + this as? DocumentMediaGroupPartContent @PreviewFeature -inline fun ResendableContent.requireDocumentMediaGroupContent(): DocumentMediaGroupContent = - this as DocumentMediaGroupContent +inline fun ResendableContent.requireDocumentMediaGroupContent(): DocumentMediaGroupPartContent = + this as DocumentMediaGroupPartContent @PreviewFeature inline fun ResendableContent.whenMediaCollectionContent(block: (MediaCollectionContent) -> T) = @@ -2891,14 +2891,14 @@ inline fun ResendableContent.asMediaContent(): MediaContent? = this as? MediaCon inline fun ResendableContent.requireMediaContent(): MediaContent = this as MediaContent @PreviewFeature -inline fun ResendableContent.whenMediaGroupContent(block: (MediaGroupContent) -> T) = +inline fun ResendableContent.whenMediaGroupContent(block: (MediaGroupPartContent) -> T) = asMediaGroupContent()?.let(block) @PreviewFeature -inline fun ResendableContent.asMediaGroupContent(): MediaGroupContent? = this as? MediaGroupContent +inline fun ResendableContent.asMediaGroupContent(): MediaGroupPartContent? = this as? MediaGroupPartContent @PreviewFeature -inline fun ResendableContent.requireMediaGroupContent(): MediaGroupContent = this as MediaGroupContent +inline fun ResendableContent.requireMediaGroupContent(): MediaGroupPartContent = this as MediaGroupPartContent @PreviewFeature inline fun ResendableContent.whenMessageContent(block: (MessageContent) -> T) = asMessageContent()?.let(block) @@ -2910,14 +2910,14 @@ inline fun ResendableContent.asMessageContent(): MessageContent? = this as? Mess inline fun ResendableContent.requireMessageContent(): MessageContent = this as MessageContent @PreviewFeature -inline fun ResendableContent.whenVisualMediaGroupContent(block: (VisualMediaGroupContent) -> T) = +inline fun ResendableContent.whenVisualMediaGroupContent(block: (VisualMediaGroupPartContent) -> T) = asVisualMediaGroupContent()?.let(block) @PreviewFeature -inline fun ResendableContent.asVisualMediaGroupContent(): VisualMediaGroupContent? = this as? VisualMediaGroupContent +inline fun ResendableContent.asVisualMediaGroupContent(): VisualMediaGroupPartContent? = this as? VisualMediaGroupPartContent @PreviewFeature -inline fun ResendableContent.requireVisualMediaGroupContent(): VisualMediaGroupContent = this as VisualMediaGroupContent +inline fun ResendableContent.requireVisualMediaGroupContent(): VisualMediaGroupPartContent = this as VisualMediaGroupPartContent @PreviewFeature inline fun ResendableContent.whenAnimationContent(block: (AnimationContent) -> T) = asAnimationContent()?.let(block) 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 c2091ffb91..3f8b1f52ad 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 @@ -288,18 +288,18 @@ import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupCont import dev.inmo.tgbotapi.types.message.abstracts.UnknownMessageType import dev.inmo.tgbotapi.types.message.content.AnimationContent import dev.inmo.tgbotapi.types.message.content.AudioContent -import dev.inmo.tgbotapi.types.message.content.AudioMediaGroupContent +import dev.inmo.tgbotapi.types.message.content.AudioMediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.ContactContent import dev.inmo.tgbotapi.types.message.content.DiceContent import dev.inmo.tgbotapi.types.message.content.DocumentContent -import dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupContent +import dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.GameContent import dev.inmo.tgbotapi.types.message.content.InvoiceContent import dev.inmo.tgbotapi.types.message.content.LiveLocationContent import dev.inmo.tgbotapi.types.message.content.LocationContent import dev.inmo.tgbotapi.types.message.content.MediaCollectionContent import dev.inmo.tgbotapi.types.message.content.MediaContent -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MessageContent import dev.inmo.tgbotapi.types.message.content.PhotoContent import dev.inmo.tgbotapi.types.message.content.PollContent @@ -311,7 +311,7 @@ import dev.inmo.tgbotapi.types.message.content.TextedMediaContent import dev.inmo.tgbotapi.types.message.content.VenueContent import dev.inmo.tgbotapi.types.message.content.VideoContent import dev.inmo.tgbotapi.types.message.content.VideoNoteContent -import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupContent +import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.VoiceContent import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent import dev.inmo.tgbotapi.types.message.textsources.BoldTextSource @@ -737,15 +737,15 @@ public inline fun commonGroupEventMessageOrNull() ?.let(block) public inline fun WithUser.commonMediaGroupMessageOrNull(): - CommonMediaGroupMessage? = this as? - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage + CommonMediaGroupMessage? = this as? + dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage public inline fun WithUser.commonMediaGroupMessageOrThrow(): - CommonMediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage + CommonMediaGroupMessage = this as + dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage public inline fun - WithUser.ifCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T): T? + WithUser.ifCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T): T? = commonMediaGroupMessageOrNull() ?.let(block) public inline fun WithUser.commonSupergroupEventMessageOrNull(): @@ -2986,15 +2986,15 @@ public inline fun channelEventMessageOrNull() ?.let(block) public inline fun Message.channelMediaGroupMessageOrNull(): - ChannelMediaGroupMessage? = this as? - dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage + ChannelMediaGroupMessage? = this as? + dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage public inline fun Message.channelMediaGroupMessageOrThrow(): - ChannelMediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage + ChannelMediaGroupMessage = this as + dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage public inline fun - Message.ifChannelMediaGroupMessage(block: (ChannelMediaGroupMessage) -> T): + Message.ifChannelMediaGroupMessage(block: (ChannelMediaGroupMessage) -> T): T? = channelMediaGroupMessageOrNull() ?.let(block) public inline fun Message.commonGroupEventMessageOrNull(): CommonGroupEventMessage? = @@ -3010,15 +3010,15 @@ public inline fun commonGroupEventMessageOrNull() ?.let(block) public inline fun Message.commonMediaGroupMessageOrNull(): - CommonMediaGroupMessage? = this as? - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage + CommonMediaGroupMessage? = this as? + dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage public inline fun Message.commonMediaGroupMessageOrThrow(): - CommonMediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage + CommonMediaGroupMessage = this as + dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage public inline fun - Message.ifCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T): T? + Message.ifCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T): T? = commonMediaGroupMessageOrNull() ?.let(block) public inline fun Message.commonSupergroupEventMessageOrNull(): @@ -3325,15 +3325,15 @@ public inline fun Message.ifCommonForumContentMessage(block: (CommonForumContentMessage) -> T): T? = commonForumContentMessageOrNull() ?.let(block) -public inline fun Message.mediaGroupMessageOrNull(): MediaGroupMessage? = this +public inline fun Message.mediaGroupMessageOrNull(): MediaGroupMessage? = this as? - dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage + dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -public inline fun Message.mediaGroupMessageOrThrow(): MediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage +public inline fun Message.mediaGroupMessageOrThrow(): MediaGroupMessage = this as + dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage public inline fun - Message.ifMediaGroupMessage(block: (MediaGroupMessage) -> T): T? = + Message.ifMediaGroupMessage(block: (MediaGroupMessage) -> T): T? = mediaGroupMessageOrNull() ?.let(block) public inline fun Message.unknownMessageTypeOrNull(): UnknownMessageType? = this as? @@ -3459,33 +3459,33 @@ public inline fun ResendableContent.mediaContentOrThrow(): MediaContent = this a public inline fun ResendableContent.ifMediaContent(block: (MediaContent) -> T): T? = mediaContentOrNull() ?.let(block) -public inline fun ResendableContent.audioMediaGroupContentOrNull(): AudioMediaGroupContent? = this - as? dev.inmo.tgbotapi.types.message.content.AudioMediaGroupContent +public inline fun ResendableContent.audioMediaGroupContentOrNull(): AudioMediaGroupPartContent? = this + as? dev.inmo.tgbotapi.types.message.content.AudioMediaGroupPartContent -public inline fun ResendableContent.audioMediaGroupContentOrThrow(): AudioMediaGroupContent = this - as dev.inmo.tgbotapi.types.message.content.AudioMediaGroupContent +public inline fun ResendableContent.audioMediaGroupContentOrThrow(): AudioMediaGroupPartContent = this + as dev.inmo.tgbotapi.types.message.content.AudioMediaGroupPartContent public inline fun - ResendableContent.ifAudioMediaGroupContent(block: (AudioMediaGroupContent) -> T): T? = + ResendableContent.ifAudioMediaGroupContent(block: (AudioMediaGroupPartContent) -> T): T? = audioMediaGroupContentOrNull() ?.let(block) -public inline fun ResendableContent.documentMediaGroupContentOrNull(): DocumentMediaGroupContent? = - this as? dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupContent +public inline fun ResendableContent.documentMediaGroupContentOrNull(): DocumentMediaGroupPartContent? = + this as? dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupPartContent -public inline fun ResendableContent.documentMediaGroupContentOrThrow(): DocumentMediaGroupContent = - this as dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupContent +public inline fun ResendableContent.documentMediaGroupContentOrThrow(): DocumentMediaGroupPartContent = + this as dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupPartContent public inline fun - ResendableContent.ifDocumentMediaGroupContent(block: (DocumentMediaGroupContent) -> T): T? = + ResendableContent.ifDocumentMediaGroupContent(block: (DocumentMediaGroupPartContent) -> T): T? = documentMediaGroupContentOrNull() ?.let(block) -public inline fun ResendableContent.mediaGroupContentOrNull(): MediaGroupContent? = this as? - dev.inmo.tgbotapi.types.message.content.MediaGroupContent +public inline fun ResendableContent.mediaGroupContentOrNull(): MediaGroupPartContent? = this as? + dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -public inline fun ResendableContent.mediaGroupContentOrThrow(): MediaGroupContent = this as - dev.inmo.tgbotapi.types.message.content.MediaGroupContent +public inline fun ResendableContent.mediaGroupContentOrThrow(): MediaGroupPartContent = this as + dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -public inline fun ResendableContent.ifMediaGroupContent(block: (MediaGroupContent) -> T): T? = +public inline fun ResendableContent.ifMediaGroupContent(block: (MediaGroupPartContent) -> T): T? = mediaGroupContentOrNull() ?.let(block) public inline fun ResendableContent.textedMediaContentOrNull(): TextedMediaContent? = this as? @@ -3497,14 +3497,14 @@ public inline fun ResendableContent.textedMediaContentOrThrow(): TextedMediaCont public inline fun ResendableContent.ifTextedMediaContent(block: (TextedMediaContent) -> T): T? = textedMediaContentOrNull() ?.let(block) -public inline fun ResendableContent.visualMediaGroupContentOrNull(): VisualMediaGroupContent? = this - as? dev.inmo.tgbotapi.types.message.content.VisualMediaGroupContent +public inline fun ResendableContent.visualMediaGroupContentOrNull(): VisualMediaGroupPartContent? = this + as? dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent -public inline fun ResendableContent.visualMediaGroupContentOrThrow(): VisualMediaGroupContent = this - as dev.inmo.tgbotapi.types.message.content.VisualMediaGroupContent +public inline fun ResendableContent.visualMediaGroupContentOrThrow(): VisualMediaGroupPartContent = this + as dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent public inline fun - ResendableContent.ifVisualMediaGroupContent(block: (VisualMediaGroupContent) -> T): T? = + ResendableContent.ifVisualMediaGroupContent(block: (VisualMediaGroupPartContent) -> T): T? = visualMediaGroupContentOrNull() ?.let(block) public inline fun ResendableContent.animationContentOrNull(): AnimationContent? = this as? diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt index 05cd19bd49..fa337b2360 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt @@ -3,7 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils import dev.inmo.tgbotapi.types.message.abstracts.* -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MessageContent inline fun ContentMessage<*>.withContentOrNull() = if (content is T) { this as ContentMessage } else { null } @@ -37,5 +37,5 @@ inline fun AnonymousGroupContentMessage<*>.withCont inline fun CommonGroupContentMessage<*>.withContentOrNull() = if (content is T) { this as CommonGroupContentMessage } else { null } inline fun CommonGroupContentMessage<*>.withContentOrThrow() = withContentOrNull()!! -inline fun MediaGroupMessage<*>.withContentOrNull() = if (content is T) { this as MediaGroupMessage } else { null } -inline fun MediaGroupMessage<*>.withContentOrThrow() = withContentOrNull()!! +inline fun MediaGroupMessage<*>.withContentOrNull() = if (content is T) { this as MediaGroupMessage } else { null } +inline fun MediaGroupMessage<*>.withContentOrThrow() = withContentOrNull()!! diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt index 80dea0da67..9f632f078f 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt @@ -3,7 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils import dev.inmo.tgbotapi.types.message.abstracts.* -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MessageContent inline fun ContentMessage<*>.withContent() = if (content is T) { this as ContentMessage } else { null } @@ -37,5 +37,5 @@ inline fun AnonymousGroupContentMessage<*>.requireW inline fun CommonGroupContentMessage<*>.withContent() = if (content is T) { this as CommonGroupContentMessage } else { null } inline fun CommonGroupContentMessage<*>.requireWithContent() = withContent()!! -inline fun MediaGroupMessage<*>.withContent() = if (content is T) { this as MediaGroupMessage } else { null } -inline fun MediaGroupMessage<*>.requireWithContent() = withContent()!! +inline fun MediaGroupMessage<*>.withContent() = if (content is T) { this as MediaGroupMessage } else { null } +inline fun MediaGroupMessage<*>.requireWithContent() = withContent()!! diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt index 4b3956f4ac..53d52cf338 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt @@ -31,7 +31,7 @@ inline fun Flow.filterContent ): Flow> = asContentMessagesFlow().mapNotNull(filterForContentMessage()) @RiskFeature("This method is low-level") -inline fun Flow.filterMediaGroupMessages( +inline fun Flow.filterMediaGroupMessages( ): Flow>> = map { it.data.mapNotNull { message -> if (message.content is T) { @@ -69,7 +69,7 @@ inline fun FlowsUpdatesFilter.filterContentMessages( */ @Suppress("UNCHECKED_CAST") @RiskFeature(lowLevelRiskFeatureMessage) -inline fun FlowsUpdatesFilter.filterMediaGroupMessages( +inline fun FlowsUpdatesFilter.filterMediaGroupMessages( scopeToIncludeChannels: CoroutineScope? = null ): Flow>> { return (scopeToIncludeChannels ?.let { scope -> @@ -212,10 +212,10 @@ fun FlowsUpdatesFilter.voiceMessages( ) = filterContentMessages(scopeToIncludeChannels) -fun Flow.mediaGroupMessages() = filterMediaGroupMessages() +fun Flow.mediaGroupMessages() = filterMediaGroupMessages() fun FlowsUpdatesFilter.mediaGroupMessages( scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) +) = filterMediaGroupMessages(scopeToIncludeChannels) fun Flow.mediaGroupPhotosMessages() = filterMediaGroupMessages() fun FlowsUpdatesFilter.mediaGroupPhotosMessages( @@ -227,10 +227,10 @@ fun FlowsUpdatesFilter.mediaGroupVideosMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterMediaGroupMessages(scopeToIncludeChannels) -fun Flow.mediaGroupVisualMessages() = filterMediaGroupMessages() +fun Flow.mediaGroupVisualMessages() = filterMediaGroupMessages() fun FlowsUpdatesFilter.mediaGroupVisualMessages( scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) +) = filterMediaGroupMessages(scopeToIncludeChannels) fun Flow.mediaGroupAudioMessages() = filterMediaGroupMessages() fun FlowsUpdatesFilter.mediaGroupAudioMessages( diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt index f5c106d568..0d5866e68d 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt @@ -5,14 +5,14 @@ import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.message.ForwardInfo import dev.inmo.tgbotapi.types.message.abstracts.* -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate -val List>.forwardInfo: ForwardInfo? +val List>.forwardInfo: ForwardInfo? get() = firstOrNull() ?.forwardInfo -val List>.replyTo: Message? +val List>.replyTo: Message? get() = firstOrNull() ?.replyTo -val List>.chat: Chat? +val List>.chat: Chat? get() = firstOrNull() ?.chat val List>.mediaGroupId: MediaGroupIdentifier? get() = firstOrNull() ?.mediaGroupId @@ -26,12 +26,12 @@ val SentMediaGroupUpdate.chat: Chat val SentMediaGroupUpdate.mediaGroupId: MediaGroupIdentifier get() = data.mediaGroupId!! -fun List>.createResend( +fun List>.createResend( chatId: ChatId, disableNotification: Boolean = false, protectContent: Boolean = false, replyTo: MessageId? = null -) = SendMediaGroup( +) = SendMediaGroup( chatId, map { it.content.toMediaGroupMemberTelegramMedia() }, disableNotification, @@ -39,7 +39,7 @@ fun List>.createResend( replyTo ) -fun List>.createResend( +fun List>.createResend( chat: Chat, disableNotification: Boolean = false, protectContent: Boolean = false, From b32ce88a97a0142b169485dff164a115c8d3193c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 8 Nov 2022 01:51:27 +0600 Subject: [PATCH 2/5] fixes --- .../api/InternalUtils/UpdatesUtils.kt | 25 ------ .../extensions/api/send/CopyMessages.kt | 1 - .../extensions/api/utils/UpdatesHandling.kt | 1 - .../expectations/WaitEditedContentMessage.kt | 1 - .../expectations/WaitMediaGroup.kt | 1 - .../expectations/WaitMediaGroupMessages.kt | 1 - .../triggers_handling/MediaGroupTriggers.kt | 1 - .../MediaGroupMarkerFactories.kt | 1 - .../requests/send/media/SendMediaGroup.kt | 12 +-- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 1 + .../message/ChannelContentMessageImpl.kt | 3 +- .../types/message/ChannelMediaGroupMessage.kt | 25 ------ .../types/message/CommonMediaGroupMessage.kt | 24 ------ .../tgbotapi/types/message/GroupMessages.kt | 21 +++-- .../message/PrivateContentMessageImpl.kt | 3 +- .../inmo/tgbotapi/types/message/RawMessage.kt | 79 +++++++------------ .../types/message/abstracts/CommonMessage.kt | 1 + .../message/abstracts/MediaGroupMessage.kt | 10 --- .../abstracts/PossiblyMediaGroupMessage.kt | 8 ++ .../types/update/ChannelPostUpdate.kt | 4 +- .../tgbotapi/types/update/MessageUpdate.kt | 4 +- .../update/abstracts/BaseSentMessageUpdate.kt | 6 +- .../ChannelPostMediaGroupUpdate.kt | 13 --- .../EditChannelPostMediaGroupUpdate.kt | 13 --- .../EditMessageMediaGroupUpdate.kt | 13 --- .../update/media_group/MediaGroupUpdate.kt | 24 ------ .../media_group/MessageMediaGroupUpdate.kt | 13 --- .../MediaGroupContentMessageCreator.kt | 38 +++++---- .../tgbotapi/extensions/utils/ClassCasts.kt | 1 - .../extensions/utils/ClassCastsNew.kt | 3 - .../extensions/utils/ContentCastsNew.kt | 3 - .../extensions/utils/updates/UpdatesUtils.kt | 67 ++++++---------- .../updates/retrieving/MediaGroupsIncluder.kt | 6 +- 33 files changed, 120 insertions(+), 307 deletions(-) delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblyMediaGroupMessage.kt delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt delete mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt index 01e9f0ff62..b559d65f18 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt @@ -2,31 +2,6 @@ package dev.inmo.tgbotapi.extensions.api.InternalUtils import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.UpdateIdentifier -import dev.inmo.tgbotapi.types.message.AnonymousForumContentMessageImpl -import dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl -import dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl -import dev.inmo.tgbotapi.types.message.CommonForumContentMessageImpl -import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl -import dev.inmo.tgbotapi.types.message.ConnectedFromChannelGroupContentMessageImpl -import dev.inmo.tgbotapi.types.message.FromChannelForumContentMessageImpl -import dev.inmo.tgbotapi.types.message.PrivateContentMessageImpl -import dev.inmo.tgbotapi.types.message.UnconnectedFromChannelGroupContentMessageImpl -import dev.inmo.tgbotapi.types.message.abstracts.AnonymousForumContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.AnonymousGroupContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.ChannelContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.CommonForumContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage -import dev.inmo.tgbotapi.types.message.abstracts.ConnectedFromChannelGroupContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.FromChannelForumContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage -import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupContentMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent -import dev.inmo.tgbotapi.types.message.content.MediaGroupContent -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.abstracts.* import dev.inmo.tgbotapi.types.update.media_group.* diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt index bbdaa00039..2ab20d94bc 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt @@ -9,7 +9,6 @@ import dev.inmo.tgbotapi.types.MessageId import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.message.ParseMode import dev.inmo.tgbotapi.types.chat.Chat -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt index 54845e4c3a..16e695fe1f 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.extensions.api.utils import dev.inmo.tgbotapi.extensions.api.InternalUtils.convertWithMediaGroupUpdates -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt index f7c201374f..9b39b1f911 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt @@ -7,7 +7,6 @@ import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate import dev.inmo.tgbotapi.utils.RiskFeature diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt index bc614d6962..5e38f5555e 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt @@ -3,7 +3,6 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.requests.abstracts.Request -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt index 889e63de09..828a582942 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt @@ -4,7 +4,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.requests.abstracts.Request -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt index 29a2cdad86..5394ec31b4 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt @@ -8,7 +8,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMediaGroupMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.utils.PreviewFeature diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt index 233d3f3a3b..e4e0a5838b 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage object ByChatMediaGroupMarkerFactory : MarkerFactory>, Any> { override suspend fun invoke(data: List>) = data.chat ?: error("Data must not be empty") diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt index 34e376a1c4..cdca64dc56 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt @@ -6,7 +6,6 @@ import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest import dev.inmo.tgbotapi.requests.send.media.base.* import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.media.* -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializerClass import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent @@ -15,13 +14,16 @@ import dev.inmo.tgbotapi.types.message.content.AudioContent import dev.inmo.tgbotapi.types.message.content.DocumentContent import dev.inmo.tgbotapi.types.message.content.MediaGroupContent import dev.inmo.tgbotapi.utils.* -import dev.inmo.tgbotapi.utils.extensions.asMediaGroupContent +import dev.inmo.tgbotapi.utils.extensions.asMediaGroupMessage import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.buildJsonArray +import kotlinx.serialization.json.jsonPrimitive const val rawSendingMediaGroupsWarning = "Media groups contains restrictions related to combinations of media" + " types. Currently it is possible to combine photo + video OR audio OR documents" @@ -35,7 +37,7 @@ fun SendMediaGroup( protectContent: Boolean = false, replyToMessageId: MessageId? = null, allowSendingWithoutReply: Boolean? = null -): Request>> { +): Request> { if (media.size !in mediaCountInMediaGroup) { throwRangeError("Count of members in media group", mediaCountInMediaGroup, media.size) } @@ -68,7 +70,7 @@ fun SendMediaGroup( data, SendMediaGroupFiles(files) ) - }) as Request>> + }) as Request> } /** @@ -126,7 +128,7 @@ private object MessagesListSerializer: KSerializer { val messages = serializer.deserialize(decoder) - return messages.asMediaGroupContent() + return messages.asMediaGroupMessage() } override fun serialize(encoder: Encoder, value: PossiblySentViaBotCommonMessage) { 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 bceec5ef4c..a9ff556602 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 @@ -151,6 +151,7 @@ const val chatIdField = "chat_id" const val senderChatIdField = "sender_chat_id" const val messageIdField = "message_id" const val messageThreadIdField = "message_thread_id" +const val mediaGroupIdField = "media_group_id" const val updateIdField = "update_id" const val fromChatIdField = "from_chat_id" const val disableWebPagePreviewField = "disable_web_page_preview" 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 a9ff456ec5..a53ce0f3c9 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 @@ -19,5 +19,6 @@ data class ChannelContentMessageImpl( override val replyTo: Message?, override val replyMarkup: InlineKeyboardMarkup?, override val senderBot: CommonBot?, - override val authorSignature: AuthorSignature? + override val authorSignature: AuthorSignature?, + override val mediaGroupId: MediaGroupIdentifier?, ) : ChannelContentMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt deleted file mode 100644 index 4126168856..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelMediaGroupMessage.kt +++ /dev/null @@ -1,25 +0,0 @@ -package dev.inmo.tgbotapi.types.message - -import com.soywiz.klock.DateTime -import dev.inmo.tgbotapi.types.MediaGroupIdentifier -import dev.inmo.tgbotapi.types.MessageId -import dev.inmo.tgbotapi.types.MessageThreadId -import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup -import dev.inmo.tgbotapi.types.chat.Chat -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.abstracts.Message -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent - -data class ChannelMediaGroupMessage( - override val messageId: MessageId, - override val threadId: MessageThreadId?, - override val chat: Chat, - override val date: DateTime, - override val mediaGroupId: MediaGroupIdentifier, - override val content: T, - override val editDate: DateTime?, - override val hasProtectedContent: Boolean, - override val forwardInfo: ForwardInfo?, - override val replyTo: Message?, - override val replyMarkup: InlineKeyboardMarkup? -) : MediaGroupMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt deleted file mode 100644 index f19fa0e779..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonMediaGroupMessage.kt +++ /dev/null @@ -1,24 +0,0 @@ -package dev.inmo.tgbotapi.types.message - -import com.soywiz.klock.DateTime -import dev.inmo.tgbotapi.types.* -import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup -import dev.inmo.tgbotapi.types.chat.Chat -import dev.inmo.tgbotapi.types.chat.User -import dev.inmo.tgbotapi.types.message.abstracts.* -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent - -data class CommonMediaGroupMessage( - override val messageId: MessageId, - override val threadId: MessageThreadId?, - override val from: User, - override val chat: Chat, - override val date: DateTime, - override val mediaGroupId: MediaGroupIdentifier, - override val content: T, - override val editDate: DateTime?, - override val hasProtectedContent: Boolean, - override val forwardInfo: ForwardInfo?, - override val replyTo: Message?, - override val replyMarkup: InlineKeyboardMarkup? -) : MediaGroupMessage, FromUserMessage 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 f297b81df1..a3fd4d4dcc 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 @@ -21,7 +21,8 @@ data class ConnectedFromChannelGroupContentMessageImpl( override val replyMarkup: InlineKeyboardMarkup?, override val content: T, override val senderBot: CommonBot?, - override val authorSignature: AuthorSignature? + override val authorSignature: AuthorSignature?, + override val mediaGroupId: MediaGroupIdentifier?, ) : ConnectedFromChannelGroupContentMessage data class UnconnectedFromChannelGroupContentMessageImpl( @@ -36,7 +37,8 @@ data class UnconnectedFromChannelGroupContentMessageImpl( override val replyMarkup: InlineKeyboardMarkup?, override val content: T, override val senderBot: CommonBot?, - override val authorSignature: AuthorSignature? + override val authorSignature: AuthorSignature?, + override val mediaGroupId: MediaGroupIdentifier?, ) : UnconnectedFromChannelGroupContentMessage data class AnonymousGroupContentMessageImpl( @@ -50,7 +52,8 @@ data class AnonymousGroupContentMessageImpl( override val replyMarkup: InlineKeyboardMarkup?, override val content: T, override val senderBot: CommonBot?, - override val authorSignature: AuthorSignature? + override val authorSignature: AuthorSignature?, + override val mediaGroupId: MediaGroupIdentifier?, ) : AnonymousGroupContentMessage data class CommonGroupContentMessageImpl( @@ -64,7 +67,8 @@ data class CommonGroupContentMessageImpl( override val replyTo: Message?, override val replyMarkup: InlineKeyboardMarkup?, override val content: T, - override val senderBot: CommonBot? + override val senderBot: CommonBot?, + override val mediaGroupId: MediaGroupIdentifier?, ) : CommonGroupContentMessage data class FromChannelForumContentMessageImpl( @@ -80,7 +84,8 @@ data class FromChannelForumContentMessageImpl( override val replyMarkup: InlineKeyboardMarkup?, override val content: T, override val senderBot: CommonBot?, - override val authorSignature: AuthorSignature? + override val authorSignature: AuthorSignature?, + override val mediaGroupId: MediaGroupIdentifier?, ) : FromChannelForumContentMessage data class AnonymousForumContentMessageImpl( @@ -95,7 +100,8 @@ data class AnonymousForumContentMessageImpl( override val replyMarkup: InlineKeyboardMarkup?, override val content: T, override val senderBot: CommonBot?, - override val authorSignature: AuthorSignature? + override val authorSignature: AuthorSignature?, + override val mediaGroupId: MediaGroupIdentifier?, ) : AnonymousForumContentMessage data class CommonForumContentMessageImpl( @@ -110,5 +116,6 @@ data class CommonForumContentMessageImpl( override val replyTo: Message?, override val replyMarkup: InlineKeyboardMarkup?, override val content: T, - override val senderBot: CommonBot? + override val senderBot: CommonBot?, + override val mediaGroupId: MediaGroupIdentifier?, ) : CommonForumContentMessage 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 300b2afc3f..aac98484a3 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 @@ -21,5 +21,6 @@ data class PrivateContentMessageImpl( override val forwardInfo: ForwardInfo?, override val replyTo: Message?, override val replyMarkup: InlineKeyboardMarkup?, - override val senderBot: CommonBot? + override val senderBot: CommonBot?, + override val mediaGroupId: MediaGroupIdentifier?, ) : PrivateContentMessage 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 65f2635202..2e63c28a71 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 @@ -269,45 +269,7 @@ internal data class RawMessage( ) else -> error("Expected one of the public chats, but was $chat (in extracting of chat event message)") } - } ?: content?.let { content -> - media_group_id?.let { - val checkedContent = when (content) { - is PhotoContent -> content - is VideoContent -> content - is AudioContent -> content - is DocumentContent -> content - else -> error("Unsupported content for media group") - } - when (from) { - null -> ChannelMediaGroupMessage( - messageId, - messageThreadId, - chat, - date.asDate, - it, - checkedContent, - edit_date?.asDate, - has_protected_content == true, - forwarded, - reply_to_message?.asMessage, - reply_markup - ) - else -> CommonMediaGroupMessage( - messageId, - messageThreadId, - from, - chat, - date.asDate, - it, - checkedContent, - edit_date?.asDate, - has_protected_content == true, - forwarded, - reply_to_message?.asMessage, - reply_markup - ) - } - } ?: when (chat) { + } ?: content?.let { content -> when (chat) { is PublicChat -> when (chat) { is ChannelChat -> ChannelContentMessageImpl( messageId, @@ -320,7 +282,8 @@ internal data class RawMessage( reply_to_message?.asMessage, reply_markup, via_bot, - author_signature + author_signature, + media_group_id ) is ForumChat -> if (messageThreadId != null) { when (sender_chat) { @@ -337,7 +300,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) is GroupChat -> AnonymousForumContentMessageImpl( chat, @@ -351,7 +315,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) null -> CommonForumContentMessageImpl( chat, @@ -365,7 +330,8 @@ internal data class RawMessage( reply_to_message ?.asMessage, reply_markup, content, - via_bot + via_bot, + media_group_id ) } } else { @@ -383,7 +349,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) } else { UnconnectedFromChannelGroupContentMessageImpl( @@ -398,7 +365,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) } is GroupChat -> AnonymousGroupContentMessageImpl( @@ -412,7 +380,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) null -> CommonGroupContentMessageImpl( chat, @@ -425,7 +394,8 @@ internal data class RawMessage( reply_to_message ?.asMessage, reply_markup, content, - via_bot + via_bot, + media_group_id ) } } @@ -443,7 +413,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) } else { UnconnectedFromChannelGroupContentMessageImpl( @@ -458,7 +429,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) } is GroupChat -> AnonymousGroupContentMessageImpl( @@ -472,7 +444,8 @@ internal data class RawMessage( reply_markup, content, via_bot, - author_signature + author_signature, + media_group_id ) null -> CommonGroupContentMessageImpl( chat, @@ -485,7 +458,8 @@ internal data class RawMessage( reply_to_message ?.asMessage, reply_markup, content, - via_bot + via_bot, + media_group_id ) } } @@ -500,7 +474,8 @@ internal data class RawMessage( forwarded, reply_to_message?.asMessage, reply_markup, - via_bot + via_bot, + media_group_id ) else -> error("Unknown type of chat: $chat") } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt index 2068ab6c33..7adf185282 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/CommonMessage.kt @@ -7,4 +7,5 @@ sealed interface CommonMessage : Message, PossiblyEditedMessage, PossiblyReplyMessage, PossiblyMarkedUp, + PossiblyMediaGroupMessage, ContentMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt deleted file mode 100644 index 9555501955..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/MediaGroupMessage.kt +++ /dev/null @@ -1,10 +0,0 @@ -package dev.inmo.tgbotapi.types.message.abstracts - -import dev.inmo.tgbotapi.types.MediaGroupIdentifier -import dev.inmo.tgbotapi.types.MessageThreadId -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent - -interface MediaGroupMessage : CommonMessage { - val mediaGroupId: MediaGroupIdentifier - val threadId: MessageThreadId? -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblyMediaGroupMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblyMediaGroupMessage.kt new file mode 100644 index 0000000000..fa865da83b --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/PossiblyMediaGroupMessage.kt @@ -0,0 +1,8 @@ +package dev.inmo.tgbotapi.types.message.abstracts + +import dev.inmo.tgbotapi.types.MediaGroupIdentifier +import dev.inmo.tgbotapi.types.message.content.MessageContent + +interface PossiblyMediaGroupMessage : ContentMessage { + val mediaGroupId: MediaGroupIdentifier? +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/ChannelPostUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/ChannelPostUpdate.kt index 23a14e9dcf..b5cbaa55b8 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/ChannelPostUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/ChannelPostUpdate.kt @@ -7,4 +7,6 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate data class ChannelPostUpdate( override val updateId: UpdateIdentifier, override val data: Message -) : BaseSentMessageUpdate +) : BaseSentMessageUpdate { + override fun copy(newData: Message): BaseSentMessageUpdate = copy(updateId, newData) +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MessageUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MessageUpdate.kt index 86d02a25c4..9585386627 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MessageUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MessageUpdate.kt @@ -7,4 +7,6 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate data class MessageUpdate( override val updateId: UpdateIdentifier, override val data: Message -) : BaseSentMessageUpdate +) : BaseSentMessageUpdate { + override fun copy(newData: Message) = copy(updateId, newData) +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/BaseSentMessageUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/BaseSentMessageUpdate.kt index bc8493432f..65efc5e9eb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/BaseSentMessageUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/BaseSentMessageUpdate.kt @@ -1,3 +1,7 @@ package dev.inmo.tgbotapi.types.update.abstracts -interface BaseSentMessageUpdate : BaseMessageUpdate +import dev.inmo.tgbotapi.types.message.abstracts.Message + +interface BaseSentMessageUpdate : BaseMessageUpdate { + fun copy(newData: Message): BaseSentMessageUpdate +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt deleted file mode 100644 index 21e6f13a0d..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/ChannelPostMediaGroupUpdate.kt +++ /dev/null @@ -1,13 +0,0 @@ -package dev.inmo.tgbotapi.types.update.media_group - -import dev.inmo.tgbotapi.types.UpdateIdentifier -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate - -data class ChannelPostMediaGroupUpdate( - override val origins: List -) : SentMediaGroupUpdate { - override val updateId: UpdateIdentifier = origins.last().updateId - override val data: List> = origins.mapNotNull { it.data as? MediaGroupMessage } -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt deleted file mode 100644 index 9af30060a1..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditChannelPostMediaGroupUpdate.kt +++ /dev/null @@ -1,13 +0,0 @@ -package dev.inmo.tgbotapi.types.update.media_group - -import dev.inmo.tgbotapi.types.UpdateIdentifier -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -import dev.inmo.tgbotapi.types.update.EditChannelPostUpdate - -data class EditChannelPostMediaGroupUpdate( - override val origin: EditChannelPostUpdate -) : EditMediaGroupUpdate { - override val updateId: UpdateIdentifier = origin.updateId - override val data: MediaGroupMessage = origin.data as MediaGroupMessage -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt deleted file mode 100644 index ebed49e6c5..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/EditMessageMediaGroupUpdate.kt +++ /dev/null @@ -1,13 +0,0 @@ -package dev.inmo.tgbotapi.types.update.media_group - -import dev.inmo.tgbotapi.types.UpdateIdentifier -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -import dev.inmo.tgbotapi.types.update.EditMessageUpdate - -data class EditMessageMediaGroupUpdate( - override val origin: EditMessageUpdate -) : EditMediaGroupUpdate { - override val updateId: UpdateIdentifier = origin.updateId - override val data: MediaGroupMessage = origin.data as MediaGroupMessage -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt deleted file mode 100644 index 0b8fd8ce15..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MediaGroupUpdate.kt +++ /dev/null @@ -1,24 +0,0 @@ -package dev.inmo.tgbotapi.types.update.media_group - -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -import dev.inmo.tgbotapi.types.update.abstracts.* - -/** - * By default there is no instances of objects which could be deserialized from raw updates. If you want to get objects - * with this type, you should use something like [dev.inmo.tgbotapi.extensions.api.SetWebhookKt.includeWebhookInRoute] - * - * @see dev.inmo.tgbotapi.extensions.api.SetWebhookKt.includeWebhookInRoute - * @see dev.inmo.tgbotapi.extensions.api.updates.UpdatesPollingKt.startGettingOfUpdates - */ -sealed interface MediaGroupUpdate : Update - -sealed interface SentMediaGroupUpdate: MediaGroupUpdate { - override val data: List> - val origins: List -} - -sealed interface EditMediaGroupUpdate : BaseEditMessageUpdate, MediaGroupUpdate { - override val data: MediaGroupMessage - val origin: BaseMessageUpdate -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt deleted file mode 100644 index d36b7f50d8..0000000000 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/media_group/MessageMediaGroupUpdate.kt +++ /dev/null @@ -1,13 +0,0 @@ -package dev.inmo.tgbotapi.types.update.media_group - -import dev.inmo.tgbotapi.types.UpdateIdentifier -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate - -data class MessageMediaGroupUpdate( - override val origins: List -) : SentMediaGroupUpdate { - override val updateId: UpdateIdentifier = origins.last().updateId - override val data: List> = origins.mapNotNull { it.data as? MediaGroupMessage } -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt index 06cb6aaaaa..e2524c74da 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt @@ -18,7 +18,6 @@ import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.ConnectedFromChannelGroupContentMessage import dev.inmo.tgbotapi.types.message.abstracts.FromChannelForumContentMessage -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage import dev.inmo.tgbotapi.types.message.abstracts.UnconnectedFromChannelGroupContentMessage @@ -28,15 +27,13 @@ import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.utils.RiskFeature @RiskFeature("This API is experimental and can be changed without any notice, use with caution") -fun List>.asMediaGroupContent( - mediaGroupIdentifier: MediaGroupIdentifier -): PossiblySentViaBotCommonMessage { +fun List>.asMediaGroupMessage(): PossiblySentViaBotCommonMessage { + val sourceMessage = first() val content = MediaGroupContent( map { MediaGroupCollectionContent.PartWrapper(it.messageId, it.content, it) }, - mediaGroupIdentifier + sourceMessage.mediaGroupId ?: error("Can't create media group message with the first message without media group id") ) - return when (val sourceMessage = first()) { - is MediaGroupMessage -> TODO() + return when (sourceMessage) { is ChannelContentMessage -> ChannelContentMessageImpl( sourceMessage.messageId, sourceMessage.chat, @@ -48,7 +45,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyTo, sourceMessage.replyMarkup, sourceMessage.senderBot, - sourceMessage.authorSignature + sourceMessage.authorSignature, + sourceMessage.mediaGroupId ) is PrivateContentMessage -> PrivateContentMessageImpl( sourceMessage.messageId, @@ -61,7 +59,8 @@ fun List>.asMediaGroupContent( sourceMessage.forwardInfo, sourceMessage.replyTo, sourceMessage.replyMarkup, - sourceMessage.senderBot + sourceMessage.senderBot, + sourceMessage.mediaGroupId ) is AnonymousGroupContentMessage -> AnonymousGroupContentMessageImpl( sourceMessage.chat, @@ -74,7 +73,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyMarkup, content, sourceMessage.senderBot, - sourceMessage.authorSignature + sourceMessage.authorSignature, + sourceMessage.mediaGroupId ) is CommonGroupContentMessage -> CommonGroupContentMessageImpl( sourceMessage.chat, @@ -87,7 +87,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyTo, sourceMessage.replyMarkup, content, - sourceMessage.senderBot + sourceMessage.senderBot, + sourceMessage.mediaGroupId ) is ConnectedFromChannelGroupContentMessage -> ConnectedFromChannelGroupContentMessageImpl( sourceMessage.chat, @@ -101,7 +102,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyMarkup, content, sourceMessage.senderBot, - sourceMessage.authorSignature + sourceMessage.authorSignature, + sourceMessage.mediaGroupId ) is UnconnectedFromChannelGroupContentMessage -> UnconnectedFromChannelGroupContentMessageImpl( sourceMessage.chat, @@ -115,7 +117,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyMarkup, content, sourceMessage.senderBot, - sourceMessage.authorSignature + sourceMessage.authorSignature, + sourceMessage.mediaGroupId ) is AnonymousForumContentMessage -> AnonymousForumContentMessageImpl( sourceMessage.chat, @@ -129,7 +132,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyMarkup, content, sourceMessage.senderBot, - sourceMessage.authorSignature + sourceMessage.authorSignature, + sourceMessage.mediaGroupId ) is CommonForumContentMessage -> CommonForumContentMessageImpl( sourceMessage.chat, @@ -143,7 +147,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyTo, sourceMessage.replyMarkup, content, - sourceMessage.senderBot + sourceMessage.senderBot, + sourceMessage.mediaGroupId ) is FromChannelForumContentMessage -> FromChannelForumContentMessageImpl( sourceMessage.chat, @@ -158,7 +163,8 @@ fun List>.asMediaGroupContent( sourceMessage.replyMarkup, content, sourceMessage.senderBot, - sourceMessage.authorSignature + sourceMessage.authorSignature, + sourceMessage.mediaGroupId ) } } diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index 293aaf0788..a2e1c1dfba 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -34,7 +34,6 @@ import dev.inmo.tgbotapi.types.message.ChatEvents.LeftChatMember import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.* import dev.inmo.tgbotapi.types.message.ChatEvents.voice.* import dev.inmo.tgbotapi.types.message.abstracts.* -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent import dev.inmo.tgbotapi.types.message.textsources.* 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 c12abfc23c..aa69edf531 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 @@ -221,7 +221,6 @@ import dev.inmo.tgbotapi.types.message.AnonymousForumContentMessageImpl import dev.inmo.tgbotapi.types.message.AnonymousGroupContentMessageImpl import dev.inmo.tgbotapi.types.message.ChannelContentMessageImpl import dev.inmo.tgbotapi.types.message.ChannelEventMessage -import dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage import dev.inmo.tgbotapi.types.message.ChatEvents.ChannelChatCreated import dev.inmo.tgbotapi.types.message.ChatEvents.DeleteChatPhoto import dev.inmo.tgbotapi.types.message.ChatEvents.GroupChatCreated @@ -255,7 +254,6 @@ import dev.inmo.tgbotapi.types.message.ChatEvents.voice.VideoChatStarted import dev.inmo.tgbotapi.types.message.CommonForumContentMessageImpl import dev.inmo.tgbotapi.types.message.CommonGroupContentMessageImpl import dev.inmo.tgbotapi.types.message.CommonGroupEventMessage -import dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage import dev.inmo.tgbotapi.types.message.CommonSupergroupEventMessage import dev.inmo.tgbotapi.types.message.ConnectedFromChannelGroupContentMessageImpl import dev.inmo.tgbotapi.types.message.ForwardInfo @@ -279,7 +277,6 @@ import dev.inmo.tgbotapi.types.message.abstracts.FromChannelGroupContentMessage import dev.inmo.tgbotapi.types.message.abstracts.FromUserMessage import dev.inmo.tgbotapi.types.message.abstracts.GroupContentMessage import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.abstracts.PossiblyEditedMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblyForwardedMessage diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt index fa337b2360..51d5200ded 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentCastsNew.kt @@ -36,6 +36,3 @@ inline fun AnonymousGroupContentMessage<*>.withCont inline fun CommonGroupContentMessage<*>.withContentOrNull() = if (content is T) { this as CommonGroupContentMessage } else { null } inline fun CommonGroupContentMessage<*>.withContentOrThrow() = withContentOrNull()!! - -inline fun MediaGroupMessage<*>.withContentOrNull() = if (content is T) { this as MediaGroupMessage } else { null } -inline fun MediaGroupMessage<*>.withContentOrThrow() = withContentOrNull()!! diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesUtils.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesUtils.kt index 27439971cf..a0c36c0c35 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesUtils.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesUtils.kt @@ -1,21 +1,20 @@ package dev.inmo.tgbotapi.extensions.utils.updates +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.UpdateIdentifier -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.abstracts.* -import dev.inmo.tgbotapi.types.update.media_group.* +import dev.inmo.tgbotapi.utils.extensions.asMediaGroupMessage /** * @return If [this] is [SentMediaGroupUpdate] - [Update.updateId] of [last] element, or its own [Update.updateId] */ +@Deprecated("Redundant", ReplaceWith("updateId")) fun Update.lastUpdateIdentifier(): UpdateIdentifier { - return if (this is SentMediaGroupUpdate) { - origins.last().updateId - } else { - updateId - } + return updateId } /** @@ -24,7 +23,7 @@ fun Update.lastUpdateIdentifier(): UpdateIdentifier { * @see [Update.lastUpdateIdentifier] */ fun List.lastUpdateIdentifier(): UpdateIdentifier? { - return maxByOrNull { it.updateId } ?.lastUpdateIdentifier() + return maxByOrNull { it.updateId } ?.updateId } /** @@ -32,62 +31,40 @@ fun List.lastUpdateIdentifier(): UpdateIdentifier? { */ fun List.convertWithMediaGroupUpdates(): List { val resultUpdates = mutableListOf() - val mediaGroups = mutableMapOf>() + val mediaGroups = mutableMapOf>>>() + for (update in this) { - val data = (update.data as? MediaGroupMessage<*>) - if (data == null) { + val message = (update.data as? PossiblySentViaBotCommonMessage<*>) ?.withContentOrNull() + val mediaGroupId = message ?.mediaGroupId + if (message == null || mediaGroupId == null) { resultUpdates.add(update) continue } when (update) { - is BaseEditMessageUpdate -> resultUpdates.add( - update.toEditMediaGroupUpdate() - ) is BaseSentMessageUpdate -> { - mediaGroups.getOrPut(data.mediaGroupId) { + mediaGroups.getOrPut(mediaGroupId) { mutableListOf() - }.add(update) + }.add(update to message) } else -> resultUpdates.add(update) } } - mediaGroups.values.map { - it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate -> - resultUpdates.add(mediaGroupUpdate) - } + + mediaGroups.map { (_, updatesWithMessages) -> + val update = updatesWithMessages.maxBy { it.first.updateId }.first + resultUpdates.add( + update.copy(updatesWithMessages.map { it.second }.asMediaGroupMessage()) + ) } + resultUpdates.sortBy { it.updateId } return resultUpdates } -/** - * @receiver List of [BaseSentMessageUpdate] where [BaseSentMessageUpdate.data] is [MediaGroupMessage] and all messages - * have the same [MediaGroupMessage.mediaGroupId] - * @return [MessageMediaGroupUpdate] in case if [first] object of [this] is [MessageUpdate]. When [first] object is - * [ChannelPostUpdate] instance - will return [ChannelPostMediaGroupUpdate]. Otherwise will be returned null - */ -fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { - if (isEmpty()) { - return@let null - } - val resultList = sortedBy { it.updateId } - when (first()) { - is MessageUpdate -> MessageMediaGroupUpdate(resultList) - is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(resultList) - else -> null - } -} - /** * @return [EditMessageMediaGroupUpdate] in case if [this] is [EditMessageUpdate]. When [this] object is * [EditChannelPostUpdate] instance - will return [EditChannelPostMediaGroupUpdate] * * @throws IllegalStateException */ -fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let { - when (this) { - is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) - is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) - else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}") - } -} +fun BaseEditMessageUpdate.toEditMediaGroupUpdate() = this diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt index c369e14c16..f4e43f6e6b 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt @@ -1,7 +1,7 @@ package dev.inmo.tgbotapi.extensions.utils.updates.retrieving import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates -import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver @@ -32,7 +32,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( launch { for (update in updatesChannel) { when (val data = update.data) { - is MediaGroupMessage<*> -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) + is PossiblyMediaGroupMessage<*> -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) else -> output(update) } } @@ -57,4 +57,4 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( */ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( output: UpdateReceiver -) = updateHandlerWithMediaGroupsAdaptation(output, 1000L) \ No newline at end of file +) = updateHandlerWithMediaGroupsAdaptation(output, 1000L) From f6f07a5e8a69219869b1419a42537bf87436eb50 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 8 Nov 2022 02:27:38 +0600 Subject: [PATCH 3/5] fixes --- .../api/InternalUtils/UpdatesUtils.kt | 78 +++--- .../extensions/api/send/CopyMessages.kt | 183 -------------- .../extensions/api/utils/UpdatesHandling.kt | 18 +- .../expectations/WaitContentMessage.kt | 16 +- .../expectations/WaitEditedContent.kt | 2 +- .../expectations/WaitEditedContentMessage.kt | 74 +++--- .../expectations/WaitMediaGroup.kt | 44 ---- .../expectations/WaitMediaGroupMessages.kt | 48 ---- .../MessageFilterExcludingMediaGroups.kt | 11 +- .../triggers_handling/ContentTriggers.kt | 2 - .../triggers_handling/MediaGroupTriggers.kt | 160 ------------- .../MediaGroupMarkerFactories.kt | 7 - .../message/content/MediaGroupContent.kt | 6 +- .../types/message/content/Typealiases.kt | 2 +- .../updateshandlers/FlowsUpdatesFilter.kt | 38 +-- .../tgbotapi/updateshandlers/UpdatesFilter.kt | 112 --------- .../tgbotapi/extensions/utils/ClassCasts.kt | 108 +-------- .../extensions/utils/ClassCastsNew.kt | 225 ++++++------------ .../tgbotapi/extensions/utils/WithContent.kt | 4 +- .../utils/extensions/UpdateChatRetriever.kt | 6 - .../utils/shortcuts/FlowsUpdatesFilter.kt | 102 -------- .../utils/shortcuts/MediaGroupsShortcuts.kt | 69 ------ .../BaseMessagesUpdatesConversations.kt | 20 -- .../utils/updates/UpdatesChatFilters.kt | 11 - .../utils/updates/retrieving/LongPolling.kt | 11 +- 25 files changed, 194 insertions(+), 1163 deletions(-) delete mode 100644 tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt delete mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt delete mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt delete mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt delete mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt delete mode 100644 tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt index b559d65f18..387872f07a 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/InternalUtils/UpdatesUtils.kt @@ -1,69 +1,57 @@ package dev.inmo.tgbotapi.extensions.api.InternalUtils import dev.inmo.tgbotapi.types.MediaGroupIdentifier -import dev.inmo.tgbotapi.types.UpdateIdentifier +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage +import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.abstracts.* -import dev.inmo.tgbotapi.types.update.media_group.* - -internal fun Update.lastUpdateIdentifier(): UpdateIdentifier { - return if (this is SentMediaGroupUpdate) { - origins.last().updateId - } else { - updateId - } -} - -internal fun List.lastUpdateIdentifier(): UpdateIdentifier? { - return maxByOrNull { it.updateId } ?.lastUpdateIdentifier() -} +import dev.inmo.tgbotapi.utils.extensions.asMediaGroupMessage +/** + * Will convert incoming list of updates to list with [MediaGroupUpdate]s + */ internal fun List.convertWithMediaGroupUpdates(): List { val resultUpdates = mutableListOf() - val mediaGroups = mutableMapOf>() + val mediaGroups = mutableMapOf>>>() + for (update in this) { - val data = (update.data as? MediaGroupMessage<*>) - if (data == null) { + val message = (update.data as? PossiblySentViaBotCommonMessage<*>) ?.let { + if (it.content is MediaGroupPartContent) { + it as PossiblySentViaBotCommonMessage + } else { + null + } + } + val mediaGroupId = message ?.mediaGroupId + if (message == null || mediaGroupId == null) { resultUpdates.add(update) continue } when (update) { - is BaseEditMessageUpdate -> resultUpdates.add( - update.toEditMediaGroupUpdate() - ) is BaseSentMessageUpdate -> { - mediaGroups.getOrPut(data.mediaGroupId) { + mediaGroups.getOrPut(mediaGroupId) { mutableListOf() - }.add(update) + }.add(update to message) } else -> resultUpdates.add(update) } } - mediaGroups.values.map { - it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate -> - resultUpdates.add(mediaGroupUpdate) - } + + mediaGroups.map { (_, updatesWithMessages) -> + val update = updatesWithMessages.maxBy { it.first.updateId }.first + resultUpdates.add( + update.copy(updatesWithMessages.map { it.second }.asMediaGroupMessage()) + ) } + resultUpdates.sortBy { it.updateId } return resultUpdates } -internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { - if (isEmpty()) { - return@let null - } - val resultList = sortedBy { it.updateId } - when (first()) { - is MessageUpdate -> MessageMediaGroupUpdate(resultList) - is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(resultList) - else -> null - } -} - -internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let { - when (this) { - is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) - is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) - else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}") - } -} +/** + * @return [EditMessageMediaGroupUpdate] in case if [this] is [EditMessageUpdate]. When [this] object is + * [EditChannelPostUpdate] instance - will return [EditChannelPostMediaGroupUpdate] + * + * @throws IllegalStateException + */ +internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate() = this diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt deleted file mode 100644 index 2ab20d94bc..0000000000 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt +++ /dev/null @@ -1,183 +0,0 @@ -package dev.inmo.tgbotapi.extensions.api.send - -import dev.inmo.tgbotapi.bot.TelegramBot -import dev.inmo.tgbotapi.extensions.api.send.media.sendMediaGroup -import dev.inmo.tgbotapi.types.ChatIdentifier -import dev.inmo.tgbotapi.types.media.* -import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList -import dev.inmo.tgbotapi.types.MessageId -import dev.inmo.tgbotapi.types.MessageThreadId -import dev.inmo.tgbotapi.types.message.ParseMode -import dev.inmo.tgbotapi.types.chat.Chat -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChatId: ChatIdentifier, - messages: List>, - text: String? = null, - parseMode: ParseMode? = null, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -): List> { - val first = messages.first().content.toMediaGroupMemberTelegramMedia().let { - if (text != null) { - when (it) { - is TelegramMediaAudio -> it.copy(text = text, parseMode = parseMode) - is TelegramMediaDocument -> it.copy(text = text, parseMode = parseMode) - is TelegramMediaPhoto -> it.copy(text = text, parseMode = parseMode) - is TelegramMediaVideo -> it.copy(text = text, parseMode = parseMode) - } - } else { - it - } - } - - return sendMediaGroup( - toChatId, - listOf(first) + messages.drop(1).map { - it.content.toMediaGroupMemberTelegramMedia() - }, - threadId, - disableNotification, - protectContent, - replyToMessageId, - allowSendingWithoutReply - ) -} - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChat: Chat, - messages: List>, - text: String? = null, - parseMode: ParseMode? = null, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -) = copyMessages(toChat.id, messages, text, parseMode, threadId, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChat: ChatIdentifier, - update: SentMediaGroupUpdate, - text: String? = null, - parseMode: ParseMode? = null, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -) = copyMessages(toChat, update.data, text, parseMode, threadId, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChat: Chat, - update: SentMediaGroupUpdate, - text: String? = null, - parseMode: ParseMode? = null, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -) = copyMessages(toChat.id, update, text, parseMode, threadId, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChatId: ChatIdentifier, - messages: List>, - entities: TextSourcesList, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -): List> { - val first = messages.first().content.toMediaGroupMemberTelegramMedia().let { - when (it) { - is TelegramMediaAudio -> TelegramMediaAudio(it.file, entities, it.duration, it.performer, it.title, it.thumb) - is TelegramMediaDocument -> TelegramMediaDocument(it.file, entities, it.thumb, it.disableContentTypeDetection) - is TelegramMediaPhoto -> TelegramMediaPhoto(it.file, entities) - is TelegramMediaVideo -> TelegramMediaVideo(it.file, entities, it.width, it.height, it.duration, it.thumb) - } - } - - return sendMediaGroup( - toChatId, - listOf(first) + messages.drop(1).map { - it.content.toMediaGroupMemberTelegramMedia() - }, - threadId, - disableNotification, - protectContent, - replyToMessageId, - allowSendingWithoutReply - ) -} - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChat: Chat, - messages: List>, - entities: TextSourcesList, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -) = copyMessages(toChat.id, messages, entities, threadId, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChat: ChatIdentifier, - update: SentMediaGroupUpdate, - entities: TextSourcesList, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -) = copyMessages(toChat, update.data, entities, threadId, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) - -/** - * Send media group via [sendMediaGroup] extension with edited [entities] of first [messages] element. Other elements - * will be copied as they are - */ -suspend inline fun TelegramBot.copyMessages( - toChat: Chat, - update: SentMediaGroupUpdate, - entities: TextSourcesList, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyToMessageId: MessageId? = null, - allowSendingWithoutReply: Boolean? = null -) = copyMessages(toChat.id, update, entities, threadId, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt index 16e695fe1f..1c660b8ad2 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/utils/UpdatesHandling.kt @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.extensions.api.utils import dev.inmo.tgbotapi.extensions.api.InternalUtils.convertWithMediaGroupUpdates +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver @@ -29,10 +30,21 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( launch { launch { for (update in updatesChannel) { - when (val data = update.data) { - is MediaGroupMessage<*> -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) - else -> output(update) + val dataAsPossiblySentViaBotCommonMessage = update.data as? PossiblySentViaBotCommonMessage<*> + + if (dataAsPossiblySentViaBotCommonMessage == null) { + output(update) + continue } + + val mediaGroupId = dataAsPossiblySentViaBotCommonMessage.mediaGroupId + + if (mediaGroupId == null) { + output(update) + continue + } + + mediaGroupChannel.send("${mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) } } launch { diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt index 818da8ce2b..d908e016dd 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitContentMessage.kt @@ -9,7 +9,6 @@ import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage import kotlinx.coroutines.flow.Flow @@ -25,20 +24,7 @@ suspend inline fun BehaviourContext.waitContentMess initRequest, errorFactory ) { - val messages = when (it) { - is SentMediaGroupUpdate -> { - if (includeMediaGroups) { - it.data - } else { - emptyList() - } - } - is BaseSentMessageUpdate -> listOf(it.data) - else -> return@expectFlow emptyList() - } - messages.mapNotNull { message -> - (message as? CommonMessage<*>) ?.withContent() - } + listOfNotNull((it.data as? CommonMessage<*>) ?.withContent()) } internal inline fun contentMessageConverter( diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt index 7bac69114d..c78ab0452a 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContent.kt @@ -15,7 +15,7 @@ suspend inline fun BehaviourContext.waitEditedConte initRequest: Request<*>? = null, includeMediaGroups: Boolean = true, noinline errorFactory: NullableRequestBuilder<*> = { null } -): Flow = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory).map { it.content } +): Flow = waitEditedContentMessage(initRequest, errorFactory).map { it.content } suspend fun BehaviourContext.waitEditedMessageContent( initRequest: Request<*>? = null, diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt index 9b39b1f911..99d14d8d0b 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitEditedContentMessage.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.utils.baseEditMessageUpdateOrNull import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.requests.abstracts.Request @@ -16,7 +17,6 @@ import kotlinx.coroutines.flow.Flow @RiskFeature(lowLevelRiskFeatureMessage) suspend inline fun BehaviourContext.waitEditedContentMessage( initRequest: Request<*>? = null, - includeMediaGroups: Boolean = true, noinline errorFactory: NullableRequestBuilder<*> = { null } ): Flow> = expectFlow( initRequest, @@ -25,11 +25,7 @@ suspend inline fun BehaviourContext.waitEditedConte val messages = when (it) { is BaseEditMessageUpdate -> { val commonMessage = it.data.commonMessageOrNull() ?: return@expectFlow emptyList() - if (commonMessage !is MediaGroupMessage<*> || includeMediaGroups) { - listOf(commonMessage) - } else { - emptyList() - } + listOf(commonMessage) } else -> return@expectFlow emptyList() } @@ -40,109 +36,103 @@ suspend inline fun BehaviourContext.waitEditedConte suspend fun BehaviourContext.waitEditedMessageContentMessage( initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null }, - includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) + errorFactory: NullableRequestBuilder<*> = { null } +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedContactMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedDiceMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedGameMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedLocationMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedLiveLocationMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedStaticLocationMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedTextMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedVenueMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedAudioMediaGroupContentMessage( initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null }, - includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) + errorFactory: NullableRequestBuilder<*> = { null } +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedDocumentMediaGroupContentMessage( initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null }, - includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) + errorFactory: NullableRequestBuilder<*> = { null } +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedMediaMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = false -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedAnyMediaGroupContentMessage( initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null }, - includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) + errorFactory: NullableRequestBuilder<*> = { null } +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedVisualMediaGroupContentMessage( initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null }, - includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) + errorFactory: NullableRequestBuilder<*> = { null } +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedTextedMediaContentMessage( initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null }, - includeMediaGroups: Boolean = true -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) + errorFactory: NullableRequestBuilder<*> = { null } +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedAnimationMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedAudioMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = false -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedDocumentMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = false -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedPhotoMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = false -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedStickerMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedVideoMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null }, includeMediaGroups: Boolean = false -) = waitEditedContentMessage(initRequest, includeMediaGroups, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedVideoNoteMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedVoiceMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) suspend fun BehaviourContext.waitEditedInvoiceMessage( initRequest: Request<*>? = null, errorFactory: NullableRequestBuilder<*> = { null } -) = waitEditedContentMessage(initRequest, false, errorFactory) +) = waitEditedContentMessage(initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt deleted file mode 100644 index 5e38f5555e..0000000000 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt +++ /dev/null @@ -1,44 +0,0 @@ -package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations - -import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext -import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter -import dev.inmo.tgbotapi.requests.abstracts.Request -import dev.inmo.tgbotapi.types.message.content.* -import dev.inmo.tgbotapi.utils.RiskFeature -import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map - -typealias MediaGroupFilter = SimpleFilter>> - - -@RiskFeature(lowLevelRiskFeatureMessage) -suspend inline fun BehaviourContext.buildMediaGroupWaiter( - initRequest: Request<*>? = null, - noinline errorFactory: NullableRequestBuilder<*> = { null } -): Flow> = buildMediaGroupMessagesWaiter(initRequest, errorFactory).map { it.map { it.content } } - -suspend fun BehaviourContext.waitMediaGroup( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitPlaylist( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitDocumentsGroup( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitVisualGallery( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitPhotoGallery( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitVideoGallery( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupWaiter(initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt deleted file mode 100644 index 828a582942..0000000000 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt +++ /dev/null @@ -1,48 +0,0 @@ -package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations - -import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext -import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull -import dev.inmo.tgbotapi.extensions.utils.withContent -import dev.inmo.tgbotapi.requests.abstracts.Request -import dev.inmo.tgbotapi.types.message.content.* -import dev.inmo.tgbotapi.utils.RiskFeature -import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage -import kotlinx.coroutines.flow.Flow - -@RiskFeature(lowLevelRiskFeatureMessage) -suspend inline fun BehaviourContext.buildMediaGroupMessagesWaiter( - initRequest: Request<*>? = null, - noinline errorFactory: NullableRequestBuilder<*> = { null } -): Flow>> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update -> - update.sentMediaGroupUpdateOrNull() ?.data ?.let { mediaGroup -> - val mapped = mediaGroup.mapNotNull { it.withContent() } - listOf( - mapped - ) - } ?: emptyList() -} - -suspend fun BehaviourContext.waitMediaGroupMessages( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitPlaylistMessages( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitDocumentsGroupMessages( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitVisualGalleryMessages( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitPhotoGalleryMessages( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) -suspend fun BehaviourContext.waitVideoGalleryMessages( - initRequest: Request<*>? = null, - errorFactory: NullableRequestBuilder<*> = { null } -) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt index 60389a5ecf..aa22050056 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt @@ -3,18 +3,13 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.filters import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.types.message.abstracts.* +import dev.inmo.tgbotapi.types.message.content.MediaGroupContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupMessage import dev.inmo.tgbotapi.types.update.abstracts.Update -/** - * Allow only messages which are not [MediaGroupMessage] - */ -val MessageFilterExcludingMediaGroups: BehaviourContextAndTwoTypesReceiver, Update> = { _, update -> - update !is MediaGroupMessage<*> -} - /** * Allow only messages which are not [MediaGroupMessage] */ val CommonMessageFilterExcludeMediaGroups = SimpleFilter { - it !is MediaGroupMessage<*> + it !is CommonMessage<*> || it.content !is MediaGroupContent } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt index 4971de586e..d83be5e0f5 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt @@ -14,7 +14,6 @@ import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate typealias CommonMessageFilter = SimpleFilter> @@ -26,7 +25,6 @@ internal suspend inline fun ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { when (it) { is BaseSentMessageUpdate -> it.data.whenCommonMessage(::listOfNotNull) - is SentMediaGroupUpdate -> it.data else -> null } ?.mapNotNull { message -> if (message.content is T) message as CommonMessage else null diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt deleted file mode 100644 index 5394ec31b4..0000000000 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt +++ /dev/null @@ -1,160 +0,0 @@ -@file:Suppress("unused") - -package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling - -import dev.inmo.tgbotapi.extensions.behaviour_builder.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessagesFilterByChat -import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter -import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMediaGroupMarkerFactory -import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory -import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull -import dev.inmo.tgbotapi.types.message.content.* -import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.utils.PreviewFeature - -@PreviewFeature -internal suspend inline fun BC.buildMediaGroupTrigger( - initialFilter: SimpleFilter>>? = null, - noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { - (it.sentMediaGroupUpdateOrNull() ?.data ?.takeIf { messages -> - messages.all { message -> - message.content is T - } - } as? List>) ?.let(::listOfNotNull) -} - -/** - * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call - * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, - * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] - * to combinate several filters - * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously - * in one "stream". Output of [markerFactory] will be used as a key for "stream" - * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that - * data - */ -suspend fun BC.onMediaGroup( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) - -/** - * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call - * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, - * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] - * to combinate several filters - * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously - * in one "stream". Output of [markerFactory] will be used as a key for "stream" - * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that - * data - */ -suspend fun BC.onPlaylist( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) - -/** - * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call - * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, - * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] - * to combinate several filters - * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously - * in one "stream". Output of [markerFactory] will be used as a key for "stream" - * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that - * data - */ -suspend fun BC.onDocumentsGroup( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) - -/** - * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call - * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, - * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] - * to combinate several filters - * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously - * in one "stream". Output of [markerFactory] will be used as a key for "stream" - * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that - * data - */ -suspend fun BC.onVisualGallery( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) - -/** - * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call - * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, - * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] - * to combinate several filters - * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously - * in one "stream". Output of [markerFactory] will be used as a key for "stream" - * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that - * data - */ -suspend fun BC.onVisualMediaGroup( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = onVisualGallery(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) - -/** - * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call - * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, - * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] - * to combinate several filters - * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously - * in one "stream". Output of [markerFactory] will be used as a key for "stream" - * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that - * data - */ -suspend fun BC.onPhotoGallery( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) - -/** - * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call - * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, - * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. - * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] - * to combinate several filters - * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously - * in one "stream". Output of [markerFactory] will be used as a key for "stream" - * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that - * data - */ -suspend fun BC.onVideoGallery( - initialFilter: SimpleFilter>>? = null, - subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver>, Update>? = MessagesFilterByChat, - markerFactory: MarkerFactory>, Any> = ByChatMediaGroupMarkerFactory, - scenarioReceiver: CustomBehaviourContextAndTypeReceiver>> -) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt deleted file mode 100644 index e4e0a5838b..0000000000 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/MediaGroupMarkerFactories.kt +++ /dev/null @@ -1,7 +0,0 @@ -package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories - -import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat - -object ByChatMediaGroupMarkerFactory : MarkerFactory>, Any> { - override suspend fun invoke(data: List>) = data.chat ?: error("Data must not be empty") -} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt index 6f2f59fbc1..2af4378fae 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt @@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.requests.send.media.SendMediaGroup import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.MediaGroupIdentifier import dev.inmo.tgbotapi.types.MessageId +import dev.inmo.tgbotapi.types.MessageThreadId import dev.inmo.tgbotapi.types.buttons.KeyboardMarkup import dev.inmo.tgbotapi.types.files.TelegramMediaFile import dev.inmo.tgbotapi.types.media.TelegramMedia @@ -31,6 +32,7 @@ data class MediaGroupContent( override fun createResend( chatId: ChatIdentifier, + threadId: MessageThreadId?, disableNotification: Boolean, protectContent: Boolean, replyToMessageId: MessageId?, @@ -39,10 +41,10 @@ data class MediaGroupContent( ): Request = SendMediaGroup( chatId, group.map { it.content.toMediaGroupMemberTelegramMedia() }, + threadId, disableNotification, protectContent, replyToMessageId, - allowSendingWithoutReply, - replyMarkup + allowSendingWithoutReply ) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt index f14628d24e..03ca71bfeb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt @@ -20,7 +20,7 @@ typealias VideoNoteMessage = CommonMessage typealias StickerMessage = CommonMessage typealias TextedMediaMessage = CommonMessage typealias VoiceMessage = CommonMessage -typealias MediaGroupMessage = CommonMessage +typealias MediaGroupMessage = CommonMessage typealias AudioMediaGroupMessage = CommonMessage typealias AudioMessage = CommonMessage typealias DocumentMediaGroupMessage = CommonMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt index c3265bc419..2e9c700266 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt @@ -2,10 +2,11 @@ package dev.inmo.tgbotapi.updateshandlers import dev.inmo.micro_utils.coroutines.plus import dev.inmo.tgbotapi.types.ALL_UPDATES_LIST +import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.update.* +import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.types.update.media_group.* import kotlinx.coroutines.channels.* import kotlinx.coroutines.flow.* @@ -13,16 +14,22 @@ interface FlowsUpdatesFilter : UpdatesFilter { override val allowedUpdates: List get() = ALL_UPDATES_LIST val allUpdatesFlow: Flow + @Deprecated("Since 4.0.0 is not actual", ReplaceWith("allUpdatesFlow")) val allUpdatesWithoutMediaGroupsGroupingFlow: Flow + get() = allUpdatesFlow val messagesFlow: Flow - val messageMediaGroupsFlow: Flow + val messageMediaGroupsFlow: Flow + get() = messagesFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null } val editedMessagesFlow: Flow - val editedMessageMediaGroupsFlow: Flow + val editedMessageMediaGroupsFlow: Flow + get() = editedMessagesFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null } val channelPostsFlow: Flow - val channelPostMediaGroupsFlow: Flow + val channelPostMediaGroupsFlow: Flow + get() = channelPostsFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null } val editedChannelPostsFlow: Flow - val editedChannelPostMediaGroupsFlow: Flow + val editedChannelPostMediaGroupsFlow: Flow + get() = editedChannelPostsFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null } val chosenInlineResultsFlow: Flow val inlineQueriesFlow: Flow val callbackQueriesFlow: Flow @@ -37,23 +44,10 @@ interface FlowsUpdatesFilter : UpdatesFilter { } abstract class AbstractFlowsUpdatesFilter : FlowsUpdatesFilter { - override val allUpdatesWithoutMediaGroupsGroupingFlow: Flow - get() = allUpdatesFlow.flatMapConcat { - when (it) { - is SentMediaGroupUpdate -> it.origins.asFlow() - is EditMediaGroupUpdate -> flowOf(it.origin) - else -> flowOf(it) - } - } - override val messagesFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } - override val messageMediaGroupsFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } override val editedMessagesFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } - override val editedMessageMediaGroupsFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } override val channelPostsFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } - override val channelPostMediaGroupsFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } override val editedChannelPostsFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } - override val editedChannelPostMediaGroupsFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } override val chosenInlineResultsFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } override val inlineQueriesFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } override val callbackQueriesFlow: Flow by lazy { allUpdatesFlow.filterIsInstance() } @@ -92,14 +86,6 @@ class DefaultFlowsUpdatesFilter( it } } - @Suppress("MemberVisibilityCanBePrivate") - override val allUpdatesWithoutMediaGroupsGroupingFlow: Flow = allUpdatesFlow.flatMapConcat { - when (it) { - is SentMediaGroupUpdate -> it.origins.asFlow() - is EditMediaGroupUpdate -> flowOf(it.origin) - else -> flowOf(it) - } - } override val asUpdateReceiver: UpdateReceiver = additionalUpdatesSharedFlow::emit } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/UpdatesFilter.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/UpdatesFilter.kt index 4704a4c9d5..cfb0dc2a26 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/UpdatesFilter.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/UpdatesFilter.kt @@ -1,10 +1,6 @@ package dev.inmo.tgbotapi.updateshandlers -import dev.inmo.tgbotapi.types.* -import dev.inmo.tgbotapi.types.update.* -import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.types.update.media_group.* typealias UpdateReceiver = suspend (T) -> Unit @@ -12,111 +8,3 @@ interface UpdatesFilter { val asUpdateReceiver: UpdateReceiver val allowedUpdates: List } - -data class SimpleUpdatesFilter( - private val messageCallback: UpdateReceiver? = null, - private val messageMediaGroupCallback: UpdateReceiver? = null, - private val editedMessageCallback: UpdateReceiver? = null, - private val editedMessageMediaGroupCallback: UpdateReceiver? = null, - private val channelPostCallback: UpdateReceiver? = null, - private val channelPostMediaGroupCallback: UpdateReceiver? = null, - private val editedChannelPostCallback: UpdateReceiver? = null, - private val editedChannelPostMediaGroupCallback: UpdateReceiver? = null, - private val chosenInlineResultCallback: UpdateReceiver? = null, - private val inlineQueryCallback: UpdateReceiver? = null, - private val callbackQueryCallback: UpdateReceiver? = null, - private val shippingQueryCallback: UpdateReceiver? = null, - private val preCheckoutQueryCallback: UpdateReceiver? = null, - private val pollUpdateCallback: UpdateReceiver? = null, - private val pollAnswerUpdateCallback: UpdateReceiver? = null, - private val unknownUpdateTypeCallback: UpdateReceiver? = null -) : UpdatesFilter { - override val asUpdateReceiver: UpdateReceiver = this::invoke - override val allowedUpdates = listOfNotNull( - (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, - (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, - (channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST }, - (editedChannelPostCallback ?: editedChannelPostMediaGroupCallback) ?.let { UPDATE_EDITED_CHANNEL_POST }, - chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT }, - inlineQueryCallback ?.let { UPDATE_INLINE_QUERY }, - callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY }, - shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY }, - preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }, - pollUpdateCallback ?.let { UPDATE_POLL }, - pollAnswerUpdateCallback ?.let { UPDATE_POLL_ANSWER } - ) - - suspend fun invoke(update: Update) { - when (update) { - is MessageUpdate -> messageCallback ?.invoke(update) - is MessageMediaGroupUpdate -> messageMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: messageCallback ?.also { receiver -> - update.origins.mapNotNull { it as? MessageUpdate }.forEach { - receiver(it) - } - } - is EditMessageUpdate -> editedMessageCallback ?.invoke(update) - is EditMessageMediaGroupUpdate -> editedMessageMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: editedMessageCallback ?.also { receiver -> - receiver(update.origin) - } - is ChannelPostUpdate -> channelPostCallback ?.invoke(update) - is ChannelPostMediaGroupUpdate -> channelPostMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: channelPostCallback ?.also { receiver -> - update.origins.mapNotNull { it as? ChannelPostUpdate }.forEach { - receiver(it) - } - } - is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update) - is EditChannelPostMediaGroupUpdate -> editedChannelPostMediaGroupCallback ?.also { receiver -> - receiver(update) - } ?: editedChannelPostCallback ?.also { receiver -> - receiver(update.origin) - } - is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update) - is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update) - is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update) - is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(update) - is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update) - is PollUpdate -> pollUpdateCallback ?.invoke(update) - is PollAnswerUpdate -> pollAnswerUpdateCallback ?.invoke(update) - is UnknownUpdate -> unknownUpdateTypeCallback ?.invoke(update) - } - } -} - -fun createSimpleUpdateFilter( - messageCallback: UpdateReceiver? = null, - mediaGroupCallback: UpdateReceiver? = null, - editedMessageCallback: UpdateReceiver? = null, - channelPostCallback: UpdateReceiver? = null, - editedChannelPostCallback: UpdateReceiver? = null, - chosenInlineResultCallback: UpdateReceiver? = null, - inlineQueryCallback: UpdateReceiver? = null, - callbackQueryCallback: UpdateReceiver? = null, - shippingQueryCallback: UpdateReceiver? = null, - preCheckoutQueryCallback: UpdateReceiver? = null, - pollCallback: UpdateReceiver? = null, - pollAnswerCallback: UpdateReceiver? = null, - unknownCallback: UpdateReceiver? = null -): UpdatesFilter = SimpleUpdatesFilter( - messageCallback = messageCallback, - messageMediaGroupCallback = mediaGroupCallback, - editedMessageCallback = editedMessageCallback, - editedMessageMediaGroupCallback = mediaGroupCallback, - channelPostCallback = channelPostCallback, - channelPostMediaGroupCallback = mediaGroupCallback, - editedChannelPostCallback = editedChannelPostCallback, - editedChannelPostMediaGroupCallback = mediaGroupCallback, - chosenInlineResultCallback = chosenInlineResultCallback, - inlineQueryCallback = inlineQueryCallback, - callbackQueryCallback = callbackQueryCallback, - shippingQueryCallback = shippingQueryCallback, - preCheckoutQueryCallback = preCheckoutQueryCallback, - pollUpdateCallback = pollCallback, - pollAnswerUpdateCallback = pollAnswerCallback, - unknownUpdateTypeCallback = unknownCallback -) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index a2e1c1dfba..0bf559a79a 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -46,7 +46,6 @@ import dev.inmo.tgbotapi.types.polls.* import dev.inmo.tgbotapi.types.queries.callback.* import dev.inmo.tgbotapi.types.update.* import dev.inmo.tgbotapi.types.update.abstracts.* -import dev.inmo.tgbotapi.types.update.media_group.* import dev.inmo.tgbotapi.utils.PreviewFeature @PreviewFeature @@ -1036,18 +1035,6 @@ inline fun Message.asChannelEventMessage(): ChannelEventMessage? = inline fun Message.requireChannelEventMessage(): ChannelEventMessage = this as ChannelEventMessage -@PreviewFeature -inline fun Message.whenChannelMediaGroupMessage(block: (ChannelMediaGroupMessage) -> T) = - asChannelMediaGroupMessage()?.let(block) - -@PreviewFeature -inline fun Message.asChannelMediaGroupMessage(): ChannelMediaGroupMessage? = - this as? ChannelMediaGroupMessage - -@PreviewFeature -inline fun Message.requireChannelMediaGroupMessage(): ChannelMediaGroupMessage = - this as ChannelMediaGroupMessage - @PreviewFeature inline fun Message.whenCommonGroupEventMessage(block: (CommonGroupEventMessage) -> T) = asCommonGroupEventMessage()?.let(block) @@ -1060,18 +1047,6 @@ inline fun Message.asCommonGroupEventMessage(): CommonGroupEventMessage = this as CommonGroupEventMessage -@PreviewFeature -inline fun Message.whenCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T) = - asCommonMediaGroupMessage()?.let(block) - -@PreviewFeature -inline fun Message.asCommonMediaGroupMessage(): CommonMediaGroupMessage? = - this as? CommonMediaGroupMessage - -@PreviewFeature -inline fun Message.requireCommonMediaGroupMessage(): CommonMediaGroupMessage = - this as CommonMediaGroupMessage - @PreviewFeature inline fun Message.whenCommonSupergroupEventMessage(block: (CommonSupergroupEventMessage) -> T) = asCommonSupergroupEventMessage()?.let(block) @@ -1219,16 +1194,16 @@ inline fun Message.requireGroupContentMessage(): GroupContentMessage @PreviewFeature -inline fun Message.whenMediaGroupMessage(block: (MediaGroupMessage) -> T) = +inline fun Message.whenMediaGroupMessage(block: (MediaGroupMessage) -> T) = asMediaGroupMessage()?.let(block) @PreviewFeature -inline fun Message.asMediaGroupMessage(): MediaGroupMessage? = - this as? MediaGroupMessage +inline fun Message.asMediaGroupMessage(): MediaGroupMessage? = + this as? MediaGroupMessage @PreviewFeature -inline fun Message.requireMediaGroupMessage(): MediaGroupMessage = - this as MediaGroupMessage +inline fun Message.requireMediaGroupMessage(): MediaGroupMessage = + this as MediaGroupMessage @PreviewFeature inline fun Message.whenPossiblyEditedMessage(block: (PossiblyEditedMessage) -> T) = @@ -2218,79 +2193,6 @@ inline fun Update.asInlineQueryUpdate(): InlineQueryUpdate? = this as? InlineQue @PreviewFeature inline fun Update.requireInlineQueryUpdate(): InlineQueryUpdate = this as InlineQueryUpdate -@PreviewFeature -inline fun Update.whenChannelPostMediaGroupUpdate(block: (ChannelPostMediaGroupUpdate) -> T) = - asChannelPostMediaGroupUpdate()?.let(block) - -@PreviewFeature -inline fun Update.asChannelPostMediaGroupUpdate(): ChannelPostMediaGroupUpdate? = this as? ChannelPostMediaGroupUpdate - -@PreviewFeature -inline fun Update.requireChannelPostMediaGroupUpdate(): ChannelPostMediaGroupUpdate = - this as ChannelPostMediaGroupUpdate - -@PreviewFeature -inline fun Update.whenEditChannelPostMediaGroupUpdate(block: (EditChannelPostMediaGroupUpdate) -> T) = - asEditChannelPostMediaGroupUpdate()?.let(block) - -@PreviewFeature -inline fun Update.asEditChannelPostMediaGroupUpdate(): EditChannelPostMediaGroupUpdate? = - this as? EditChannelPostMediaGroupUpdate - -@PreviewFeature -inline fun Update.requireEditChannelPostMediaGroupUpdate(): EditChannelPostMediaGroupUpdate = - this as EditChannelPostMediaGroupUpdate - -@PreviewFeature -inline fun Update.whenEditMediaGroupUpdate(block: (EditMediaGroupUpdate) -> T) = - asEditMediaGroupUpdate()?.let(block) - -@PreviewFeature -inline fun Update.asEditMediaGroupUpdate(): EditMediaGroupUpdate? = this as? EditMediaGroupUpdate - -@PreviewFeature -inline fun Update.requireEditMediaGroupUpdate(): EditMediaGroupUpdate = this as EditMediaGroupUpdate - -@PreviewFeature -inline fun Update.whenEditMessageMediaGroupUpdate(block: (EditMessageMediaGroupUpdate) -> T) = - asEditMessageMediaGroupUpdate()?.let(block) - -@PreviewFeature -inline fun Update.asEditMessageMediaGroupUpdate(): EditMessageMediaGroupUpdate? = this as? EditMessageMediaGroupUpdate - -@PreviewFeature -inline fun Update.requireEditMessageMediaGroupUpdate(): EditMessageMediaGroupUpdate = - this as EditMessageMediaGroupUpdate - -@PreviewFeature -inline fun Update.whenMediaGroupUpdate(block: (MediaGroupUpdate) -> T) = asMediaGroupUpdate()?.let(block) - -@PreviewFeature -inline fun Update.asMediaGroupUpdate(): MediaGroupUpdate? = this as? MediaGroupUpdate - -@PreviewFeature -inline fun Update.requireMediaGroupUpdate(): MediaGroupUpdate = this as MediaGroupUpdate - -@PreviewFeature -inline fun Update.whenMessageMediaGroupUpdate(block: (MessageMediaGroupUpdate) -> T) = - asMessageMediaGroupUpdate()?.let(block) - -@PreviewFeature -inline fun Update.asMessageMediaGroupUpdate(): MessageMediaGroupUpdate? = this as? MessageMediaGroupUpdate - -@PreviewFeature -inline fun Update.requireMessageMediaGroupUpdate(): MessageMediaGroupUpdate = this as MessageMediaGroupUpdate - -@PreviewFeature -inline fun Update.whenSentMediaGroupUpdate(block: (SentMediaGroupUpdate) -> T) = - asSentMediaGroupUpdate()?.let(block) - -@PreviewFeature -inline fun Update.asSentMediaGroupUpdate(): SentMediaGroupUpdate? = this as? SentMediaGroupUpdate - -@PreviewFeature -inline fun Update.requireSentMediaGroupUpdate(): SentMediaGroupUpdate = this as SentMediaGroupUpdate - @PreviewFeature inline fun Update.whenMessageUpdate(block: (MessageUpdate) -> T) = asMessageUpdate()?.let(block) 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 aa69edf531..123c9dfb6f 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 @@ -280,8 +280,10 @@ import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.abstracts.PossiblyEditedMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblyForwardedMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblyPaymentMessage import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage +import dev.inmo.tgbotapi.types.message.abstracts.PossiblyTopicMessage import dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage import dev.inmo.tgbotapi.types.message.abstracts.PublicContentMessage import dev.inmo.tgbotapi.types.message.abstracts.SignedMessage @@ -301,6 +303,8 @@ import dev.inmo.tgbotapi.types.message.content.LiveLocationContent import dev.inmo.tgbotapi.types.message.content.LocationContent import dev.inmo.tgbotapi.types.message.content.MediaCollectionContent import dev.inmo.tgbotapi.types.message.content.MediaContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent +import dev.inmo.tgbotapi.types.message.content.MediaGroupContent import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MessageContent import dev.inmo.tgbotapi.types.message.content.PhotoContent @@ -438,13 +442,6 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.types.update.media_group.ChannelPostMediaGroupUpdate -import dev.inmo.tgbotapi.types.update.media_group.EditChannelPostMediaGroupUpdate -import dev.inmo.tgbotapi.types.update.media_group.EditMediaGroupUpdate -import dev.inmo.tgbotapi.types.update.media_group.EditMessageMediaGroupUpdate -import dev.inmo.tgbotapi.types.update.media_group.MediaGroupUpdate -import dev.inmo.tgbotapi.types.update.media_group.MessageMediaGroupUpdate -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import kotlin.Suppress public inline fun CommonSendInvoiceData.createInvoiceLinkOrNull(): CreateInvoiceLink? = this as? @@ -738,18 +735,6 @@ public inline fun WithUser.ifCommonGroupEventMessage(block: (CommonGroupEventMessage) -> T): T? = commonGroupEventMessageOrNull() ?.let(block) -public inline fun WithUser.commonMediaGroupMessageOrNull(): - CommonMediaGroupMessage? = this as? - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage - -public inline fun WithUser.commonMediaGroupMessageOrThrow(): - CommonMediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage - -public inline fun - WithUser.ifCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T): T? - = commonMediaGroupMessageOrNull() ?.let(block) - public inline fun WithUser.commonSupergroupEventMessageOrNull(): CommonSupergroupEventMessage? = this as? dev.inmo.tgbotapi.types.message.CommonSupergroupEventMessage @@ -3032,18 +3017,6 @@ public inline fun Message.ifChannelEventMessage(block: (ChannelEventMessage) -> T): T? = channelEventMessageOrNull() ?.let(block) -public inline fun Message.channelMediaGroupMessageOrNull(): - ChannelMediaGroupMessage? = this as? - dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage - -public inline fun Message.channelMediaGroupMessageOrThrow(): - ChannelMediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.ChannelMediaGroupMessage - -public inline fun - Message.ifChannelMediaGroupMessage(block: (ChannelMediaGroupMessage) -> T): - T? = channelMediaGroupMessageOrNull() ?.let(block) - public inline fun Message.commonGroupEventMessageOrNull(): CommonGroupEventMessage? = this as? dev.inmo.tgbotapi.types.message.CommonGroupEventMessage @@ -3056,18 +3029,6 @@ public inline fun Message.ifCommonGroupEventMessage(block: (CommonGroupEventMessage) -> T): T? = commonGroupEventMessageOrNull() ?.let(block) -public inline fun Message.commonMediaGroupMessageOrNull(): - CommonMediaGroupMessage? = this as? - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage - -public inline fun Message.commonMediaGroupMessageOrThrow(): - CommonMediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.CommonMediaGroupMessage - -public inline fun - Message.ifCommonMediaGroupMessage(block: (CommonMediaGroupMessage) -> T): T? - = commonMediaGroupMessageOrNull() ?.let(block) - public inline fun Message.commonSupergroupEventMessageOrNull(): CommonSupergroupEventMessage? = this as? dev.inmo.tgbotapi.types.message.CommonSupergroupEventMessage @@ -3372,17 +3333,6 @@ public inline fun Message.ifCommonForumContentMessage(block: (CommonForumContentMessage) -> T): T? = commonForumContentMessageOrNull() ?.let(block) -public inline fun Message.mediaGroupMessageOrNull(): MediaGroupMessage? = this - as? - dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage - -public inline fun Message.mediaGroupMessageOrThrow(): MediaGroupMessage = this as - dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage - -public inline fun - Message.ifMediaGroupMessage(block: (MediaGroupMessage) -> T): T? = - mediaGroupMessageOrNull() ?.let(block) - public inline fun Message.unknownMessageTypeOrNull(): UnknownMessageType? = this as? dev.inmo.tgbotapi.types.message.abstracts.UnknownMessageType @@ -3410,6 +3360,18 @@ public inline fun Message.possiblyForwardedMessageOrThrow(): PossiblyForwardedMe public inline fun Message.ifPossiblyForwardedMessage(block: (PossiblyForwardedMessage) -> T): T? = possiblyForwardedMessageOrNull() ?.let(block) +public inline fun Message.possiblyMediaGroupMessageOrNull(): + PossiblyMediaGroupMessage? = this as? + dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage + +public inline fun Message.possiblyMediaGroupMessageOrThrow(): + PossiblyMediaGroupMessage = this as + dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage + +public inline fun + Message.ifPossiblyMediaGroupMessage(block: (PossiblyMediaGroupMessage) -> T): T? + = possiblyMediaGroupMessageOrNull() ?.let(block) + public inline fun Message.possiblyPaymentMessageOrNull(): PossiblyPaymentMessage? = this as? dev.inmo.tgbotapi.types.message.abstracts.PossiblyPaymentMessage @@ -3431,6 +3393,15 @@ public inline fun Message.ifPossiblySentViaBotCommonMessage(block: (PossiblySentViaBotCommonMessage) -> T): T? = possiblySentViaBotCommonMessageOrNull() ?.let(block) +public inline fun Message.possiblyTopicMessageOrNull(): PossiblyTopicMessage? = this as? + dev.inmo.tgbotapi.types.message.abstracts.PossiblyTopicMessage + +public inline fun Message.possiblyTopicMessageOrThrow(): PossiblyTopicMessage = this as + dev.inmo.tgbotapi.types.message.abstracts.PossiblyTopicMessage + +public inline fun Message.ifPossiblyTopicMessage(block: (PossiblyTopicMessage) -> T): T? = + possiblyTopicMessageOrNull() ?.let(block) + public inline fun Message.privateContentMessageOrNull(): PrivateContentMessage? = this as? dev.inmo.tgbotapi.types.message.abstracts.PrivateContentMessage @@ -3506,34 +3477,27 @@ public inline fun ResendableContent.mediaContentOrThrow(): MediaContent = this a public inline fun ResendableContent.ifMediaContent(block: (MediaContent) -> T): T? = mediaContentOrNull() ?.let(block) -public inline fun ResendableContent.audioMediaGroupContentOrNull(): AudioMediaGroupPartContent? = this - as? dev.inmo.tgbotapi.types.message.content.AudioMediaGroupPartContent +public inline fun ResendableContent.audioMediaGroupPartContentOrNull(): AudioMediaGroupPartContent? + = this as? dev.inmo.tgbotapi.types.message.content.AudioMediaGroupPartContent -public inline fun ResendableContent.audioMediaGroupContentOrThrow(): AudioMediaGroupPartContent = this - as dev.inmo.tgbotapi.types.message.content.AudioMediaGroupPartContent +public inline fun ResendableContent.audioMediaGroupPartContentOrThrow(): AudioMediaGroupPartContent + = this as dev.inmo.tgbotapi.types.message.content.AudioMediaGroupPartContent public inline fun - ResendableContent.ifAudioMediaGroupContent(block: (AudioMediaGroupPartContent) -> T): T? = - audioMediaGroupContentOrNull() ?.let(block) + ResendableContent.ifAudioMediaGroupPartContent(block: (AudioMediaGroupPartContent) -> T): T? = + audioMediaGroupPartContentOrNull() ?.let(block) -public inline fun ResendableContent.documentMediaGroupContentOrNull(): DocumentMediaGroupPartContent? = - this as? dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupPartContent +public inline fun ResendableContent.documentMediaGroupPartContentOrNull(): + DocumentMediaGroupPartContent? = this as? + dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupPartContent -public inline fun ResendableContent.documentMediaGroupContentOrThrow(): DocumentMediaGroupPartContent = - this as dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupPartContent +public inline fun ResendableContent.documentMediaGroupPartContentOrThrow(): + DocumentMediaGroupPartContent = this as + dev.inmo.tgbotapi.types.message.content.DocumentMediaGroupPartContent public inline fun - ResendableContent.ifDocumentMediaGroupContent(block: (DocumentMediaGroupPartContent) -> T): T? = - documentMediaGroupContentOrNull() ?.let(block) - -public inline fun ResendableContent.mediaGroupContentOrNull(): MediaGroupPartContent? = this as? - dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent - -public inline fun ResendableContent.mediaGroupContentOrThrow(): MediaGroupPartContent = this as - dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent - -public inline fun ResendableContent.ifMediaGroupContent(block: (MediaGroupPartContent) -> T): T? = - mediaGroupContentOrNull() ?.let(block) + ResendableContent.ifDocumentMediaGroupPartContent(block: (DocumentMediaGroupPartContent) -> T): + T? = documentMediaGroupPartContentOrNull() ?.let(block) public inline fun ResendableContent.textedMediaContentOrNull(): TextedMediaContent? = this as? dev.inmo.tgbotapi.types.message.content.TextedMediaContent @@ -3544,15 +3508,39 @@ public inline fun ResendableContent.textedMediaContentOrThrow(): TextedMediaCont public inline fun ResendableContent.ifTextedMediaContent(block: (TextedMediaContent) -> T): T? = textedMediaContentOrNull() ?.let(block) -public inline fun ResendableContent.visualMediaGroupContentOrNull(): VisualMediaGroupPartContent? = this - as? dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent +public inline fun ResendableContent.mediaGroupCollectionContentOrNull(): + MediaGroupCollectionContent? = this as? + dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent -public inline fun ResendableContent.visualMediaGroupContentOrThrow(): VisualMediaGroupPartContent = this - as dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent +public inline fun ResendableContent.mediaGroupCollectionContentOrThrow(): + MediaGroupCollectionContent = this as + dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent public inline fun - ResendableContent.ifVisualMediaGroupContent(block: (VisualMediaGroupPartContent) -> T): T? = - visualMediaGroupContentOrNull() ?.let(block) + ResendableContent.ifMediaGroupCollectionContent(block: (MediaGroupCollectionContent) -> T): T? = + mediaGroupCollectionContentOrNull() ?.let(block) + +public inline fun ResendableContent.mediaGroupPartContentOrNull(): MediaGroupPartContent? = this as? + dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent + +public inline fun ResendableContent.mediaGroupPartContentOrThrow(): MediaGroupPartContent = this as + dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent + +public inline fun + ResendableContent.ifMediaGroupPartContent(block: (MediaGroupPartContent) -> T): T? = + mediaGroupPartContentOrNull() ?.let(block) + +public inline fun ResendableContent.visualMediaGroupPartContentOrNull(): + VisualMediaGroupPartContent? = this as? + dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent + +public inline fun ResendableContent.visualMediaGroupPartContentOrThrow(): + VisualMediaGroupPartContent = this as + dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent + +public inline fun + ResendableContent.ifVisualMediaGroupPartContent(block: (VisualMediaGroupPartContent) -> T): T? = + visualMediaGroupPartContentOrNull() ?.let(block) public inline fun ResendableContent.animationContentOrNull(): AnimationContent? = this as? dev.inmo.tgbotapi.types.message.content.AnimationContent @@ -3645,6 +3633,15 @@ public inline fun ResendableContent.ifStaticLocationContent(block: (StaticLocationContent) -> T): T? = staticLocationContentOrNull() ?.let(block) +public inline fun ResendableContent.mediaGroupContentOrNull(): MediaGroupContent? = this as? + dev.inmo.tgbotapi.types.message.content.MediaGroupContent + +public inline fun ResendableContent.mediaGroupContentOrThrow(): MediaGroupContent = this as + dev.inmo.tgbotapi.types.message.content.MediaGroupContent + +public inline fun ResendableContent.ifMediaGroupContent(block: (MediaGroupContent) -> T): T? = + mediaGroupContentOrNull() ?.let(block) + public inline fun ResendableContent.photoContentOrNull(): PhotoContent? = this as? dev.inmo.tgbotapi.types.message.content.PhotoContent @@ -4728,69 +4725,3 @@ public inline fun Update.unknownUpdateOrThrow(): UnknownUpdate = this as public inline fun Update.ifUnknownUpdate(block: (UnknownUpdate) -> T): T? = unknownUpdateOrNull() ?.let(block) - -public inline fun Update.channelPostMediaGroupUpdateOrNull(): ChannelPostMediaGroupUpdate? = this - as? dev.inmo.tgbotapi.types.update.media_group.ChannelPostMediaGroupUpdate - -public inline fun Update.channelPostMediaGroupUpdateOrThrow(): ChannelPostMediaGroupUpdate = this as - dev.inmo.tgbotapi.types.update.media_group.ChannelPostMediaGroupUpdate - -public inline fun - Update.ifChannelPostMediaGroupUpdate(block: (ChannelPostMediaGroupUpdate) -> T): T? = - channelPostMediaGroupUpdateOrNull() ?.let(block) - -public inline fun Update.editChannelPostMediaGroupUpdateOrNull(): EditChannelPostMediaGroupUpdate? = - this as? dev.inmo.tgbotapi.types.update.media_group.EditChannelPostMediaGroupUpdate - -public inline fun Update.editChannelPostMediaGroupUpdateOrThrow(): EditChannelPostMediaGroupUpdate = - this as dev.inmo.tgbotapi.types.update.media_group.EditChannelPostMediaGroupUpdate - -public inline fun - Update.ifEditChannelPostMediaGroupUpdate(block: (EditChannelPostMediaGroupUpdate) -> T): T? = - editChannelPostMediaGroupUpdateOrNull() ?.let(block) - -public inline fun Update.editMessageMediaGroupUpdateOrNull(): EditMessageMediaGroupUpdate? = this - as? dev.inmo.tgbotapi.types.update.media_group.EditMessageMediaGroupUpdate - -public inline fun Update.editMessageMediaGroupUpdateOrThrow(): EditMessageMediaGroupUpdate = this as - dev.inmo.tgbotapi.types.update.media_group.EditMessageMediaGroupUpdate - -public inline fun - Update.ifEditMessageMediaGroupUpdate(block: (EditMessageMediaGroupUpdate) -> T): T? = - editMessageMediaGroupUpdateOrNull() ?.let(block) - -public inline fun Update.mediaGroupUpdateOrNull(): MediaGroupUpdate? = this as? - dev.inmo.tgbotapi.types.update.media_group.MediaGroupUpdate - -public inline fun Update.mediaGroupUpdateOrThrow(): MediaGroupUpdate = this as - dev.inmo.tgbotapi.types.update.media_group.MediaGroupUpdate - -public inline fun Update.ifMediaGroupUpdate(block: (MediaGroupUpdate) -> T): T? = - mediaGroupUpdateOrNull() ?.let(block) - -public inline fun Update.sentMediaGroupUpdateOrNull(): SentMediaGroupUpdate? = this as? - dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate - -public inline fun Update.sentMediaGroupUpdateOrThrow(): SentMediaGroupUpdate = this as - dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate - -public inline fun Update.ifSentMediaGroupUpdate(block: (SentMediaGroupUpdate) -> T): T? = - sentMediaGroupUpdateOrNull() ?.let(block) - -public inline fun Update.editMediaGroupUpdateOrNull(): EditMediaGroupUpdate? = this as? - dev.inmo.tgbotapi.types.update.media_group.EditMediaGroupUpdate - -public inline fun Update.editMediaGroupUpdateOrThrow(): EditMediaGroupUpdate = this as - dev.inmo.tgbotapi.types.update.media_group.EditMediaGroupUpdate - -public inline fun Update.ifEditMediaGroupUpdate(block: (EditMediaGroupUpdate) -> T): T? = - editMediaGroupUpdateOrNull() ?.let(block) - -public inline fun Update.messageMediaGroupUpdateOrNull(): MessageMediaGroupUpdate? = this as? - dev.inmo.tgbotapi.types.update.media_group.MessageMediaGroupUpdate - -public inline fun Update.messageMediaGroupUpdateOrThrow(): MessageMediaGroupUpdate = this as - dev.inmo.tgbotapi.types.update.media_group.MessageMediaGroupUpdate - -public inline fun Update.ifMessageMediaGroupUpdate(block: (MessageMediaGroupUpdate) -> T): T? = - messageMediaGroupUpdateOrNull() ?.let(block) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt index 9f632f078f..7fdcdf54fb 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils import dev.inmo.tgbotapi.types.message.abstracts.* +import dev.inmo.tgbotapi.types.message.content.MediaGroupMessage import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.types.message.content.MessageContent @@ -36,6 +37,3 @@ inline fun AnonymousGroupContentMessage<*>.requireW inline fun CommonGroupContentMessage<*>.withContent() = if (content is T) { this as CommonGroupContentMessage } else { null } inline fun CommonGroupContentMessage<*>.requireWithContent() = withContent()!! - -inline fun MediaGroupMessage<*>.withContent() = if (content is T) { this as MediaGroupMessage } else { null } -inline fun MediaGroupMessage<*>.requireWithContent() = withContent()!! diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt index aaf91e4521..56ec9dc233 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/UpdateChatRetriever.kt @@ -3,21 +3,15 @@ package dev.inmo.tgbotapi.extensions.utils.extensions import dev.inmo.tgbotapi.abstracts.FromUser import dev.inmo.tgbotapi.abstracts.WithUser import dev.inmo.tgbotapi.extensions.utils.asUser -import dev.inmo.tgbotapi.extensions.utils.shortcuts.chat import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.update.ChatJoinRequestUpdate import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.types.update.media_group.* import dev.inmo.tgbotapi.utils.PreviewFeature @PreviewFeature fun Update.sourceChat(): Chat? = when (this) { - is MediaGroupUpdate -> when (this) { - is SentMediaGroupUpdate -> data.chat - is EditMediaGroupUpdate -> data.chat - } is BaseMessageUpdate -> data.chat is ChatJoinRequestUpdate -> data.chat else -> { diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt index 53d52cf338..91acd5419e 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/FlowsUpdatesFilter.kt @@ -8,7 +8,6 @@ import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage @@ -30,19 +29,6 @@ inline fun filterForContentMessage(): suspend (Cont inline fun Flow.filterContentMessages( ): Flow> = asContentMessagesFlow().mapNotNull(filterForContentMessage()) -@RiskFeature("This method is low-level") -inline fun Flow.filterMediaGroupMessages( -): Flow>> = map { - it.data.mapNotNull { message -> - if (message.content is T) { - @Suppress("UNCHECKED_CAST") - message as CommonMessage - } else { - null - } - } -} - /** * @param scopeToIncludeChannels This parameter is required when you want to include [textMessages] for channels too. * In this case will be created new channel which will aggregate messages from [FlowsUpdatesFilter.messagesFlow] and @@ -62,39 +48,9 @@ inline fun FlowsUpdatesFilter.filterContentMessages( } ?: messagesFlow).filterContentMessages() } -/** - * @param scopeToIncludeChannels This parameter is required when you want to include [SentMediaGroupUpdate] for channels - * too. In this case will be created new channel which will aggregate messages from [FlowsUpdatesFilter.messagesFlow] and - * [FlowsUpdatesFilter.channelPostsFlow]. In case it is null will be used [Flow]s mapping - */ -@Suppress("UNCHECKED_CAST") -@RiskFeature(lowLevelRiskFeatureMessage) -inline fun FlowsUpdatesFilter.filterMediaGroupMessages( - scopeToIncludeChannels: CoroutineScope? = null -): Flow>> { - return (scopeToIncludeChannels ?.let { scope -> - aggregateFlows( - scope, - messageMediaGroupsFlow, - channelPostMediaGroupsFlow - ) - } ?: messageMediaGroupsFlow).filterMediaGroupMessages() -} - fun FlowsUpdatesFilter.sentMessages( scopeToIncludeChannels: CoroutineScope? = null ): Flow> = filterContentMessages(scopeToIncludeChannels) -fun FlowsUpdatesFilter.sentMessagesWithMediaGroups( - scopeToIncludeChannels: CoroutineScope? = null -): Flow> = merge( - sentMessages(scopeToIncludeChannels), - mediaGroupMessages(scopeToIncludeChannels).flatMap { - it.mapNotNull { - @Suppress("UNCHECKED_CAST") - it as? ContentMessage - } - } -) fun Flow.animationMessages() = filterContentMessages() fun FlowsUpdatesFilter.animationMessages( @@ -105,12 +61,6 @@ fun Flow.audioMessages() = filterContentMessages(scopeToIncludeChannels) -fun FlowsUpdatesFilter.audioMessagesWithMediaGroups( - scopeToIncludeChannels: CoroutineScope? = null -) = merge( - filterContentMessages(scopeToIncludeChannels), - mediaGroupAudioMessages(scopeToIncludeChannels).flatten() -) fun Flow.contactMessages() = filterContentMessages() fun FlowsUpdatesFilter.contactMessages( @@ -126,12 +76,6 @@ fun Flow.documentMessages() = filterContentMessages(scopeToIncludeChannels) -fun FlowsUpdatesFilter.documentMessagesWithMediaGroups( - scopeToIncludeChannels: CoroutineScope? = null -) = merge( - filterContentMessages(scopeToIncludeChannels), - mediaGroupDocumentMessages(scopeToIncludeChannels).flatten() -) fun Flow.gameMessages() = filterContentMessages() fun FlowsUpdatesFilter.gameMessages( @@ -153,12 +97,6 @@ fun Flow.imageMessages() = photoMessages() fun FlowsUpdatesFilter.photoMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) -fun FlowsUpdatesFilter.photoMessagesWithMediaGroups( - scopeToIncludeChannels: CoroutineScope? = null -) = merge( - filterContentMessages(scopeToIncludeChannels), - mediaGroupPhotosMessages(scopeToIncludeChannels).flatten() -) /** * Shortcut for [photoMessages] */ @@ -166,9 +104,6 @@ fun FlowsUpdatesFilter.photoMessagesWithMediaGroups( inline fun FlowsUpdatesFilter.imageMessages( scopeToIncludeChannels: CoroutineScope? = null ) = photoMessages(scopeToIncludeChannels) -fun FlowsUpdatesFilter.imageMessagesWithMediaGroups( - scopeToIncludeChannels: CoroutineScope? = null -) = photoMessagesWithMediaGroups(scopeToIncludeChannels) fun Flow.pollMessages() = filterContentMessages() fun FlowsUpdatesFilter.pollMessages( @@ -194,12 +129,6 @@ fun Flow.videoMessages() = filterContentMessages(scopeToIncludeChannels) -fun FlowsUpdatesFilter.videoMessagesWithMediaGroups( - scopeToIncludeChannels: CoroutineScope? = null -) = merge( - filterContentMessages(scopeToIncludeChannels), - mediaGroupVideosMessages(scopeToIncludeChannels).flatten() -) fun Flow.videoNoteMessages() = filterContentMessages() fun FlowsUpdatesFilter.videoNoteMessages( @@ -210,34 +139,3 @@ fun Flow.voiceMessages() = filterContentMessages(scopeToIncludeChannels) - - -fun Flow.mediaGroupMessages() = filterMediaGroupMessages() -fun FlowsUpdatesFilter.mediaGroupMessages( - scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) - -fun Flow.mediaGroupPhotosMessages() = filterMediaGroupMessages() -fun FlowsUpdatesFilter.mediaGroupPhotosMessages( - scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) - -fun Flow.mediaGroupVideosMessages() = filterMediaGroupMessages() -fun FlowsUpdatesFilter.mediaGroupVideosMessages( - scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) - -fun Flow.mediaGroupVisualMessages() = filterMediaGroupMessages() -fun FlowsUpdatesFilter.mediaGroupVisualMessages( - scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) - -fun Flow.mediaGroupAudioMessages() = filterMediaGroupMessages() -fun FlowsUpdatesFilter.mediaGroupAudioMessages( - scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) - -fun Flow.mediaGroupDocumentMessages() = filterMediaGroupMessages() -fun FlowsUpdatesFilter.mediaGroupDocumentMessages( - scopeToIncludeChannels: CoroutineScope? = null -) = filterMediaGroupMessages(scopeToIncludeChannels) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt deleted file mode 100644 index 5b32fc4e99..0000000000 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/MediaGroupsShortcuts.kt +++ /dev/null @@ -1,69 +0,0 @@ -package dev.inmo.tgbotapi.extensions.utils.shortcuts - -import dev.inmo.tgbotapi.requests.send.media.SendMediaGroup -import dev.inmo.tgbotapi.types.* -import dev.inmo.tgbotapi.types.chat.Chat -import dev.inmo.tgbotapi.types.message.ForwardInfo -import dev.inmo.tgbotapi.types.message.abstracts.* -import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate - -val List>.forwardInfo: ForwardInfo? - get() = firstOrNull() ?.forwardInfo -val List>.replyTo: Message? - get() = firstOrNull() ?.replyTo -val List>.chat: Chat? - get() = firstOrNull() ?.chat -val List>.mediaGroupId: MediaGroupIdentifier? - get() = firstOrNull() ?.mediaGroupId - -val SentMediaGroupUpdate.forwardInfo: ForwardInfo? - get() = data.first().forwardInfo -val SentMediaGroupUpdate.replyTo: Message? - get() = data.first().replyTo -val SentMediaGroupUpdate.chat: Chat - get() = data.chat!! -val SentMediaGroupUpdate.mediaGroupId: MediaGroupIdentifier - get() = data.mediaGroupId!! - -fun List>.createResend( - chatId: ChatId, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyTo: MessageId? = null -) = SendMediaGroup( - chatId, - map { it.content.toMediaGroupMemberTelegramMedia() }, - threadId, - disableNotification, - protectContent, - replyTo -) - -fun List>.createResend( - chat: Chat, - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyTo: MessageId? = null -) = createResend( - chat.id, - threadId, - disableNotification, - protectContent, - replyTo -) - -fun SentMediaGroupUpdate.createResend( - threadId: MessageThreadId? = null, - disableNotification: Boolean = false, - protectContent: Boolean = false, - replyTo: MessageId? = null -) = data.createResend( - chat, - threadId, - disableNotification, - protectContent, - replyTo -) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/BaseMessagesUpdatesConversations.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/BaseMessagesUpdatesConversations.kt index 0caa1805cc..2eae2350b0 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/BaseMessagesUpdatesConversations.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/BaseMessagesUpdatesConversations.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.extensions.utils.updates import dev.inmo.tgbotapi.types.update.abstracts.* -import dev.inmo.tgbotapi.types.update.media_group.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.filterIsInstance @@ -16,22 +15,3 @@ fun Flow.onlySentMessageUpdates(): Flow.onlyEditMessageUpdates(): Flow = filterIsInstance() - -/** - * Converts flow to [Flow] of [MediaGroupUpdate]. Please, remember that it could be either [EditMediaGroupUpdate] - * or [SentMediaGroupUpdate] - * - * @see onlySentMediaGroupUpdates - * @see onlyEditMediaGroupUpdates - */ -fun Flow.onlyMediaGroupsUpdates(): Flow = filterIsInstance() - -/** - * Converts flow to [Flow] of [SentMediaGroupUpdate] - */ -fun Flow.onlySentMediaGroupUpdates(): Flow = filterIsInstance() - -/** - * Converts flow to [Flow] of [EditMediaGroupUpdate] - */ -fun Flow.onlyEditMediaGroupUpdates(): Flow = filterIsInstance() diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesChatFilters.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesChatFilters.kt index 70be37786a..bf415ce45e 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesChatFilters.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/UpdatesChatFilters.kt @@ -3,7 +3,6 @@ package dev.inmo.tgbotapi.extensions.utils.updates import dev.inmo.tgbotapi.types.ChatId import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.filter @@ -15,13 +14,3 @@ fun Flow.filterBaseMessageUpdatesByChatId(chatId: Cha * [Flow.filter] incoming [BaseMessageUpdate]s by their [ChatId] using [Chat.id] of [chat] */ fun Flow.filterBaseMessageUpdatesByChat(chat: Chat): Flow = filterBaseMessageUpdatesByChatId(chat.id) - - -/** - * [Flow.filter] incoming [SentMediaGroupUpdate]s by their [ChatId] - */ -fun Flow.filterSentMediaGroupUpdatesByChatId(chatId: ChatId): Flow = filter { it.data.first().chat.id == chatId } -/** - * [Flow.filter] incoming [SentMediaGroupUpdate]s by their [ChatId] using [Chat.id] of [chat] - */ -fun Flow.filterSentMediaGroupUpdatesByChat(chat: Chat): Flow = filterSentMediaGroupUpdatesByChatId(chat.id) diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index 3e9f27dfaa..cb4f10b0db 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -8,9 +8,11 @@ import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates import dev.inmo.tgbotapi.extensions.utils.updates.lastUpdateIdentifier import dev.inmo.tgbotapi.requests.GetUpdates import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.content.MediaGroupContent import dev.inmo.tgbotapi.types.update.* +import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.types.update.media_group.SentMediaGroupUpdate import dev.inmo.tgbotapi.updateshandlers.* import io.ktor.client.plugins.HttpRequestTimeoutException import io.ktor.utils.io.CancellationException @@ -56,7 +58,10 @@ fun TelegramBot.longPollingFlow( * We are throw out the last media group and will reretrieve it again in the next get updates * and it will guarantee that it is full */ - if (originalUpdates.size == getUpdatesLimit.last && converted.last() is SentMediaGroupUpdate) { + if ( + originalUpdates.size == getUpdatesLimit.last + && ((converted.last() as? BaseSentMessageUpdate) ?.data as? CommonMessage<*>) ?.content is MediaGroupContent + ) { converted - converted.last() } else { converted @@ -67,7 +72,7 @@ fun TelegramBot.longPollingFlow( for (update in updates) { send(update) - lastUpdateIdentifier = update.lastUpdateIdentifier() + lastUpdateIdentifier = update.updateId } }.onFailure { cancel(it as? CancellationException ?: return@onFailure) From 4736610aa8eef58f6e88ba0c36a943e223c8ccc0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 8 Nov 2022 02:50:22 +0600 Subject: [PATCH 4/5] several fixes --- .../MessageFilterExcludingMediaGroups.kt | 2 +- .../MediaGroupMessagesTriggers.kt | 164 ++++++++++++++++++ .../triggers_handling/MediaGroupTriggers.kt | 164 ++++++++++++++++++ .../marker_factories/AnyMarkerFactory.kt | 5 + .../requests/send/media/SendMediaGroup.kt | 12 +- .../types/message/content/AbstractsMedia.kt | 10 +- .../message/content/MediaGroupContent.kt | 6 +- .../types/message/content/Typealiases.kt | 2 +- .../MediaGroupContentMessageCreator.kt | 2 +- .../tgbotapi/extensions/utils/ClassCasts.kt | 10 +- .../extensions/utils/ClassCastsNew.kt | 27 +-- .../utils/updates/retrieving/LongPolling.kt | 2 +- 12 files changed, 371 insertions(+), 35 deletions(-) create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupMessagesTriggers.kt create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/AnyMarkerFactory.kt diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt index aa22050056..f6d4fc2b76 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/filters/MessageFilterExcludingMediaGroups.kt @@ -11,5 +11,5 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update * Allow only messages which are not [MediaGroupMessage] */ val CommonMessageFilterExcludeMediaGroups = SimpleFilter { - it !is CommonMessage<*> || it.content !is MediaGroupContent + it !is CommonMessage<*> || it.content !is MediaGroupContent<*> } diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupMessagesTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupMessagesTriggers.kt new file mode 100644 index 0000000000..b4f80f4fd5 --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupMessagesTriggers.kt @@ -0,0 +1,164 @@ +@file:Suppress("unused") + +package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling + +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory +import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull +import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.types.update.abstracts.Update +import dev.inmo.tgbotapi.utils.PreviewFeature + +@PreviewFeature +internal suspend inline fun BC.buildMediaGroupMessagesTrigger( + initialFilter: SimpleFilter>? = null, + noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + it.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull>() ?.let { + if (it.content.group.all { it.content is T }) { + listOf(it as MediaGroupMessage) + } else { + null + } + } ?: emptyList() +} + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onMediaGroupMessages( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupMessagesTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onPlaylistMessages( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupMessagesTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onDocumentsGroupMessages( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupMessagesTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onVisualGalleryMessages( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupMessagesTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onVisualMediaGroupMessages( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = onVisualGalleryMessages(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onPhotoGalleryMessages( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupMessagesTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onVideoGalleryMessages( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupMessagesTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt new file mode 100644 index 0000000000..16057119ae --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MediaGroupTriggers.kt @@ -0,0 +1,164 @@ +@file:Suppress("unused") + +package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling + +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.AnyMarkerFactory +import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory +import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull +import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.types.update.abstracts.Update +import dev.inmo.tgbotapi.utils.PreviewFeature + +@PreviewFeature +internal suspend inline fun BC.buildMediaGroupTrigger( + initialFilter: SimpleFilter>? = null, + noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { + it.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull>() ?.let { + if (it.content.group.all { it.content is T }) { + listOf(it.content as MediaGroupContent) + } else { + null + } + } ?: emptyList() +} + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onMediaGroup( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onPlaylist( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onDocumentsGroup( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onVisualGallery( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onVisualMediaGroup( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = onVisualGallery(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onPhotoGallery( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) + +/** + * @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call + * @param subcontextUpdatesFilter This filter will be applied to each update inside of [scenarioReceiver]. For example, + * this filter will be used if you will call [dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage]. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextAndTwoTypesReceiver] function to create your own. + * Use [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus] or [dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times] + * to combinate several filters + * @param [markerFactory] Will be used to identify different "stream". [scenarioReceiver] will be called synchronously + * in one "stream". Output of [markerFactory] will be used as a key for "stream" + * @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that + * data + */ +suspend fun BC.onVideoGallery( + initialFilter: SimpleFilter>? = null, + subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver, Update>? = null, + markerFactory: MarkerFactory, Any> = AnyMarkerFactory(), + scenarioReceiver: CustomBehaviourContextAndTypeReceiver> +) = buildMediaGroupTrigger(initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/AnyMarkerFactory.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/AnyMarkerFactory.kt new file mode 100644 index 0000000000..dcf3059fcd --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/marker_factories/AnyMarkerFactory.kt @@ -0,0 +1,5 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories + +private val commonAnyMarker = MarkerFactory { it } + +fun AnyMarkerFactory() = commonAnyMarker as MarkerFactory diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt index cdca64dc56..c3c599aab9 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/media/SendMediaGroup.kt @@ -122,16 +122,16 @@ inline fun SendVisualMediaGroup( allowSendingWithoutReply: Boolean? = null ) = SendMediaGroup(chatId, media, threadId, disableNotification, protectContent, replyToMessageId, allowSendingWithoutReply) -private object MessagesListSerializer: KSerializer> { +private object MessagesListSerializer: KSerializer>> { private val serializer = ListSerializer(TelegramBotAPIMessageDeserializeOnlySerializerClass>()) override val descriptor: SerialDescriptor = serializer.descriptor - override fun deserialize(decoder: Decoder): PossiblySentViaBotCommonMessage { + override fun deserialize(decoder: Decoder): PossiblySentViaBotCommonMessage> { val messages = serializer.deserialize(decoder) return messages.asMediaGroupMessage() } - override fun serialize(encoder: Encoder, value: PossiblySentViaBotCommonMessage) { + override fun serialize(encoder: Encoder, value: PossiblySentViaBotCommonMessage>) { serializer.serialize(encoder, value.content.group.map { it.sourceMessage }) } @@ -152,8 +152,8 @@ data class SendMediaGroupData internal constructor( override val replyToMessageId: MessageId? = null, @SerialName(allowSendingWithoutReplyField) override val allowSendingWithoutReply: Boolean? = null -) : DataRequest>, - SendMessageRequest> { +) : DataRequest>>, + SendMessageRequest>> { @SerialName(mediaField) private val convertedMedia: String get() = buildJsonArray { @@ -166,7 +166,7 @@ data class SendMediaGroupData internal constructor( override fun method(): String = "sendMediaGroup" override val requestSerializer: SerializationStrategy<*> get() = serializer() - override val resultDeserializer: DeserializationStrategy> + override val resultDeserializer: DeserializationStrategy>> get() = MessagesListSerializer } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt index 3f5cc6adcc..21dd74142f 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/AbstractsMedia.kt @@ -24,14 +24,14 @@ sealed interface DocumentMediaGroupPartContent : MediaGroupPartContent { sealed interface TextedMediaContent : MediaContent, TextedInput -sealed interface MediaGroupCollectionContent : TextedMediaContent { +sealed interface MediaGroupCollectionContent : TextedMediaContent { @Serializable - data class PartWrapper( + data class PartWrapper( val messageId: MessageId, - val content: MediaGroupPartContent, - val sourceMessage: PossiblySentViaBotCommonMessage + val content: T, + val sourceMessage: PossiblySentViaBotCommonMessage ) - val group: List + val group: List> val mediaGroupId: MediaGroupIdentifier } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt index 2af4378fae..8af2a53a34 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/MediaGroupContent.kt @@ -14,10 +14,10 @@ import dev.inmo.tgbotapi.types.message.textsources.TextSource import kotlinx.serialization.Serializable @Serializable -data class MediaGroupContent( - override val group: List, +data class MediaGroupContent( + override val group: List>, override val mediaGroupId: MediaGroupIdentifier -) : MediaGroupCollectionContent { +) : MediaGroupCollectionContent { val mainContent: MediaGroupPartContent get() = group.first().content override val media: TelegramMediaFile diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt index 03ca71bfeb..9ed6c11430 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/content/Typealiases.kt @@ -20,7 +20,7 @@ typealias VideoNoteMessage = CommonMessage typealias StickerMessage = CommonMessage typealias TextedMediaMessage = CommonMessage typealias VoiceMessage = CommonMessage -typealias MediaGroupMessage = CommonMessage +typealias MediaGroupMessage = CommonMessage> typealias AudioMediaGroupMessage = CommonMessage typealias AudioMessage = CommonMessage typealias DocumentMediaGroupMessage = CommonMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt index e2524c74da..dcf3b4d3eb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/extensions/MediaGroupContentMessageCreator.kt @@ -27,7 +27,7 @@ import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent import dev.inmo.tgbotapi.utils.RiskFeature @RiskFeature("This API is experimental and can be changed without any notice, use with caution") -fun List>.asMediaGroupMessage(): PossiblySentViaBotCommonMessage { +fun List>.asMediaGroupMessage(): PossiblySentViaBotCommonMessage> { val sourceMessage = first() val content = MediaGroupContent( map { MediaGroupCollectionContent.PartWrapper(it.messageId, it.content, it) }, diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index 0bf559a79a..27b1e6f8fa 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -1194,16 +1194,16 @@ inline fun Message.requireGroupContentMessage(): GroupContentMessage @PreviewFeature -inline fun Message.whenMediaGroupMessage(block: (MediaGroupMessage) -> T) = +inline fun Message.whenMediaGroupMessage(block: (MediaGroupMessage) -> T) = asMediaGroupMessage()?.let(block) @PreviewFeature -inline fun Message.asMediaGroupMessage(): MediaGroupMessage? = - this as? MediaGroupMessage +inline fun Message.asMediaGroupMessage(): MediaGroupMessage? = + this as? MediaGroupMessage @PreviewFeature -inline fun Message.requireMediaGroupMessage(): MediaGroupMessage = - this as MediaGroupMessage +inline fun Message.requireMediaGroupMessage(): MediaGroupMessage = + this as MediaGroupMessage @PreviewFeature inline fun Message.whenPossiblyEditedMessage(block: (PossiblyEditedMessage) -> T) = 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 123c9dfb6f..2f2929d78d 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 @@ -3509,16 +3509,16 @@ public inline fun ResendableContent.ifTextedMediaContent(block: (TextedMedia textedMediaContentOrNull() ?.let(block) public inline fun ResendableContent.mediaGroupCollectionContentOrNull(): - MediaGroupCollectionContent? = this as? - dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent + MediaGroupCollectionContent? = this as? + dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent public inline fun ResendableContent.mediaGroupCollectionContentOrThrow(): - MediaGroupCollectionContent = this as - dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent + MediaGroupCollectionContent = this as + dev.inmo.tgbotapi.types.message.content.MediaGroupCollectionContent public inline fun - ResendableContent.ifMediaGroupCollectionContent(block: (MediaGroupCollectionContent) -> T): T? = - mediaGroupCollectionContentOrNull() ?.let(block) + ResendableContent.ifMediaGroupCollectionContent(block: (MediaGroupCollectionContent) -> T): + T? = mediaGroupCollectionContentOrNull() ?.let(block) public inline fun ResendableContent.mediaGroupPartContentOrNull(): MediaGroupPartContent? = this as? dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent @@ -3633,14 +3633,17 @@ public inline fun ResendableContent.ifStaticLocationContent(block: (StaticLocationContent) -> T): T? = staticLocationContentOrNull() ?.let(block) -public inline fun ResendableContent.mediaGroupContentOrNull(): MediaGroupContent? = this as? - dev.inmo.tgbotapi.types.message.content.MediaGroupContent +public inline fun ResendableContent.mediaGroupContentOrNull(): + MediaGroupContent? = this as? + dev.inmo.tgbotapi.types.message.content.MediaGroupContent -public inline fun ResendableContent.mediaGroupContentOrThrow(): MediaGroupContent = this as - dev.inmo.tgbotapi.types.message.content.MediaGroupContent +public inline fun ResendableContent.mediaGroupContentOrThrow(): + MediaGroupContent = this as + dev.inmo.tgbotapi.types.message.content.MediaGroupContent -public inline fun ResendableContent.ifMediaGroupContent(block: (MediaGroupContent) -> T): T? = - mediaGroupContentOrNull() ?.let(block) +public inline fun + ResendableContent.ifMediaGroupContent(block: (MediaGroupContent) -> T): + T? = mediaGroupContentOrNull() ?.let(block) public inline fun ResendableContent.photoContentOrNull(): PhotoContent? = this as? dev.inmo.tgbotapi.types.message.content.PhotoContent diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index cb4f10b0db..7306a8946a 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -60,7 +60,7 @@ fun TelegramBot.longPollingFlow( */ if ( originalUpdates.size == getUpdatesLimit.last - && ((converted.last() as? BaseSentMessageUpdate) ?.data as? CommonMessage<*>) ?.content is MediaGroupContent + && ((converted.last() as? BaseSentMessageUpdate) ?.data as? CommonMessage<*>) ?.content is MediaGroupContent<*> ) { converted - converted.last() } else { From 6bc96162a8e6085c347a084871eba591128f18a7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 8 Nov 2022 12:17:56 +0600 Subject: [PATCH 5/5] add waiters and small improvement of triggers --- .../expectations/WaitMediaGroup.kt | 51 +++++++++++++++++++ .../expectations/WaitMediaGroupMessages.kt | 51 +++++++++++++++++++ .../triggers_handling/MainTrigger.kt | 19 +++++-- 3 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt new file mode 100644 index 0000000000..111c400f4a --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroup.kt @@ -0,0 +1,51 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull +import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull +import dev.inmo.tgbotapi.extensions.utils.withContent +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.utils.RiskFeature +import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage +import kotlinx.coroutines.flow.Flow + +@RiskFeature(lowLevelRiskFeatureMessage) +suspend inline fun BehaviourContext.buildMediaGroupWaiter( + initRequest: Request<*>? = null, + noinline errorFactory: NullableRequestBuilder<*> = { null } +): Flow> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update -> + update.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull>() ?.let { message -> + if (message.content.group.all { it is T }) { + listOf(message.content as MediaGroupContent) + } else { + null + } + } ?: emptyList() +} + +suspend fun BehaviourContext.waitMediaGroup( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPlaylist( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitDocumentsGroup( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVisualGallery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPhotoGallery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVideoGallery( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupWaiter(initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt new file mode 100644 index 0000000000..2b592a355e --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitMediaGroupMessages.kt @@ -0,0 +1,51 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull +import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull +import dev.inmo.tgbotapi.extensions.utils.withContent +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.types.message.content.* +import dev.inmo.tgbotapi.utils.RiskFeature +import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage +import kotlinx.coroutines.flow.Flow + +@RiskFeature(lowLevelRiskFeatureMessage) +suspend inline fun BehaviourContext.buildMediaGroupMessagesWaiter( + initRequest: Request<*>? = null, + noinline errorFactory: NullableRequestBuilder<*> = { null } +): Flow> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update -> + update.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull>() ?.let { message -> + if (message.content.group.all { it is T }) { + listOf(message as MediaGroupMessage) + } else { + null + } + } ?: emptyList() +} + +suspend fun BehaviourContext.waitMediaGroupMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPlaylistMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitDocumentsGroupMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVisualGalleryMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitPhotoGalleryMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) +suspend fun BehaviourContext.waitVideoGalleryMessages( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null } +) = buildMediaGroupMessagesWaiter(initRequest, errorFactory) diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt index 31f52a2e73..80c386c70c 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt @@ -13,11 +13,20 @@ internal suspend inline fun BC.on( noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver? = null, noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver, noinline updateToData: (Update) -> List? -) = flowsUpdatesFilter.expectFlow(bot) { - updateToData(it) ?.mapNotNull { data -> - if (initialFilter ?.invoke(data) != false) it to data else null - } ?: emptyList() -}.subscribeSafelyWithoutExceptionsAsync( +) = flowsUpdatesFilter.expectFlow( + bot, + filter = initialFilter ?.let { + { + updateToData(it) ?.mapNotNull { data -> + if (initialFilter(data)) it to data else null + } ?: emptyList() + } + } ?: { + updateToData(it) ?.mapNotNull { data -> + it to data + } ?: emptyList() + } +).subscribeSafelyWithoutExceptionsAsync( scope, { markerFactory(it.second) } ) { (update, triggerData) ->