diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt index 20ede5d212..441c59e101 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt @@ -1,7 +1,11 @@ package dev.inmo.tgbotapi.extensions.utils.extensions +import dev.inmo.tgbotapi.abstracts.FromUser +import dev.inmo.tgbotapi.types.IdChatIdentifier import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.transform /** * @return true in case if [this] message is placed in the same chat that [other] @@ -33,3 +37,15 @@ inline fun Message.sameTopic(other: Message) = sameChat(other) && threadIdOrNull */ @Suppress("NOTHING_TO_INLINE") inline fun Message.sameThread(other: Message) = sameChat(other) && threadIdOrNull == other.threadIdOrNull + +@Suppress("NOTHING_TO_INLINE") +inline fun Flow.filterSameChat(chatId: IdChatIdentifier): Flow = + transform { value -> + if (value.chat.id.chatId == chatId.chatId) return@transform emit(value) + } + +@Suppress("NOTHING_TO_INLINE") +inline fun Flow.filterSameUser(userId: IdChatIdentifier): Flow = + transform { value -> + if (value.from.id.chatId == userId.chatId) return@transform emit(value) + }