From 4423eba1d98bed93495a69bbbb5401fe80f57ee7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 18 Nov 2022 12:43:53 +0600 Subject: [PATCH] fixes --- gradle/libs.versions.toml | 1 + inlines/src/jvmMain/kotlin/Plugin.kt | 8 ++++++++ posts/panel/build.gradle | 5 +++++ .../src/commonMain/kotlin/PanelButtonBuilder.kt | 17 ++++++++++++++++- .../src/commonMain/kotlin/PanelButtonsAPI.kt | 17 +++++++++++------ posts/panel/src/jvmMain/kotlin/Plugin.kt | 11 ++++++++--- settings/build.gradle | 1 + triggers/command/src/jvmMain/kotlin/Plugin.kt | 9 --------- 8 files changed, 50 insertions(+), 19 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c6a0431..bacd557 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -28,6 +28,7 @@ tgbotapi = { module = "dev.inmo:tgbotapi", version.ref = "tgbotapi" } plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" } plagubot-bot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" } plagubot-plugins-inline-queries = { module = "dev.inmo:plagubot.plugins.inline.queries", version.ref = "plagubot-plugins" } +plagubot-plugins-inline-buttons = { module = "dev.inmo:plagubot.plugins.inline.buttons", version.ref = "plagubot-plugins" } microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", version.ref = "microutils" } microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", version.ref = "microutils" } microutils-repos-cache = { module = "dev.inmo:micro_utils.repos.cache", version.ref = "microutils" } diff --git a/inlines/src/jvmMain/kotlin/Plugin.kt b/inlines/src/jvmMain/kotlin/Plugin.kt index 0b94aa7..a0a37bb 100644 --- a/inlines/src/jvmMain/kotlin/Plugin.kt +++ b/inlines/src/jvmMain/kotlin/Plugin.kt @@ -4,12 +4,20 @@ import dev.inmo.kslog.common.TagLogger import dev.inmo.kslog.common.w import dev.inmo.plagubot.Plugin import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import kotlinx.serialization.json.JsonObject +import org.jetbrains.exposed.sql.Database import org.koin.core.Koin +import org.koin.core.module.Module private val actualPlugin = dev.inmo.plagubot.plugins.inline.queries.Plugin object Plugin : Plugin by actualPlugin { private val log = TagLogger("InlinePlugin") + + override fun Module.setupDI(database: Database, params: JsonObject) { + single { actualPlugin } + } + override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { log.w { "Built-in inline plugin has been deprecated. Use \"${actualPlugin::class.qualifiedName}\" instead" diff --git a/posts/panel/build.gradle b/posts/panel/build.gradle index eda422f..32f902f 100644 --- a/posts/panel/build.gradle +++ b/posts/panel/build.gradle @@ -14,5 +14,10 @@ kotlin { api libs.microutils.koin } } + jvmMain { + dependencies { + api libs.plagubot.plugins.inline.queries + } + } } } diff --git a/posts/panel/src/commonMain/kotlin/PanelButtonBuilder.kt b/posts/panel/src/commonMain/kotlin/PanelButtonBuilder.kt index 74960f0..85c3296 100644 --- a/posts/panel/src/commonMain/kotlin/PanelButtonBuilder.kt +++ b/posts/panel/src/commonMain/kotlin/PanelButtonBuilder.kt @@ -3,6 +3,21 @@ package dev.inmo.plaguposter.posts.panel import dev.inmo.plaguposter.posts.models.RegisteredPost import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.InlineKeyboardButton -fun interface PanelButtonBuilder { +interface PanelButtonBuilder { + val weight: Int suspend fun buildButton(post: RegisteredPost): InlineKeyboardButton? + + class Default(override val weight: Int = 0, private val block: suspend (RegisteredPost) -> InlineKeyboardButton?) : PanelButtonBuilder { + override suspend fun buildButton(post: RegisteredPost): InlineKeyboardButton? = block(post) + } + + companion object { + operator fun invoke(block: suspend (RegisteredPost) -> InlineKeyboardButton?) = Default( + block = block + ) + operator fun invoke(weight: Int, block: suspend (RegisteredPost) -> InlineKeyboardButton?) = Default( + weight, + block + ) + } } diff --git a/posts/panel/src/commonMain/kotlin/PanelButtonsAPI.kt b/posts/panel/src/commonMain/kotlin/PanelButtonsAPI.kt index 2f8c11d..7e66c2a 100644 --- a/posts/panel/src/commonMain/kotlin/PanelButtonsAPI.kt +++ b/posts/panel/src/commonMain/kotlin/PanelButtonsAPI.kt @@ -5,14 +5,14 @@ import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineK import kotlinx.coroutines.flow.MutableSharedFlow class PanelButtonsAPI( - private val preset: List, + private val preset: Map>, private val rootPanelButtonText: String ) { - private val _buttons = mutableSetOf().also { - it.addAll(preset) + private val _buttonsMap = mutableMapOf>().also { + it.putAll(preset.map { it.key to it.value.toMutableList() }) } internal val buttonsBuilders: List - get() = _buttons.toList() + get() = _buttonsMap.toList().sortedBy { it.first }.flatMap { it.second } internal val forceRefreshFlow = MutableSharedFlow() val RootPanelButtonBuilder = PanelButtonBuilder { @@ -22,8 +22,13 @@ class PanelButtonsAPI( ) } - fun add(button: PanelButtonBuilder) = _buttons.add(button) - fun remove(button: PanelButtonBuilder) = _buttons.remove(button) + fun add(button: PanelButtonBuilder, weight: Int = button.weight) = _buttonsMap.getOrPut(weight) { mutableListOf() }.add(button) + fun remove(button: PanelButtonBuilder) = _buttonsMap.mapNotNull { (k, v) -> + v.remove(button) + k.takeIf { v.isEmpty() } + }.forEach { + _buttonsMap.remove(it) + } suspend fun forceRefresh(postId: PostId) { forceRefreshFlow.emit(postId) } diff --git a/posts/panel/src/jvmMain/kotlin/Plugin.kt b/posts/panel/src/jvmMain/kotlin/Plugin.kt index 2d6622a..6384e2d 100644 --- a/posts/panel/src/jvmMain/kotlin/Plugin.kt +++ b/posts/panel/src/jvmMain/kotlin/Plugin.kt @@ -4,6 +4,7 @@ import com.benasher44.uuid.uuid4 import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.koin.getAllDistinct +import dev.inmo.micro_utils.koin.singleWithRandomQualifierAndBinds import dev.inmo.micro_utils.repos.deleteById import dev.inmo.micro_utils.repos.set import dev.inmo.plagubot.Plugin @@ -22,10 +23,10 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessag import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard -import dev.inmo.tgbotapi.types.ChatId import dev.inmo.tgbotapi.types.IdChatIdentifier import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton +import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.SwitchInlineQueryCurrentChatInlineKeyboardButton import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.message.ParseMode import kotlinx.coroutines.flow.first @@ -70,9 +71,13 @@ object Plugin : Plugin { } ) PanelButtonsAPI( - getAllDistinct() + builtInButtons, + emptyMap(), config.rootButtonText - ) + ).apply { + (getAllDistinct() + builtInButtons).forEach { + add(it) + } + } } } diff --git a/settings/build.gradle b/settings/build.gradle index a0d2361..d101bd6 100644 --- a/settings/build.gradle +++ b/settings/build.gradle @@ -1,6 +1,7 @@ plugins { id "org.jetbrains.kotlin.multiplatform" id "org.jetbrains.kotlin.plugin.serialization" + id "com.android.library" } apply from: "$mppProjectWithSerializationPresetPath" diff --git a/triggers/command/src/jvmMain/kotlin/Plugin.kt b/triggers/command/src/jvmMain/kotlin/Plugin.kt index 23e86e1..599fe42 100644 --- a/triggers/command/src/jvmMain/kotlin/Plugin.kt +++ b/triggers/command/src/jvmMain/kotlin/Plugin.kt @@ -1,12 +1,9 @@ package dev.inmo.plaguposter.triggers.command import com.benasher44.uuid.uuid4 -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.fsm.common.State -import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.plagubot.Plugin import dev.inmo.plaguposter.common.SuccessfulSymbol -import dev.inmo.plaguposter.common.UnsuccessfulSymbol import dev.inmo.plagubot.plugins.inline.queries.models.Format import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo @@ -16,14 +13,10 @@ import dev.inmo.plaguposter.posts.panel.PanelButtonsAPI import dev.inmo.plaguposter.posts.repo.PostsRepo import dev.inmo.plaguposter.posts.sending.PostPublisher import dev.inmo.plaguposter.ratings.selector.Selector -import dev.inmo.tgbotapi.extensions.api.answers.answer import dev.inmo.tgbotapi.extensions.api.edit.edit import dev.inmo.tgbotapi.extensions.api.send.reply -import dev.inmo.tgbotapi.extensions.api.send.send import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextWithFSM import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery -import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitTextMessage -import dev.inmo.tgbotapi.extensions.behaviour_builder.strictlyOn import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery import dev.inmo.tgbotapi.extensions.utils.* @@ -33,9 +26,7 @@ import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard import dev.inmo.tgbotapi.types.ChatId import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton -import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.message.textsources.regular -import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first import kotlinx.serialization.Serializable import kotlinx.serialization.json.*