From 33b50c6c681a20976e9127723548179b58b1a8e4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 12 Nov 2020 21:17:11 +0600 Subject: [PATCH] events filters --- .../types/message/ChannelEventMessage.kt | 7 +- .../types/message/CommonGroupEventMessage.kt | 8 +- .../message/CommonSupergroupEventMessage.kt | 9 ++- .../message/abstracts/ChatEventMessage.kt | 4 +- .../message/abstracts/GroupEventMessage.kt | 4 +- .../abstracts/SupergroupEventMessage.kt | 4 +- .../utils/shortcuts/CommandsShortcuts.kt | 7 +- .../utils/shortcuts/EventsShortcuts.kt | 74 +++++++++++++++++++ 8 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/EventsShortcuts.kt diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt index ce972e7d52..009dd19ef7 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/ChannelEventMessage.kt @@ -4,11 +4,12 @@ import com.soywiz.klock.DateTime import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.chat.abstracts.ChannelChat import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ChannelEvent +import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage -data class ChannelEventMessage( +data class ChannelEventMessage( override val messageId: MessageIdentifier, override val chat: ChannelChat, - override val chatEvent: ChannelEvent, + override val chatEvent: T, override val date: DateTime -) : ChatEventMessage +) : ChatEventMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt index d55134b6c5..d3c2edb7a4 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonGroupEventMessage.kt @@ -8,12 +8,12 @@ import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage @Deprecated("Renamed", ReplaceWith("CommonGroupEventMessage")) -typealias GroupEventMessage = CommonGroupEventMessage +typealias GroupEventMessage = CommonGroupEventMessage<*> -data class CommonGroupEventMessage( +data class CommonGroupEventMessage( override val messageId: MessageIdentifier, override val user: User, override val chat: GroupChat, - override val chatEvent: GroupEvent, + override val chatEvent: T, override val date: DateTime -) : GroupEventMessage +) : GroupEventMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt index f5a382888e..d79212f169 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/CommonSupergroupEventMessage.kt @@ -4,16 +4,17 @@ import com.soywiz.klock.DateTime import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.User import dev.inmo.tgbotapi.types.chat.abstracts.SupergroupChat +import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.SupergroupEvent import dev.inmo.tgbotapi.types.message.abstracts.SupergroupEventMessage @Deprecated("Renamed", ReplaceWith("CommonSupergroupEventMessage")) -typealias SupergroupEventMessage = CommonSupergroupEventMessage +typealias SupergroupEventMessage = CommonSupergroupEventMessage<*> -data class CommonSupergroupEventMessage( +data class CommonSupergroupEventMessage( override val messageId: MessageIdentifier, override val user: User, override val chat: SupergroupChat, - override val chatEvent: SupergroupEvent, + override val chatEvent: T, override val date: DateTime -) : SupergroupEventMessage +) : SupergroupEventMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChatEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChatEventMessage.kt index abef861c5d..c409d53a0e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChatEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/ChatEventMessage.kt @@ -2,6 +2,6 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ChatEvent -interface ChatEventMessage : Message { - val chatEvent: ChatEvent +interface ChatEventMessage : Message { + val chatEvent: T } \ No newline at end of file diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt index 2ca03698de..f8988d489d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/GroupEventMessage.kt @@ -2,6 +2,4 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent -interface GroupEventMessage : ChatEventMessage, FromUserMessage { - override val chatEvent: GroupEvent -} +interface GroupEventMessage : ChatEventMessage, FromUserMessage diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt index d923fd0904..0e111cec2c 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/abstracts/SupergroupEventMessage.kt @@ -2,6 +2,4 @@ package dev.inmo.tgbotapi.types.message.abstracts import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.SupergroupEvent -interface SupergroupEventMessage : GroupEventMessage { - override val chatEvent: SupergroupEvent -} +interface SupergroupEventMessage : GroupEventMessage diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/CommandsShortcuts.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/CommandsShortcuts.kt index 5a8181819e..f23f1aae33 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/CommandsShortcuts.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/CommandsShortcuts.kt @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.extensions.utils.shortcuts import dev.inmo.tgbotapi.CommonAbstracts.TextSource +import dev.inmo.tgbotapi.CommonAbstracts.textSources import dev.inmo.tgbotapi.extensions.utils.onlyTextContentMessages import dev.inmo.tgbotapi.extensions.utils.updates.asContentMessagesFlow import dev.inmo.tgbotapi.types.MessageEntity.textsources.BotCommandTextSource @@ -27,7 +28,7 @@ import kotlinx.coroutines.flow.* fun > Flow.filterExactCommands( commandRegex: Regex ) = filter { contentMessage -> - (contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true + (contentMessage.content.textSources.singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true } /** @@ -46,7 +47,7 @@ fun > Flow.filterExactCommands( fun > Flow.filterCommandsInsideTextMessages( commandRegex: Regex ) = filter { contentMessage -> - contentMessage.content.fullEntitiesList().any { + contentMessage.content.textSources.any { (it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true } } @@ -70,7 +71,7 @@ fun > Flow.filterCommandsInsideTextMessages( fun > Flow.filterCommandsWithArgs( commandRegex: Regex ) = mapNotNull { contentMessage -> - val allEntities = contentMessage.content.fullEntitiesList() + val allEntities = contentMessage.content.textSources (allEntities.firstOrNull() as? BotCommandTextSource) ?.let { if (commandRegex.matches(it.command)) { contentMessage to allEntities.flatMap { diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/EventsShortcuts.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/EventsShortcuts.kt new file mode 100644 index 0000000000..4f3493d206 --- /dev/null +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/shortcuts/EventsShortcuts.kt @@ -0,0 +1,74 @@ +package dev.inmo.tgbotapi.extensions.utils.shortcuts + +import dev.inmo.tgbotapi.extensions.utils.aggregateFlows +import dev.inmo.tgbotapi.types.message.ChannelEventMessage +import dev.inmo.tgbotapi.types.message.ChatEvents.* +import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.* +import dev.inmo.tgbotapi.types.message.abstracts.* +import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent +import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter +import dev.inmo.tgbotapi.utils.RiskFeature +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.* + +/** + * @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.messageFlow] and + * [FlowsUpdatesFilter.channelPostFlow]. In case it is null will be used [Flow]s mapping + */ +@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE") +@RiskFeature("Use with caution") +inline fun FlowsUpdatesFilter.filterChannelEvents(): Flow> = channelPostFlow.mapNotNull { + it.data as? ChannelEventMessage<*> +} + +/** + * @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.messageFlow] and + * [FlowsUpdatesFilter.channelPostFlow]. In case it is null will be used [Flow]s mapping + */ +@Suppress("UNCHECKED_CAST") +@RiskFeature("Use with caution") +inline fun > FlowsUpdatesFilter.filterGroupEvents(): Flow = messageFlow.mapNotNull { + it.data as? T +} + +@RiskFeature("Use with caution") +inline fun > Flow>.filterByChatEvent(): Flow = mapNotNull { + if (it.chatEvent is T) { + it as? O + } else { + null + } +} + +@RiskFeature("Use with caution") +inline fun Flow>.channelEvents() = filterByChatEvent>() +inline fun Flow>.channelCreatedEvents() = channelEvents() +inline fun Flow>.deletedChannelPhotoEvents() = channelEvents() +inline fun Flow>.newChannelPhotoEvents() = channelEvents() +inline fun Flow>.newChannelTitleEvents() = channelEvents() +inline fun Flow>.newChannelPinnedMessageEvents() = channelEvents() + +@RiskFeature("Use with caution") +inline fun Flow>.groupEvents() = filterByChatEvent>() +inline fun Flow>.groupCreatedEvents() = groupEvents() +inline fun Flow>.deletedGroupPhotoEvents() = groupEvents() +inline fun Flow>.newGroupMembersEvents() = groupEvents() +inline fun Flow>.leftGroupMemberEvents() = groupEvents() +inline fun Flow>.newGroupPhotoEvents() = groupEvents() +inline fun Flow>.newGroupTitleEvents() = groupEvents() +inline fun Flow>.newGroupPinnedMessageEvents() = groupEvents() +inline fun Flow>.proximityAlertTriggeredInGroupEvents() = groupEvents() + + +@RiskFeature("Use with caution") +inline fun Flow>.supergroupEvents() = filterByChatEvent>() +inline fun Flow>.supergroupCreatedEvents() = supergroupEvents() +inline fun Flow>.deletedSupergroupPhotoEvents() = supergroupEvents() +inline fun Flow>.newSupergroupMembersEvents() = supergroupEvents() +inline fun Flow>.leftSupergroupMemberEvents() = supergroupEvents() +inline fun Flow>.newSupergroupPhotoEvents() = supergroupEvents() +inline fun Flow>.newSupergroupTitleEvents() = supergroupEvents() +inline fun Flow>.newSupergroupPinnedMessageEvents() = supergroupEvents() +inline fun Flow>.proximityAlertTriggeredInSupergroupEvents() = supergroupEvents()