From 1f40ce1a819469a42fb1048a7f949c83dbf75d48 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 12 Nov 2020 22:35:27 +0600 Subject: [PATCH] add a lot of shortcuts for events --- CHANGELOG.md | 4 + .../ChatEventsSourcesConversations.kt | 15 ++-- .../utils/shortcuts/EventsShortcuts.kt | 89 ++++++++++++------- 3 files changed, 66 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1445175932f..fd46ba7a2d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,11 @@ * `Flow>#onlyGroupEvents` * `Flow>#onlySupergroupEvents` * A lot of extensions for `Flow` has been added: + * `FlowsUpdatesFilter#events` * `FlowsUpdatesFilter#channelEvents` + * `FlowsUpdatesFilter#groupEvents` + * `FlowsUpdatesFilter#supergroupEvents` + * And a lot of other filters with specific types ## 0.30.3 diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/chat_events/ChatEventsSourcesConversations.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/chat_events/ChatEventsSourcesConversations.kt index af0a1bad5cb..43504fef314 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/chat_events/ChatEventsSourcesConversations.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/chat_events/ChatEventsSourcesConversations.kt @@ -1,9 +1,6 @@ package dev.inmo.tgbotapi.extensions.utils.chat_events import dev.inmo.tgbotapi.extensions.utils.shortcuts.* -import dev.inmo.tgbotapi.types.message.* -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 import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.mapNotNull @@ -19,9 +16,9 @@ fun > Flow>.divideBySource(contentTy } } -@Deprecated("Replaced and renamed", ReplaceWith("allChannelEvents", "dev.inmo.tgbotapi.extensions.utils.shortcuts.allChannelEvents")) -fun Flow>.onlyChannelEvents() = allChannelEvents() -@Deprecated("Replaced and renamed", ReplaceWith("allGroupEvents", "dev.inmo.tgbotapi.extensions.utils.shortcuts.allGroupEvents")) -fun Flow>.onlyGroupEvents() = allGroupEvents() -@Deprecated("Replaced and renamed", ReplaceWith("allSupergroupEvents", "dev.inmo.tgbotapi.extensions.utils.shortcuts.allSupergroupEvents")) -fun Flow>.onlySupergroupEvents() = allSupergroupEvents() +@Deprecated("Replaced and renamed", ReplaceWith("channelEvents", "dev.inmo.tgbotapi.extensions.utils.shortcuts.channelEvents")) +fun Flow>.onlyChannelEvents() = channelEvents() +@Deprecated("Replaced and renamed", ReplaceWith("groupEvents", "dev.inmo.tgbotapi.extensions.utils.shortcuts.groupEvents")) +fun Flow>.onlyGroupEvents() = groupEvents() +@Deprecated("Replaced and renamed", ReplaceWith("supergroupEvents", "dev.inmo.tgbotapi.extensions.utils.shortcuts.supergroupEvents")) +fun Flow>.onlySupergroupEvents() = supergroupEvents() 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 index 381af8efa41..c9f0831301b 100644 --- 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 @@ -1,4 +1,4 @@ -@file:Suppress("NOTHING_TO_INLINE", "unused") +@file:Suppress("NOTHING_TO_INLINE", "unused", "EXPERIMENTAL_API_USAGE") package dev.inmo.tgbotapi.extensions.utils.shortcuts @@ -33,43 +33,66 @@ inline fun FlowsUpdatesFilter.supergroupEvents(): Flow @RiskFeature("Use with caution") inline fun > Flow>.filterByChatEvent(): Flow = mapNotNull { - if (it.chatEvent is T) { - it as? O - } else { - null - } + 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() -inline fun Flow>.allChannelEvents() = channelEvents() +inline fun Flow>.filterChannelEvents() = filterByChatEvent>() +@RiskFeature("Use with caution") +inline fun FlowsUpdatesFilter.filterChannelEvents() = channelEvents().filterChannelEvents() +inline fun Flow>.channelCreatedEvents() = filterChannelEvents() +inline fun FlowsUpdatesFilter.channelCreatedEvents() = filterChannelEvents() +inline fun Flow>.deletedChannelPhotoEvents() = filterChannelEvents() +inline fun FlowsUpdatesFilter.deletedChannelPhotoEvents() = filterChannelEvents() +inline fun Flow>.newChannelPhotoEvents() = filterChannelEvents() +inline fun FlowsUpdatesFilter.newChannelPhotoEvents() = filterChannelEvents() +inline fun Flow>.newChannelTitleEvents() = filterChannelEvents() +inline fun FlowsUpdatesFilter.newChannelTitleEvents() = filterChannelEvents() +inline fun Flow>.newChannelPinnedMessageEvents() = filterChannelEvents() +inline fun FlowsUpdatesFilter.newChannelPinnedMessageEvents() = filterChannelEvents() +inline fun Flow>.channelEvents() = filterChannelEvents() @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() -inline fun Flow>.allGroupEvents() = groupEvents() +inline fun Flow>.filterGroupEvents() = filterByChatEvent>() +@RiskFeature("Use with caution") +inline fun FlowsUpdatesFilter.filterGroupEvents() = groupEvents().filterByChatEvent>() +inline fun Flow>.groupCreatedEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.groupCreatedEvents() = filterGroupEvents() +inline fun Flow>.deletedGroupPhotoEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.deletedGroupPhotoEvents() = filterGroupEvents() +inline fun Flow>.newGroupMembersEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.newGroupMembersEvents() = filterGroupEvents() +inline fun Flow>.leftGroupMemberEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.leftGroupMemberEvents() = filterGroupEvents() +inline fun Flow>.newGroupPhotoEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.newGroupPhotoEvents() = filterGroupEvents() +inline fun Flow>.newGroupTitleEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.newGroupTitleEvents() = filterGroupEvents() +inline fun Flow>.newGroupPinnedMessageEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.newGroupPinnedMessageEvents() = filterGroupEvents() +inline fun Flow>.proximityAlertTriggeredInGroupEvents() = filterGroupEvents() +inline fun FlowsUpdatesFilter.proximityAlertTriggeredInGroupEvents() = filterGroupEvents() +inline fun Flow>.groupEvents() = filterGroupEvents() @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() -inline fun Flow>.allSupergroupEvents() = supergroupEvents() +inline fun Flow>.filterSupergroupEvents() = filterByChatEvent>() +@RiskFeature("Use with caution") +inline fun FlowsUpdatesFilter.filterSupergroupEvents() = supergroupEvents().filterByChatEvent>() +inline fun Flow>.supergroupCreatedEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.supergroupCreatedEvents() = filterSupergroupEvents() +inline fun Flow>.deletedSupergroupPhotoEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.deletedSupergroupPhotoEvents() = filterSupergroupEvents() +inline fun Flow>.newSupergroupMembersEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.newSupergroupMembersEvents() = filterSupergroupEvents() +inline fun Flow>.leftSupergroupMemberEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.leftSupergroupMemberEvents() = filterSupergroupEvents() +inline fun Flow>.newSupergroupPhotoEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.newSupergroupPhotoEvents() = filterSupergroupEvents() +inline fun Flow>.newSupergroupTitleEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.newSupergroupTitleEvents() = filterSupergroupEvents() +inline fun Flow>.newSupergroupPinnedMessageEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.newSupergroupPinnedMessageEvents() = filterSupergroupEvents() +inline fun Flow>.proximityAlertTriggeredInSupergroupEvents() = filterSupergroupEvents() +inline fun FlowsUpdatesFilter.proximityAlertTriggeredInSupergroupEvents() = filterSupergroupEvents() +inline fun Flow>.supergroupEvents() = filterSupergroupEvents()