From 15e29286fb53c76adfbd01a1742ee30da7e1e8ef Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 14 Sep 2020 00:12:28 +0600 Subject: [PATCH] commonMessages and sent via bot shortcuts updates --- CHANGELOG.md | 3 +++ .../utils/CommonMessageConversations.kt | 25 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2911765d28..218f63700d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,9 @@ * `filterCommandsWithArgs` * Extension `Flow.filterCommandsWithArgs` has changed its signature: now it will also have original message paired with list of text sources + * Shortcut method `commonMessages` for `onlyCommonMessages` + * Shortcuts `onlySentViaBot` and `withoutSentViaBot` now are extensions for any `Flow` with types which implementing + `ContentMessage` ## 0.27.0 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 index d4fa37e1c0..afccd714b3 100644 --- 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 @@ -1,7 +1,6 @@ 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.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.PossiblySentViaBotCommonMessage import kotlinx.coroutines.flow.* @@ -11,17 +10,21 @@ import kotlinx.coroutines.flow.* */ fun > Flow.onlyCommonMessages() = filterIsInstance>() +/** + * Shortcut for [onlyCommonMessages] + */ +@Suppress("NOTHING_TO_INLINE") +inline fun > Flow.commonMessages() = onlyCommonMessages() + /** * 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 - } +fun > Flow.onlySentViaBot() = mapNotNull { + if (it is PossiblySentViaBot && it.senderBot != null) { + it + } else { + null } } @@ -29,6 +32,6 @@ fun Flow>.onlySentViaBot() = mapNotNull { * 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 +fun > Flow.withoutSentViaBot() = filter { + it !is PossiblySentViaBot || it.senderBot == null }