From 2d15e13ae693d806e1e7a61820a2a84225a7c8bb Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Aug 2020 01:08:44 +0600 Subject: [PATCH 1/6] started 0.27.11 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ec5dae49a..434c1b0c6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ * `closePollExactAfter` * `closePollAfter` +### 0.27.11 + ### 0.27.10 * `TelegramBotAPI-extensions-api`: diff --git a/gradle.properties b/gradle.properties index 2dd30909ce..248839347c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,6 +9,6 @@ ktor_version=1.3.2 javax_activation_version=1.1.1 library_group=com.github.insanusmokrassar -library_version=0.27.10 +library_version=0.27.11 gradle_bintray_plugin_version=1.8.4 From 6f650f6d6c42d0ce3f049bee0da3e21a8e99777b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Aug 2020 01:19:12 +0600 Subject: [PATCH 2/6] Flow extensions --- CHANGELOG.md | 20 ++++++++++++ .../utils/shortcuts/FlowsUpdatesFilter.kt | 32 ++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 434c1b0c6c..631a9a8d93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,26 @@ ### 0.27.11 +* `TelegramBotAPI-extensions-utils`: + * A lot of extensions like `Flow#textMessages` were added: + * `Flow#animationMessages` + * `Flow#audioMessages` + * `Flow#contactMessages` + * `Flow#diceMessages` + * `Flow#documentMessages` + * `Flow#gameMessages` + * `Flow#invoiceMessages` + * `Flow#locationMessages` + * `Flow#photoMessages` + * `Flow#imageMessages` + * `Flow#pollMessages` + * `Flow#stickerMessages` + * `Flow#textMessages` + * `Flow#venueMessages` + * `Flow#videoMessages` + * `Flow#videoNoteMessages` + * `Flow#voiceMessages` + ### 0.27.10 * `TelegramBotAPI-extensions-api`: diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt index 8b02c3924f..8776b894e8 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt @@ -3,10 +3,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.aggregateFlows import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.asContentMessagesFlow import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.InvoiceContent +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.* @@ -19,6 +21,10 @@ inline fun filterForContentMessage(): suspend (Cont } } +@Suppress("UNCHECKED_CAST") +inline fun Flow.filterContentMessages( +): Flow> = asContentMessagesFlow().mapNotNull(filterForContentMessage()) + /** * @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 agregate messages from [FlowsUpdatesFilter.messageFlow] and @@ -28,48 +34,57 @@ inline fun filterForContentMessage(): suspend (Cont inline fun FlowsUpdatesFilter.filterContentMessages( scopeToIncludeChannels: CoroutineScope? = null ): Flow> { - val filter = filterForContentMessage() - return scopeToIncludeChannels ?.let { scope -> + return (scopeToIncludeChannels ?.let { scope -> aggregateFlows( scope, - messageFlow.asContentMessagesFlow().mapNotNull(filter), - channelPostFlow.asContentMessagesFlow().mapNotNull(filter) + messageFlow, + channelPostFlow ) - } ?: messageFlow.asContentMessagesFlow().mapNotNull(filter) + } ?: messageFlow).filterContentMessages() } +fun Flow.animationMessages() = filterContentMessages() fun FlowsUpdatesFilter.animationMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.audioMessages() = filterContentMessages() fun FlowsUpdatesFilter.audioMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.contactMessages() = filterContentMessages() fun FlowsUpdatesFilter.contactMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.diceMessages() = filterContentMessages() fun FlowsUpdatesFilter.diceMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.documentMessages() = filterContentMessages() fun FlowsUpdatesFilter.documentMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.gameMessages() = filterContentMessages() fun FlowsUpdatesFilter.gameMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.invoiceMessages() = filterContentMessages() fun FlowsUpdatesFilter.invoiceMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.locationMessages() = filterContentMessages() fun FlowsUpdatesFilter.locationMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.photoMessages() = filterContentMessages() +fun Flow.imageMessages() = photoMessages() fun FlowsUpdatesFilter.photoMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) @@ -81,30 +96,37 @@ inline fun FlowsUpdatesFilter.imageMessages( scopeToIncludeChannels: CoroutineScope? = null ) = photoMessages(scopeToIncludeChannels) +fun Flow.pollMessages() = filterContentMessages() fun FlowsUpdatesFilter.pollMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.stickerMessages() = filterContentMessages() fun FlowsUpdatesFilter.stickerMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.textMessages() = filterContentMessages() fun FlowsUpdatesFilter.textMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.venueMessages() = filterContentMessages() fun FlowsUpdatesFilter.venueMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.videoMessages() = filterContentMessages() fun FlowsUpdatesFilter.videoMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.videoNoteMessages() = filterContentMessages() fun FlowsUpdatesFilter.videoNoteMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun Flow.voiceMessages() = filterContentMessages() fun FlowsUpdatesFilter.voiceMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) From 4f984d1dbc439943a5609433442f6948dab62a4c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Aug 2020 12:53:04 +0600 Subject: [PATCH 3/6] update documentation for FlowsAggregation --- .../TelegramBotAPI/extensions/utils/FlowsAggregation.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt index 37333b2cec..17c783a215 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt @@ -5,6 +5,9 @@ import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.* +/** + * Analog of [merge] function for [Flow]s. The difference is in the usage of [BroadcastChannel] in this case + */ fun aggregateFlows( withScope: CoroutineScope, vararg flows: Flow, From ce9f7f35dc1c389ec6f0eed108b441805f4254e6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Aug 2020 13:45:25 +0600 Subject: [PATCH 4/6] fix scope of one of startGettingOfUpdatesByLongPolling --- .../extensions/utils/updates/retrieving/LongPolling.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt index 6fbd655048..2e77fef119 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt @@ -114,7 +114,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( pollAnswerCallback: UpdateReceiver? = null, timeoutSeconds: Seconds = 30, exceptionsHandler: ExceptionHandler? = null, - scope: CoroutineScope = GlobalScope + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job { return startGettingOfUpdatesByLongPolling( SimpleUpdatesFilter( From 790959ea4935198756ec9244e2c985eac542c290 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Aug 2020 14:55:21 +0600 Subject: [PATCH 5/6] add a lot of extension for FlowUpdatesFilter and Flow --- CHANGELOG.md | 13 ++++ .../extensions/utils/FlowsAggregation.kt | 8 +++ .../utils/extensions/FlowsUpdatesFilter.kt | 19 ++++++ .../utils/shortcuts/FlowsUpdatesFilter.kt | 65 ++++++++++++++++++- 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FlowsUpdatesFilter.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 631a9a8d93..cd0f9d8e5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,16 @@ ### 0.27.11 * `TelegramBotAPI-extensions-utils`: + * `Flow>.flatMap` extension was added + * Extensions for `FlowUpdatesFilter` were added: + * `FlowsUpdatesFilter#allSentMessagesFlow` (combination of `messageFlow` and `channelPostFlow`) + * `FlowsUpdatesFilter#allSentMediaGroupsFlow` (combination of `messageMediaGroupFlow` and `channelPostMediaGroupFlow`) + * `FlowsUpdatesFilter#photoMessagesWithMediaGroups` + * `FlowsUpdatesFilter#imageMessagesWithMediaGroups` + * `FlowsUpdatesFilter#videoMessagesWithMediaGroups` + * `FlowsUpdatesFilter#mediaGroupMessages` + * `FlowsUpdatesFilter#mediaGroupPhotosMessages` + * `FlowsUpdatesFilter#mediaGroupVideosMessages` * A lot of extensions like `Flow#textMessages` were added: * `Flow#animationMessages` * `Flow#audioMessages` @@ -70,6 +80,9 @@ * `Flow#videoMessages` * `Flow#videoNoteMessages` * `Flow#voiceMessages` + * `Flow#mediaGroupMessages` + * `Flow#mediaGroupPhotosMessages` + * `Flow#mediaGroupVideosMessages` ### 0.27.10 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt index 17c783a215..eba50df519 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt @@ -21,3 +21,11 @@ fun aggregateFlows( } return bc.asFlow() } + +fun Flow>.flatMap(): Flow = flow { + collect { + it.forEach { + emit(it) + } + } +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FlowsUpdatesFilter.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FlowsUpdatesFilter.kt new file mode 100644 index 0000000000..51efd9f2cb --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/extensions/FlowsUpdatesFilter.kt @@ -0,0 +1,19 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.extensions + +import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.SentMediaGroupUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.merge + +val FlowsUpdatesFilter.allSentMessagesFlow: Flow + get() = merge( + messageFlow, + channelPostFlow + ) + +val FlowsUpdatesFilter.allSentMediaGroupsFlow: Flow + get() = merge( + messageMediaGroupFlow, + channelPostMediaGroupFlow + ) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt index 8776b894e8..2f968339d8 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt @@ -1,13 +1,16 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.aggregateFlows +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.flatMap import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.asContentMessagesFlow +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.InvoiceContent +import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.SentMediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter import kotlinx.coroutines.CoroutineScope @@ -25,9 +28,20 @@ inline fun filterForContentMessage(): suspend (Cont inline fun Flow.filterContentMessages( ): Flow> = asContentMessagesFlow().mapNotNull(filterForContentMessage()) +inline fun Flow.filterMediaGroupMessages( +): Flow>> = map { + it.data.mapNotNull { message -> + if (message.content is T) { + 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 agregate messages from [FlowsUpdatesFilter.messageFlow] and + * In this case will be created new channel which will aggregate messages from [FlowsUpdatesFilter.messageFlow] and * [FlowsUpdatesFilter.channelPostFlow]. In case it is null will be used [Flow]s mapping */ @Suppress("UNCHECKED_CAST") @@ -43,6 +57,24 @@ inline fun FlowsUpdatesFilter.filterContentMessages( } ?: messageFlow).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.messageFlow] and + * [FlowsUpdatesFilter.channelPostFlow]. In case it is null will be used [Flow]s mapping + */ +@Suppress("UNCHECKED_CAST") +inline fun FlowsUpdatesFilter.filterMediaGroupMessages( + scopeToIncludeChannels: CoroutineScope? = null +): Flow>> { + return (scopeToIncludeChannels ?.let { scope -> + aggregateFlows( + scope, + messageMediaGroupFlow, + channelPostMediaGroupFlow + ) + } ?: messageMediaGroupFlow).filterMediaGroupMessages() +} + fun Flow.animationMessages() = filterContentMessages() fun FlowsUpdatesFilter.animationMessages( scopeToIncludeChannels: CoroutineScope? = null @@ -88,6 +120,12 @@ fun Flow.imageMessages() = photoMessages() fun FlowsUpdatesFilter.photoMessages( scopeToIncludeChannels: CoroutineScope? = null ) = filterContentMessages(scopeToIncludeChannels) +fun FlowsUpdatesFilter.photoMessagesWithMediaGroups( + scopeToIncludeChannels: CoroutineScope? = null +) = merge( + filterContentMessages(scopeToIncludeChannels), + mediaGroupPhotosMessages(scopeToIncludeChannels).flatMap() +) /** * Shortcut for [photoMessages] */ @@ -95,6 +133,9 @@ fun FlowsUpdatesFilter.photoMessages( inline fun FlowsUpdatesFilter.imageMessages( scopeToIncludeChannels: CoroutineScope? = null ) = photoMessages(scopeToIncludeChannels) +fun FlowsUpdatesFilter.imageMessagesWithMediaGroups( + scopeToIncludeChannels: CoroutineScope? = null +) = photoMessagesWithMediaGroups(scopeToIncludeChannels) fun Flow.pollMessages() = filterContentMessages() fun FlowsUpdatesFilter.pollMessages( @@ -120,6 +161,12 @@ fun Flow.videoMessages() = filterContentMessages(scopeToIncludeChannels) +fun FlowsUpdatesFilter.videoMessagesWithMediaGroups( + scopeToIncludeChannels: CoroutineScope? = null +) = merge( + filterContentMessages(scopeToIncludeChannels), + mediaGroupVideosMessages(scopeToIncludeChannels).flatMap() +) fun Flow.videoNoteMessages() = filterContentMessages() fun FlowsUpdatesFilter.videoNoteMessages( @@ -132,3 +179,17 @@ fun FlowsUpdatesFilter.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) From d2228e274c039e741979b072e0eeff2ddce674ee Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Aug 2020 15:28:10 +0600 Subject: [PATCH 6/6] sentMessages extension for FlowsUpdatesFilter --- CHANGELOG.md | 2 ++ .../extensions/utils/FlowsAggregation.kt | 8 ++++++++ .../extensions/utils/shortcuts/FlowsUpdatesFilter.kt | 12 ++++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd0f9d8e5e..f8fbffb0d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,8 @@ * Extensions for `FlowUpdatesFilter` were added: * `FlowsUpdatesFilter#allSentMessagesFlow` (combination of `messageFlow` and `channelPostFlow`) * `FlowsUpdatesFilter#allSentMediaGroupsFlow` (combination of `messageMediaGroupFlow` and `channelPostMediaGroupFlow`) + * `FlowsUpdatesFilter#sentMessages` + * `FlowsUpdatesFilter#sentMessagesWithMediaGroups` * `FlowsUpdatesFilter#photoMessagesWithMediaGroups` * `FlowsUpdatesFilter#imageMessagesWithMediaGroups` * `FlowsUpdatesFilter#videoMessagesWithMediaGroups` diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt index eba50df519..89398c7e10 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/FlowsAggregation.kt @@ -29,3 +29,11 @@ fun Flow>.flatMap(): Flow = flow { } } } + +fun Flow.flatMap(mapper: (T) -> Iterable): Flow = flow { + collect { + mapper(it).forEach { + emit(it) + } + } +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt index 2f968339d8..ff7a3705f1 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/shortcuts/FlowsUpdatesFilter.kt @@ -75,6 +75,18 @@ inline fun FlowsUpdatesFilter.filterMediaGroupMes } ?: messageMediaGroupFlow).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 { it as? ContentMessage } + } +) + fun Flow.animationMessages() = filterContentMessages() fun FlowsUpdatesFilter.animationMessages( scopeToIncludeChannels: CoroutineScope? = null