more "same" flow filters were added

This commit is contained in:
Sergey Tolmachev 2023-03-16 16:53:39 +05:30
parent 741d808db2
commit 941a25b116
1 changed files with 16 additions and 0 deletions

View File

@ -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 <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)
}