1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-03 07:09:23 +00:00

WAT A HUGE REFACTOR

This commit is contained in:
2025-07-12 15:42:48 +06:00
parent 586a61157d
commit 9e4d7dd9f9
292 changed files with 1337 additions and 509 deletions

View File

@@ -146,7 +146,7 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/VariantsKt {
public static final fun oneOf (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;[Lkotlinx/coroutines/Deferred;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun oneOfActions (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Ljava/lang/Iterable;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun oneOfActions (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;[Ldev/inmo/micro_utils/coroutines/DeferredAction;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun parallel (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun parallel (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/Deferred;
public static final fun withAction (Lkotlinx/coroutines/Deferred;Lkotlin/jvm/functions/Function2;)Ldev/inmo/micro_utils/coroutines/DeferredAction;
}

View File

@@ -9,6 +9,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.handlers_registrar.T
import dev.inmo.tgbotapi.types.UpdateId
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.*
import dev.inmo.tgbotapi.utils.launchWithBotLogger
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
@@ -150,6 +151,7 @@ inline fun <T> BehaviourContext(
*
* @param updatesFilter This param will not be used anymore
*/
@Suppress("UNCHECKED_CAST")
fun <BC : BehaviourContext> BC.createSubContext(
scope: CoroutineScope = LinkedSupervisorScope(),
triggersHolder: TriggersHolder = this.triggersHolder,
@@ -217,7 +219,7 @@ fun <T, BC : BehaviourContext> BC.launchInNewSubContext(
updatesUpstreamFlow = updatesUpstreamFlow,
subcontextInitialAction = subcontextInitialAction
).apply {
this@apply.launchLoggingDropExceptions(logger = Log) {
this@apply.launchWithBotLogger {
behaviourContextReceiver()
}
}.coroutineContext.job

View File

@@ -1,3 +1,5 @@
@file:Suppress("unused")
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.DeferredAction
@@ -5,15 +7,15 @@ import dev.inmo.micro_utils.coroutines.invokeFirstOf
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
suspend fun <T> BehaviourContext.parallel(
fun <T> BehaviourContext.parallel(
action: BehaviourContextReceiver<T>
) = async {
action()
}
inline infix fun <T, O> Deferred<T>.withAction(noinline callback: suspend (T) -> O) = DeferredAction(this, callback)
infix fun <T, O> Deferred<T>.withAction(callback: suspend (T) -> O) = DeferredAction(this, callback)
inline fun <T> Deferred<T>.asAction() = DeferredAction(this) { it }
fun <T> Deferred<T>.asAction() = DeferredAction(this) { it }
suspend fun <O> BehaviourContext.oneOfActions(
deferredActions: Iterable<DeferredAction<*, O>>

View File

@@ -44,7 +44,7 @@ fun <T> FlowsUpdatesFilter.expectFlow(
if (result.isFailure || result.getOrThrow().isEmpty()) {
if (cancelTrigger(it)) {
cancelRequestFactory(it) ?.also {
safelyWithResult { bot.execute(it) }
runCatching { bot.execute(it) }
throw cancelledByFilterException
}
}

View File

@@ -1,3 +1,5 @@
@file:Suppress("unused")
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
@@ -10,9 +12,9 @@ import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage
import kotlinx.coroutines.flow.Flow
inline fun BehaviourContext.waitChatMessageReactionsCountUpdated(
fun BehaviourContext.waitChatMessageReactionsCountUpdated(
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }
errorFactory: NullableRequestBuilder<*> = { null }
): Flow<ChatMessageReactionsCountUpdated> = expectFlow(
initRequest,
errorFactory

View File

@@ -1,3 +1,5 @@
@file:Suppress("OPT_IN_USAGE")
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext

View File

@@ -1,3 +1,5 @@
@file:Suppress("unused")
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
@@ -17,7 +19,8 @@ inline fun <reified T : MediaGroupPartContent> BehaviourContext.buildMediaGroupW
noinline errorFactory: NullableRequestBuilder<*> = { null }
): Flow<MediaGroupContent<T>> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update ->
update.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull<MediaGroupContent<*>>() ?.let { message ->
if (message.content.group.all { it is T }) {
if (message.content.group.all { it.content is T }) {
@Suppress("UNCHECKED_CAST")
listOf(message.content as MediaGroupContent<T>)
} else {
null

View File

@@ -17,7 +17,8 @@ inline fun <reified T : MediaGroupPartContent> BehaviourContext.buildMediaGroupM
noinline errorFactory: NullableRequestBuilder<*> = { null }
): Flow<MediaGroupMessage<T>> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update ->
update.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull<MediaGroupContent<*>>() ?.let { message ->
if (message.content.group.all { it is T }) {
if (message.content.group.all { it.content is T }) {
@Suppress("UNCHECKED_CAST")
listOf(message as MediaGroupMessage<T>)
} else {
null

View File

@@ -2,8 +2,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CallbackQueryFilterByUser
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
@@ -13,6 +11,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times
import dev.inmo.tgbotapi.extensions.utils.callbackQueryUpdateOrNull
import dev.inmo.tgbotapi.types.queries.callback.*
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.launchWithBotLogger
import kotlinx.coroutines.Job
internal inline fun <BC : BehaviourContext, reified T : CallbackQuery> BC.onCallbackQuery(
@@ -63,7 +62,7 @@ internal inline fun <BC : BehaviourContext, reified T : DataCallbackQuery> BC.on
this@launchInNewSubContext.triggersHolder.handleableCallbackQueriesDataHolder.registerHandleable(newInitialFilterInvoke)
it.invokeOnCompletion {
runCatching {
launchSafelyWithoutExceptions {
launchWithBotLogger {
this@launchInNewSubContext.triggersHolder.handleableCallbackQueriesDataHolder.unregisterHandleable(newInitialFilterInvoke)
}
}

View File

@@ -2,10 +2,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.kslog.common.KSLog
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
@@ -20,6 +16,7 @@ import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.message.content.TextMessage
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.launchWithBotLogger
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
@@ -77,7 +74,7 @@ fun <BC : BehaviourContext> BC.command(
triggersHolder.handleableCommandsHolder.registerHandleable(commandRegex)
it.invokeOnCompletion {
runCatching {
launchSafelyWithoutExceptions {
launchWithBotLogger {
triggersHolder.handleableCommandsHolder.unregisterHandleable(commandRegex)
}
}

View File

@@ -15,6 +15,7 @@ import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.message.content.TextMessage
import dev.inmo.tgbotapi.types.message.textsources.RegularTextSource
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.launchWithBotLogger
import io.ktor.http.decodeURLQueryComponent
import kotlinx.coroutines.Job
@@ -43,7 +44,7 @@ fun <BC : BehaviourContext> BC.onDeepLink(
}.also {
triggersHolder.handleableCommandsHolder.registerHandleable(startRegex)
it.invokeOnCompletion {
this@onDeepLink.launchSafelyWithoutExceptions { triggersHolder.handleableCommandsHolder.unregisterHandleable(startRegex) }
this@onDeepLink.launchWithBotLogger { triggersHolder.handleableCommandsHolder.unregisterHandleable(startRegex) }
}
}
}

View File

@@ -1,17 +1,17 @@
@file:OptIn(ExperimentalCoroutinesApi::class)
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.flatMap
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.launchWithBotLogger
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapLatest
@@ -75,7 +75,7 @@ internal fun <BC : BehaviourContext, T> BC.on(
block = handler
)
} ?: subscribeLoggingDropExceptions(scope) {
scope.launchLoggingDropExceptions {
scope.launchWithBotLogger {
handler(it)
}
}

View File

@@ -24,6 +24,7 @@ internal inline fun <BC : BehaviourContext, reified T : MediaGroupPartContent> B
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, additionalSubcontextInitialAction, scenarioReceiver) {
it.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull<MediaGroupContent<*>>() ?.let {
if (it.content.group.all { it.content is T }) {
@Suppress("UNCHECKED_CAST")
listOf(it as MediaGroupMessage<T>)
} else {
null

View File

@@ -24,6 +24,7 @@ internal inline fun <BC : BehaviourContext, reified T : MediaGroupPartContent> B
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, additionalSubcontextInitialAction, scenarioReceiver) {
it.baseSentMessageUpdateOrNull() ?.data ?.commonMessageOrNull() ?.withContentOrNull<MediaGroupContent<*>>() ?.let {
if (it.content.group.all { it.content is T }) {
@Suppress("UNCHECKED_CAST")
listOf(it.content as MediaGroupContent<T>)
} else {
null

View File

@@ -10,12 +10,12 @@ import dev.inmo.tgbotapi.extensions.utils.pollAnswerUpdateOrNull
import dev.inmo.tgbotapi.types.polls.PollAnswer
import dev.inmo.tgbotapi.types.update.abstracts.Update
internal inline fun <BC : BehaviourContext> BC.onPollAnswered(
internal fun <BC : BehaviourContext> BC.onPollAnswered(
initialFilter: SimpleFilter<PollAnswer>? = null,
noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, PollAnswer, Update>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, PollAnswer, Update>? = null,
markerFactory: MarkerFactory<in PollAnswer, Any>? = ByIdPollAnswerMarkerFactory,
noinline additionalSubcontextInitialAction: CustomBehaviourContextAndTwoTypesReceiver<BC, Unit, Update, PollAnswer>? = null,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PollAnswer>
additionalSubcontextInitialAction: CustomBehaviourContextAndTwoTypesReceiver<BC, Unit, Update, PollAnswer>? = null,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PollAnswer>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, additionalSubcontextInitialAction, scenarioReceiver) {
(it.pollAnswerUpdateOrNull() ?.data) ?.let(::listOfNotNull)
}

View File

@@ -1,6 +1,5 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.handlers_registrar
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
@@ -40,7 +39,7 @@ suspend fun <T, R> HandleableTriggersHolder<T>.doWithRegistration(
block: suspend () -> R
): R {
registerHandleable(data)
val result = runCatchingSafely {
val result = runCatching {
block()
}
unregisterHandleable(data)

View File

@@ -1,3 +1,5 @@
@file:Suppress("UNCHECKED_CAST")
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories
private val commonAnyMarker = MarkerFactory<Any, Any> { it }