1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-16 06:45:26 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter.kt

93 lines
5.4 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.updateshandlers
2019-05-10 03:54:57 +00:00
2021-10-13 08:22:01 +00:00
import dev.inmo.micro_utils.coroutines.plus
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.ALL_UPDATES_LIST
2022-11-07 20:27:38 +00:00
import dev.inmo.tgbotapi.types.message.abstracts.PossiblySentViaBotCommonMessage
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.update.*
2022-11-07 20:27:38 +00:00
import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
2021-10-13 08:22:01 +00:00
import kotlinx.coroutines.channels.*
2020-06-27 03:43:24 +00:00
import kotlinx.coroutines.flow.*
2019-05-10 03:54:57 +00:00
2021-01-19 12:50:45 +00:00
interface FlowsUpdatesFilter : UpdatesFilter {
override val allowedUpdates: List<String>
get() = ALL_UPDATES_LIST
val allUpdatesFlow: Flow<Update>
2021-08-22 17:14:56 +00:00
val messagesFlow: Flow<MessageUpdate>
2022-11-07 20:27:38 +00:00
val messageMediaGroupsFlow: Flow<MessageUpdate>
get() = messagesFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null }
2021-08-22 17:14:56 +00:00
val editedMessagesFlow: Flow<EditMessageUpdate>
2022-11-07 20:27:38 +00:00
val editedMessageMediaGroupsFlow: Flow<EditMessageUpdate>
get() = editedMessagesFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null }
2021-08-22 17:14:56 +00:00
val channelPostsFlow: Flow<ChannelPostUpdate>
2022-11-07 20:27:38 +00:00
val channelPostMediaGroupsFlow: Flow<ChannelPostUpdate>
get() = channelPostsFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null }
2021-08-22 17:14:56 +00:00
val editedChannelPostsFlow: Flow<EditChannelPostUpdate>
2022-11-07 20:27:38 +00:00
val editedChannelPostMediaGroupsFlow: Flow<EditChannelPostUpdate>
get() = editedChannelPostsFlow.filter { (it.data as? PossiblySentViaBotCommonMessage<*>) ?.mediaGroupId != null }
2021-08-22 17:14:56 +00:00
val chosenInlineResultsFlow: Flow<ChosenInlineResultUpdate>
val inlineQueriesFlow: Flow<InlineQueryUpdate>
val callbackQueriesFlow: Flow<CallbackQueryUpdate>
val shippingQueriesFlow: Flow<ShippingQueryUpdate>
val preCheckoutQueriesFlow: Flow<PreCheckoutQueryUpdate>
val pollsFlow: Flow<PollUpdate>
val pollAnswersFlow: Flow<PollAnswerUpdate>
val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate>
val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate>
2021-11-08 12:21:55 +00:00
val chatJoinRequestUpdateFlow: Flow<ChatJoinRequestUpdate>
2023-12-31 10:16:26 +00:00
val chatMessageReactionUpdatedUpdateFlow: Flow<ChatMessageReactionUpdatedUpdate>
val chatMessageReactionsCountUpdatedUpdateFlow: Flow<ChatMessageReactionsCountUpdatedUpdate>
2021-08-22 17:14:56 +00:00
val unknownUpdatesFlow: Flow<UnknownUpdate>
2021-01-19 12:50:45 +00:00
}
2021-10-13 08:22:01 +00:00
abstract class AbstractFlowsUpdatesFilter : FlowsUpdatesFilter {
override val messagesFlow: Flow<MessageUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val editedMessagesFlow: Flow<EditMessageUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val channelPostsFlow: Flow<ChannelPostUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val editedChannelPostsFlow: Flow<EditChannelPostUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val chosenInlineResultsFlow: Flow<ChosenInlineResultUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val inlineQueriesFlow: Flow<InlineQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val callbackQueriesFlow: Flow<CallbackQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val shippingQueriesFlow: Flow<ShippingQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val preCheckoutQueriesFlow: Flow<PreCheckoutQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val pollsFlow: Flow<PollUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val pollAnswersFlow: Flow<PollAnswerUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
2021-11-08 12:21:55 +00:00
override val chatJoinRequestUpdateFlow: Flow<ChatJoinRequestUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
2023-12-31 10:16:26 +00:00
override val chatMessageReactionUpdatedUpdateFlow: Flow<ChatMessageReactionUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val chatMessageReactionsCountUpdatedUpdateFlow: Flow<ChatMessageReactionsCountUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
2021-10-13 08:22:01 +00:00
override val unknownUpdatesFlow: Flow<UnknownUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
}
2021-01-19 12:50:45 +00:00
/**
* Creates [DefaultFlowsUpdatesFilter]
*/
@Suppress("FunctionName")
fun FlowsUpdatesFilter(
2021-10-13 08:22:01 +00:00
broadcastChannelsSize: Int = 100,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
upstreamUpdatesFlow: Flow<Update>? = null
) = DefaultFlowsUpdatesFilter(broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow)
2021-01-19 12:50:45 +00:00
@Suppress("unused")
2021-01-19 12:50:45 +00:00
class DefaultFlowsUpdatesFilter(
2021-06-27 19:00:20 +00:00
broadcastChannelsSize: Int = 100,
2021-10-13 08:22:01 +00:00
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
upstreamUpdatesFlow: Flow<Update>? = null
): AbstractFlowsUpdatesFilter() {
private val additionalUpdatesSharedFlow = MutableSharedFlow<Update>(0, broadcastChannelsSize, onBufferOverflow)
2020-06-27 03:43:24 +00:00
@Suppress("MemberVisibilityCanBePrivate")
2021-10-13 08:22:01 +00:00
override val allUpdatesFlow: Flow<Update> = (additionalUpdatesSharedFlow.asSharedFlow()).let {
if (upstreamUpdatesFlow != null) {
(it + upstreamUpdatesFlow).distinctUntilChanged { old, new -> old.updateId == new.updateId }
} else {
it
}
}
2019-05-10 03:54:57 +00:00
2021-10-13 08:22:01 +00:00
override val asUpdateReceiver: UpdateReceiver<Update> = additionalUpdatesSharedFlow::emit
2021-06-27 19:00:20 +00:00
}