From 130e00b62b4978acd05b1f8137ac7867583f7fa1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 5 Jun 2020 14:57:58 +0600 Subject: [PATCH] extensions for CommonMessage --- CHANGELOG.md | 2 ++ .../utils/CommonMessageConversations.kt | 34 +++++++++++++++++++ .../SentMessageUpdatesConversations.kt | 7 ++++ 3 files changed, 43 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 25c016e183..d0d8af500a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,8 @@ * Abstraction `ThumbedWithMimeTypeInlineQueryResult` with `thumbMimeType` field was added * `InlineQueryResultGif` and `InlineQueryResultMpeg4Gif` now extend `ThumbedWithMimeTypeInlineQueryResult` instead of `ThumbedInlineQueryResult` +* `TelegramBotAPI-extensions-utils`: + * New extensions `onlyCommonMessages`, `onlySentViaBot` and `withoutSentViaBot` was added ### 0.27.5 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt new file mode 100644 index 0000000000..d4fa37e1c0 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/CommonMessageConversations.kt @@ -0,0 +1,34 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.PossiblySentViaBotCommonMessage +import kotlinx.coroutines.flow.* + +/** + * Simple factory to convert [ContentMessage] to a [CommonMessage] + */ +fun > Flow.onlyCommonMessages() = filterIsInstance>() + +/** + * Filter the messages and checking that incoming [CommonMessage] is [PossiblySentViaBotCommonMessage] and its + * [PossiblySentViaBotCommonMessage.senderBot] is not null + */ +fun Flow>.onlySentViaBot() = mapNotNull { + (it as? PossiblySentViaBotCommonMessage) ?.let { possiblySentViaBot -> + if (possiblySentViaBot.senderBot != null) { + possiblySentViaBot + } else { + null + } + } +} + +/** + * Filter the messages and checking that incoming [CommonMessage] not is [PossiblySentViaBotCommonMessage] or its + * [PossiblySentViaBotCommonMessage.senderBot] is null + */ +fun Flow>.withoutSentViaBot() = filter { + it !is PossiblySentViaBotCommonMessage || it.senderBot == null +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt index 15ebf19f76..f88f9f09a8 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt @@ -12,6 +12,13 @@ fun Flow.asContentMessagesFlow() = mapNotNull { it.data as? ContentMessage<*> } +/** + * Will map incoming [BaseSentMessageUpdate]s to [CommonMessage] from [BaseSentMessageUpdate.data] + */ +fun Flow.asCommonMessagesFlow() = mapNotNull { + it.data as? CommonMessage<*> +} + /** * Will map incoming [BaseSentMessageUpdate]s to [ChatEventMessage] from [BaseSentMessageUpdate.data] */