From bb1856de902fa07e72b449880f9e01d0d9bed6d8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 16 May 2022 13:58:16 -0400 Subject: [PATCH] complete 1.0.0 --- .../kotlin/dev/inmo/plagubot/HelloPlugin.kt | 26 ++++++---- .../main/kotlin/dev/inmo/plagubot/PlaguBot.kt | 51 ++++++++++++------- .../kotlin/dev/inmo/plagubot/config/Config.kt | 4 +- .../inmo/plagubot/config/DatabaseConfig.kt | 10 +--- .../dev/inmo/plagubot/config/ConfigTest.kt | 4 +- .../main/kotlin/dev/inmo/plagubot/Plugin.kt | 25 +++------ 6 files changed, 64 insertions(+), 56 deletions(-) diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt index 4aaa829..ed82f6e 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt @@ -1,6 +1,9 @@ package dev.inmo.plagubot +import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.send.reply import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand import kotlinx.serialization.* import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject @@ -9,6 +12,7 @@ import org.koin.core.Koin import org.koin.core.KoinApplication import org.koin.core.component.KoinComponent import org.koin.core.component.get +import org.koin.core.module.Module import org.koin.dsl.module @Serializable @@ -18,16 +22,18 @@ class HelloPlugin : Plugin { data class HelloPluginConfig( val print: String ) - override suspend fun BehaviourContext.invoke( - database: Database, - params: JsonObject - ) { - loadModule { - single { - get().decodeFromJsonElement(HelloPluginConfig.serializer(), params["helloPlugin"] ?: return@single null) - } - } - println(get().print) + override fun Module.setupDI(database: Database, params: JsonObject) { + single { + get().decodeFromJsonElement(HelloPluginConfig.serializer(), params["helloPlugin"] ?: return@single null) + } + } + + override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { + println(koin.get().print) + println(getMe()) + onCommand("hello_world") { + reply(it, "Hello :)") + } } } diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt index 875d927..7f7e5fd 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt @@ -2,6 +2,7 @@ package dev.inmo.plagubot import dev.inmo.plagubot.config.* import dev.inmo.tgbotapi.bot.ktor.telegramBot +import dev.inmo.tgbotapi.extensions.api.webhook.deleteWebhook import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling import kotlinx.coroutines.* @@ -9,14 +10,14 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import kotlinx.serialization.json.JsonObject import org.jetbrains.exposed.sql.Database +import org.koin.core.Koin import org.koin.core.KoinApplication -import org.koin.core.component.get import org.koin.core.context.GlobalContext -import org.koin.core.qualifier.named +import org.koin.core.module.Module +import org.koin.core.scope.Scope import org.koin.dsl.module -const val DefaultPlaguBotParamsKey = "plagubot" -val Plugin.plagubot: PlaguBot +val Scope.plagubot: PlaguBot get() = get() @Serializable @@ -27,12 +28,30 @@ data class PlaguBot( @Transient private val bot = telegramBot(config.botToken) - override suspend fun BehaviourContext.invoke( - database: Database, - params: JsonObject - ) { + override fun Module.setupDI(database: Database, params: JsonObject) { + single { config } + single { config.plugins } + single { config.databaseConfig } + single { config.databaseConfig.database } + single { defaultJsonFormat } + single { this@PlaguBot } + + includes( + config.plugins.map { + module { + with(it) { + setupDI(database, params) + } + } + } + ) + } + + override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { config.plugins.forEach { - it.apply { invoke(database, params) } + with(it) { + setupBotPlugin(koin) + } } } @@ -40,25 +59,21 @@ data class PlaguBot( * This method will create an [Job] which will be the main [Job] of ran instance */ suspend fun start( - scope: CoroutineScope = CoroutineScope(Dispatchers.Default) + scope: CoroutineScope = CoroutineScope(Dispatchers.IO) ): Job { val koinApp = KoinApplication.init() koinApp.modules( module { - single { config } - single { config.plugins } - single { config.database } - single(named(defaultDatabaseParamsName)) { config.database.database } - single { defaultJsonFormat } - single(named(DefaultPlaguBotParamsKey)) { this@PlaguBot } + setupDI(config.databaseConfig.database, json) } ) + GlobalContext.startKoin(koinApp) lateinit var behaviourContext: BehaviourContext bot.buildBehaviour(scope = scope) { - invoke(config.database.database, json) behaviourContext = this + setupBotPlugin(koinApp.koin) + deleteWebhook() } - GlobalContext.startKoin(koinApp) return bot.startGettingOfUpdatesByLongPolling(scope = behaviourContext, updatesFilter = behaviourContext) } } diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt index 4f13a4b..5135e5b 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt @@ -1,11 +1,13 @@ package dev.inmo.plagubot.config import dev.inmo.plagubot.Plugin +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Serializable data class Config( val botToken: String, val plugins: List, - val database: DatabaseConfig = DatabaseConfig(), + @SerialName("database") + val databaseConfig: DatabaseConfig = DatabaseConfig(), ) diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt index 19e513e..7d38b13 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt @@ -1,21 +1,15 @@ package dev.inmo.plagubot.config -import dev.inmo.plagubot.Plugin import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.transactions.transactionManager -import org.koin.core.KoinApplication -import org.koin.core.context.loadKoinModules -import org.koin.core.qualifier.StringQualifier -import org.koin.core.qualifier.named import org.koin.core.scope.Scope import org.sqlite.JDBC import java.sql.Connection -const val defaultDatabaseParamsName = "defaultDatabase" -inline val Plugin.database: Database? - get() = getKoin().getOrNull(named(defaultDatabaseParamsName)) +inline val Scope.database: Database? + get() = getOrNull() @Serializable data class DatabaseConfig( diff --git a/bot/src/test/kotlin/dev/inmo/plagubot/config/ConfigTest.kt b/bot/src/test/kotlin/dev/inmo/plagubot/config/ConfigTest.kt index 3cd4fe9..dd5e783 100644 --- a/bot/src/test/kotlin/dev/inmo/plagubot/config/ConfigTest.kt +++ b/bot/src/test/kotlin/dev/inmo/plagubot/config/ConfigTest.kt @@ -32,9 +32,9 @@ class ConfigTest { PluginsConfigurationSerializer, configAndPluginsConfigJsonFormat.encodeToString(PluginsConfigurationSerializer, config) ) as Config - assertEquals(config.database, redecoded.database) + assertEquals(config.databaseConfig, redecoded.databaseConfig) assertEquals(config.plugins, redecoded.plugins) assertEquals(config.botToken, redecoded.botToken) assertEquals(config.params ?.toMap(), redecoded.params ?.toMap()) } -} \ No newline at end of file +} diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt index 439b394..15d92b7 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt @@ -1,18 +1,11 @@ package dev.inmo.plagubot -import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext -import dev.inmo.tgbotapi.types.BotCommand -import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter -import kotlinx.coroutines.CoroutineScope import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject import org.jetbrains.exposed.sql.Database import org.koin.core.Koin -import org.koin.core.KoinApplication -import org.koin.core.component.KoinComponent -import org.koin.dsl.ModuleDeclaration -import org.koin.dsl.module +import org.koin.core.module.Module /** * **ANY REALIZATION OF [Plugin] MUST HAVE CONSTRUCTOR WITH ABSENCE OF INCOMING PARAMETERS** @@ -22,17 +15,15 @@ import org.koin.dsl.module * too. */ @Serializable(PluginSerializer::class) -interface Plugin : KoinComponent { - fun loadModule(createdAtStart: Boolean = false, moduleDeclaration: ModuleDeclaration) = getKoin().loadModules( - listOf( - module(createdAtStart, moduleDeclaration) - ) - ) +interface Plugin { /** - * This method (usually) will be invoked just one time in the whole application. + * This method will be called when this plugin should configure di module based on the incoming params */ - suspend operator fun BehaviourContext.invoke( + fun Module.setupDI( database: Database, params: JsonObject - ) {} + ) + suspend fun BehaviourContext.setupBotPlugin( + koin: Koin + ) }