From 72cf38d3bb05ee3f1d78f911b527174ade65bbad Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 12 Mar 2021 14:36:10 +0600 Subject: [PATCH] fill Behaviour Builder for ChatMemberUpdated --- .../expectations/WaitChatMemberUpdated.kt | 65 +++++++++++++++++++ .../ChatMemberUpdatedTriggers.kt | 61 +++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt create mode 100644 tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt new file mode 100644 index 0000000000..27827be3e1 --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitChatMemberUpdated.kt @@ -0,0 +1,65 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations + +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate +import dev.inmo.tgbotapi.requests.abstracts.Request +import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery +import dev.inmo.tgbotapi.types.CallbackQuery.DataCallbackQuery +import dev.inmo.tgbotapi.types.ChatMemberUpdated +import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate +import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate +import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate +import kotlinx.coroutines.flow.toList + +typealias ChatMemberUpdatedMapper = T.() -> T? + +private suspend inline fun BehaviourContext.waitChatMemberUpdated( + count: Int = 1, + initRequest: Request<*>? = null, + noinline errorFactory: NullableRequestBuilder<*> = { null }, + noinline mapper: ChatMemberUpdatedMapper +): List = expectFlow( + initRequest, + count, + errorFactory +) { + (it as? T) ?.data.let(::listOfNotNull) +}.toList().toList() + +private suspend inline fun BehaviourContext.waitChatMemberUpdated( + count: Int = 1, + initRequest: Request<*>? = null, + noinline errorFactory: NullableRequestBuilder<*> = { null }, + noinline filter: ChatMemberUpdatedMapper? = null +) : List = waitChatMemberUpdated( + count, + initRequest, + errorFactory +) { + if (filter == null) { + this + } else { + filter(this) + } +} + +suspend fun BehaviourContext.waitChatMemberUpdated( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, + count: Int = 1, + filter: ChatMemberUpdatedMapper? = null +) = waitChatMemberUpdated(count, initRequest, errorFactory, filter) + +suspend fun BehaviourContext.waitCommonChatMemberUpdated( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, + count: Int = 1, + filter: ChatMemberUpdatedMapper? = null +) = waitChatMemberUpdated(count, initRequest, errorFactory, filter) + +suspend fun BehaviourContext.waitMyChatMemberUpdated( + initRequest: Request<*>? = null, + errorFactory: NullableRequestBuilder<*> = { null }, + count: Int = 1, + filter: ChatMemberUpdatedMapper? = null +) = waitChatMemberUpdated(count, initRequest, errorFactory, filter) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt new file mode 100644 index 0000000000..180702731e --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/ChatMemberUpdatedTriggers.kt @@ -0,0 +1,61 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling + +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow +import dev.inmo.tgbotapi.extensions.utils.asChatMemberUpdatedUpdate +import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat +import dev.inmo.tgbotapi.types.ChatMemberUpdated +import dev.inmo.tgbotapi.types.update.CommonChatMemberUpdatedUpdate +import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate +import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate + +internal suspend inline fun BehaviourContext.onChatMemberUpdated( + includeFilterByChatInBehaviourSubContext: Boolean = true, + noinline additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, + noinline scenarioReceiver: BehaviourContextAndTypeReceiver +) = flowsUpdatesFilter.expectFlow(bot) { + (it as? U) ?.data ?.let { chatMemberUpdated -> + if (additionalFilter == null || additionalFilter(chatMemberUpdated)) chatMemberUpdated else null + }.let(::listOfNotNull) +}.subscribeSafelyWithoutExceptions(scope) { triggerChatMemberUpdated -> + doInSubContextWithUpdatesFilter( + updatesFilter = if (includeFilterByChatInBehaviourSubContext) { + { it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId } + } else { + null + } + ) { + scenarioReceiver(triggerChatMemberUpdated) + } +} + +suspend fun BehaviourContext.onChatMemberUpdated( + includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onChatMemberUpdated( + includeFilterByChatInBehaviourSubContext, + additionalFilter, + scenarioReceiver +) + +suspend fun BehaviourContext.onCommonChatMemberUpdated( + includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onChatMemberUpdated( + includeFilterByChatInBehaviourSubContext, + additionalFilter, + scenarioReceiver +) + +suspend fun BehaviourContext.onMyChatMemberUpdated( + includeFilterByChatInBehaviourSubContext: Boolean = true, + additionalFilter: (suspend (ChatMemberUpdated) -> Boolean)? = null, + scenarioReceiver: BehaviourContextAndTypeReceiver +) = onChatMemberUpdated( + includeFilterByChatInBehaviourSubContext, + additionalFilter, + scenarioReceiver +)