1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

refactor and fixes in ContentTriggers

This commit is contained in:
InsanusMokrassar 2021-06-27 19:28:21 +06:00
parent ee8cc2aa46
commit 47fe048b10

View File

@ -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.abstracts.*
import dev.inmo.tgbotapi.types.message.content.media.* import dev.inmo.tgbotapi.types.message.content.media.*
import dev.inmo.tgbotapi.types.message.payments.InvoiceContent import dev.inmo.tgbotapi.types.message.payments.InvoiceContent
import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.SentMediaGroupUpdate
import dev.inmo.tgbotapi.utils.PreviewFeature import dev.inmo.tgbotapi.utils.PreviewFeature
typealias CommonMessageFilter<T> = (suspend (CommonMessage<T>) -> Boolean) typealias CommonMessageFilter<T> = (suspend (CommonMessage<T>) -> Boolean)
@ -27,26 +28,21 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
markerFactory: MarkerFactory<in CommonMessage<T>, Any> = ByChatMessageMarkerFactory, markerFactory: MarkerFactory<in CommonMessage<T>, Any> = ByChatMessageMarkerFactory,
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<T>> noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<T>>
) = flowsUpdatesFilter.expectFlow(bot) { ) = flowsUpdatesFilter.expectFlow(bot) {
if (includeMediaGroups) { val messages = it.whenBaseSentMessageUpdate {
it.asSentMediaGroupUpdate() ?.data ?.mapNotNull { it.data.whenCommonMessage(::listOfNotNull)
if (it.content is T) { } ?: if (includeMediaGroups) {
val adaptedMessage = it as CommonMessage<T> it.asSentMediaGroupUpdate() ?.data ?: emptyList()
if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null } else {
} else { emptyList()
null
}
} ?.let {
return@expectFlow it
}
} }
it.asBaseSentMessageUpdate() ?.data ?.asCommonMessage() ?.let { message -> messages.mapNotNull { message ->
if (message.content is T) { if (message.content is T) {
val adaptedMessage = message as CommonMessage<T> val adaptedMessage = message as CommonMessage<T>
if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null if (additionalFilter == null || additionalFilter(adaptedMessage)) adaptedMessage else null
} else { } else {
null null
} }
}.let(::listOfNotNull) }
}.subscribeSafelyWithoutExceptionsAsync( }.subscribeSafelyWithoutExceptionsAsync(
scope, scope,
markerFactory::invoke markerFactory::invoke
@ -66,9 +62,10 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
suspend fun BehaviourContext.onContentMessage( suspend fun BehaviourContext.onContentMessage(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<MessageContent>? = null, additionalFilter: CommonMessageFilter<MessageContent>? = null,
includeMediaGroups: Boolean = true,
markerFactory: MarkerFactory<in CommonMessage<MessageContent>, Any> = ByChatMessageMarkerFactory, markerFactory: MarkerFactory<in CommonMessage<MessageContent>, Any> = ByChatMessageMarkerFactory,
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MessageContent>> scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<MessageContent>>
) = onContent(includeFilterByChatInBehaviourSubContext, false, additionalFilter, markerFactory, scenarioReceiver) ) = onContent(includeFilterByChatInBehaviourSubContext, includeMediaGroups, additionalFilter, markerFactory, scenarioReceiver)
suspend fun BehaviourContext.onContact( suspend fun BehaviourContext.onContact(
includeFilterByChatInBehaviourSubContext: Boolean = true, includeFilterByChatInBehaviourSubContext: Boolean = true,
additionalFilter: CommonMessageFilter<ContactContent>? = null, additionalFilter: CommonMessageFilter<ContactContent>? = null,