1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

Merge pull request #737 from Tolsi/more-same-flow-filters

more "same" flow filters were added
This commit is contained in:
InsanusMokrassar 2023-03-16 18:52:13 +06:00 committed by GitHub
commit f8bbfa2b1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,11 @@
package dev.inmo.tgbotapi.extensions.utils.extensions 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.types.message.abstracts.Message
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull 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] * @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") @Suppress("NOTHING_TO_INLINE")
inline fun Message.sameThread(other: Message) = sameChat(other) && threadIdOrNull == other.threadIdOrNull inline fun Message.sameThread(other: Message) = sameChat(other) && threadIdOrNull == other.threadIdOrNull
@Suppress("NOTHING_TO_INLINE")
inline fun <T : Message> Flow<T>.filterSameChat(chatId: IdChatIdentifier): Flow<T> =
transform { value ->
if (value.chat.id.chatId == chatId.chatId) return@transform emit(value)
}
@Suppress("NOTHING_TO_INLINE")
inline fun <T : FromUser> Flow<T>.filterSameUser(userId: IdChatIdentifier): Flow<T> =
transform { value ->
if (value.from.id.chatId == userId.chatId) return@transform emit(value)
}