updates in plagubot module

This commit is contained in:
InsanusMokrassar 2021-02-22 19:53:51 +06:00
parent e638aff72a
commit 141cdc2625
6 changed files with 81 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.mappers.withMapper import dev.inmo.micro_utils.repos.mappers.withMapper
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.libraries.cache.admins.micro_utils.DefaultAdminsCacheAPIRepo import dev.inmo.tgbotapi.libraries.cache.admins.micro_utils.DefaultAdminsCacheAPIRepo
import dev.inmo.tgbotapi.libraries.cache.admins.micro_utils.DynamicAdminsCacheSettingsAPI import dev.inmo.tgbotapi.libraries.cache.admins.micro_utils.DynamicAdminsCacheSettingsAPI
import dev.inmo.tgbotapi.types.toChatId import dev.inmo.tgbotapi.types.toChatId
@ -62,3 +63,5 @@ fun AdminsCacheAPI(
repo, repo,
settingsAPI settingsAPI
) )
fun BehaviourContext.AdminsCacheAPI(database: Database) = AdminsCacheAPI(this, database, this)

22
cache/admins/plagubot/build.gradle vendored Normal file
View File

@ -0,0 +1,22 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":tgbotapi.libraries.cache.admins.micro_utils")
}
}
jvmMain {
dependencies {
api "dev.inmo:plagubot.plugin:$plagubot_version"
}
}
}
}

View File

@ -0,0 +1,53 @@
package dev.inmo.tgbotapi.libraries.cache.admins
import dev.inmo.plagubot.Plugin
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.types.ChatId
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import org.jetbrains.exposed.sql.Database
val Map<String, Any>.adminsPlugin: AdminsPlugin?
get() = get("admins") as? AdminsPlugin
@Serializable
class AdminsPlugin(
private val chatsSettings: Map<ChatId, AdminsCacheSettings>? = null
) : Plugin {
@Transient
private val globalAdminsCacheAPI = MutableStateFlow<AdminsCacheAPI?>(null)
@Transient
private val databaseToAdminsCacheAPI = mutableMapOf<Database, MutableStateFlow<AdminsCacheAPI?>>()
private val mutex = Mutex()
suspend fun adminsAPI(database: Database): AdminsCacheAPI {
return when (chatsSettings) {
null -> {
val flow = mutex.withLock {
databaseToAdminsCacheAPI.getOrPut(database){ MutableStateFlow(null) }
}
flow.first { it != null }!!
}
else -> globalAdminsCacheAPI.first { it != null }!!
}
}
override suspend fun BehaviourContext.invoke(database: Database, params: Map<String, Any>) {
when (chatsSettings) {
null -> {
mutex.withLock {
val flow = databaseToAdminsCacheAPI.getOrPut(database){ MutableStateFlow(null) }
if (flow.value == null) {
flow.value = AdminsCacheAPI(database)
}
}
}
else -> mutex.withLock {
globalAdminsCacheAPI.value = AdminsCacheAPI(database)
}
}
}
}

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.tgbotapi.libraries.cache.admins.plagubot"/>

View File

@ -14,6 +14,7 @@ github_release_plugin_version=2.2.12
tgbotapi_version=0.32.8 tgbotapi_version=0.32.8
micro_utils_version=0.4.26 micro_utils_version=0.4.26
exposed_version=0.29.1 exposed_version=0.29.1
plagubot_version=0.1.4
# ANDROID # ANDROID

View File

@ -3,6 +3,7 @@ rootProject.name = 'tgbotapi.libraries'
String[] includes = [ String[] includes = [
":cache:admins:common", ":cache:admins:common",
":cache:admins:micro_utils", ":cache:admins:micro_utils",
":cache:admins:plagubot",
":cache:media" ":cache:media"
] ]