From 47fe048b10b987e1081b781b74414bcaf8c48ecf Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 27 Jun 2021 19:28:21 +0600 Subject: [PATCH] refactor and fixes in ContentTriggers --- .../triggers_handling/ContentTriggers.kt | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt index 6dcab2502f..81dfbbb894 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ContentTriggers.kt @@ -15,6 +15,7 @@ import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.abstracts.* import dev.inmo.tgbotapi.types.message.content.media.* import dev.inmo.tgbotapi.types.message.payments.InvoiceContent +import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.SentMediaGroupUpdate import dev.inmo.tgbotapi.utils.PreviewFeature typealias CommonMessageFilter = (suspend (CommonMessage) -> Boolean) @@ -27,26 +28,21 @@ internal suspend inline fun BehaviourContext.onCont markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, noinline scenarioReceiver: BehaviourContextAndTypeReceiver> ) = flowsUpdatesFilter.expectFlow(bot) { - if (includeMediaGroups) { - it.asSentMediaGroupUpdate() ?.data ?.mapNotNull { - if (it.content is T) { - val adaptedMessage = it as CommonMessage - if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null - } else { - null - } - } ?.let { - return@expectFlow it - } + val messages = it.whenBaseSentMessageUpdate { + it.data.whenCommonMessage(::listOfNotNull) + } ?: if (includeMediaGroups) { + it.asSentMediaGroupUpdate() ?.data ?: emptyList() + } else { + emptyList() } - it.asBaseSentMessageUpdate() ?.data ?.asCommonMessage() ?.let { message -> + messages.mapNotNull { message -> if (message.content is T) { val adaptedMessage = message as CommonMessage if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null } else { null } - }.let(::listOfNotNull) + } }.subscribeSafelyWithoutExceptionsAsync( scope, markerFactory::invoke @@ -66,9 +62,10 @@ internal suspend inline fun BehaviourContext.onCont suspend fun BehaviourContext.onContentMessage( includeFilterByChatInBehaviourSubContext: Boolean = true, additionalFilter: CommonMessageFilter? = null, + includeMediaGroups: Boolean = true, markerFactory: MarkerFactory, Any> = ByChatMessageMarkerFactory, scenarioReceiver: BehaviourContextAndTypeReceiver> -) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) +) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver) suspend fun BehaviourContext.onContact( includeFilterByChatInBehaviourSubContext: Boolean = true, additionalFilter: CommonMessageFilter? = null,