fixes and fills

This commit is contained in:
InsanusMokrassar 2022-09-07 00:23:14 +06:00
parent f3f7761bf9
commit 53675ca598
13 changed files with 91 additions and 13 deletions

View File

@ -7,6 +7,7 @@ plagubot = "2.3.0"
tgbotapi = "3.2.0" tgbotapi = "3.2.0"
microutils = "0.12.6" microutils = "0.12.6"
kslog = "0.5.1" kslog = "0.5.1"
krontab = "0.8.0"
psql = "42.3.6" psql = "42.3.6"
@ -38,6 +39,7 @@ microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", versio
microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", 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" } microutils-repos-cache = { module = "dev.inmo:micro_utils.repos.cache", version.ref = "microutils" }
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" } kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
krontab = { module = "dev.inmo:krontab", version.ref = "krontab" }
psql = { module = "org.postgresql:postgresql", version.ref = "psql" } psql = { module = "org.postgresql:postgresql", version.ref = "psql" }

View File

@ -1,5 +1,6 @@
package dev.inmo.plaguposter.ratings.selector package dev.inmo.plaguposter.ratings.selector
import com.soywiz.klock.DateTime
import dev.inmo.plaguposter.posts.models.PostId import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.ratings.repo.RatingsRepo import dev.inmo.plaguposter.ratings.repo.RatingsRepo
import dev.inmo.plaguposter.ratings.selector.models.SelectorConfig import dev.inmo.plaguposter.ratings.selector.models.SelectorConfig
@ -8,11 +9,11 @@ class DefaultSelector (
private val config: SelectorConfig, private val config: SelectorConfig,
private val repo: RatingsRepo private val repo: RatingsRepo
) : Selector { ) : Selector {
override suspend fun take(n: Int): List<PostId> { override suspend fun take(n: Int, now: DateTime): List<PostId> {
val result = mutableListOf<PostId>() val result = mutableListOf<PostId>()
do { do {
val selected = config.active ?.ratings ?.select(repo, result) ?: break val selected = config.active(now.time) ?.ratings ?.select(repo, result) ?: break
result.add(selected) result.add(selected)
} while (result.size < n) } while (result.size < n)

View File

@ -1,7 +1,8 @@
package dev.inmo.plaguposter.ratings.selector package dev.inmo.plaguposter.ratings.selector
import com.soywiz.klock.DateTime
import dev.inmo.plaguposter.posts.models.PostId import dev.inmo.plaguposter.posts.models.PostId
interface Selector { interface Selector {
suspend fun take(n: Int = 1): List<PostId> suspend fun take(n: Int = 1, now: DateTime = DateTime.now()): List<PostId>
} }

View File

@ -1,11 +1,12 @@
package dev.inmo.plaguposter.ratings.selector.models package dev.inmo.plaguposter.ratings.selector.models
import com.soywiz.klock.DateTime
import com.soywiz.klock.Time
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class SelectorConfig( data class SelectorConfig(
val items: List<SelectorConfigItem> val items: List<SelectorConfigItem>
) { ) {
val active: SelectorConfigItem? fun active(now: Time = DateTime.now().time) = items.firstOrNull { it.time.isActive(now) }
get() = items.firstOrNull { it.time.isActive }
} }

View File

@ -17,8 +17,7 @@ class TimeConfig(
@Transient @Transient
val range = from .. to val range = from .. to
val isActive: Boolean fun isActive(now: Time): Boolean = now in range
get() = DateTime.now().time in range
object TimeSerializer : KSerializer<Time> { object TimeSerializer : KSerializer<Time> {

View File

@ -10,7 +10,9 @@
"dev.inmo.plaguposter.posts.Plugin", "dev.inmo.plaguposter.posts.Plugin",
"dev.inmo.plaguposter.posts.registrar.Plugin", "dev.inmo.plaguposter.posts.registrar.Plugin",
"dev.inmo.plaguposter.ratings.Plugin", "dev.inmo.plaguposter.ratings.Plugin",
"dev.inmo.plaguposter.ratings.source.Plugin" "dev.inmo.plaguposter.ratings.source.Plugin",
"dev.inmo.plaguposter.ratings.selector.Plugin",
"dev.inmo.plaguposter.triggers.selector_with_timer.Plugin"
], ],
"posts": { "posts": {
"targetChat": 12345678, "targetChat": 12345678,
@ -61,5 +63,8 @@
} }
} }
] ]
},
"timer_trigger": {
"krontab": "0 30 2/4 * *"
} }
} }

View File

@ -8,6 +8,7 @@ String[] includes = [
":ratings:source", ":ratings:source",
":ratings:selector", ":ratings:selector",
":triggers:command", ":triggers:command",
":triggers:selector_with_timer",
// ":settings", // ":settings",
":runner" ":runner"
] ]

View File

@ -12,6 +12,7 @@ kotlin {
dependencies { dependencies {
api project(":plaguposter.common") api project(":plaguposter.common")
api project(":plaguposter.posts") api project(":plaguposter.posts")
api project(":plaguposter.ratings.selector")
} }
} }
jvmMain { jvmMain {

View File

@ -6,6 +6,7 @@ import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.common.SuccessfulSymbol import dev.inmo.plaguposter.common.SuccessfulSymbol
import dev.inmo.plaguposter.posts.repo.PostsRepo import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.posts.sending.PostPublisher import dev.inmo.plaguposter.posts.sending.PostPublisher
import dev.inmo.plaguposter.ratings.selector.Selector
import dev.inmo.tgbotapi.extensions.api.edit.edit import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.send.reply import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.send import dev.inmo.tgbotapi.extensions.api.send.send
@ -38,10 +39,7 @@ object Plugin : Plugin {
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) { override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
val postsRepo = koin.get<PostsRepo>() val postsRepo = koin.get<PostsRepo>()
val publisher = koin.get<PostPublisher>() val publisher = koin.get<PostPublisher>()
strictlyOn { state: PublishState -> val selector = koin.getOrNull<Selector>()
null
}
onCommand("publish_post") { onCommand("publish_post") {
val messageInReply = it.replyTo ?.contentMessageOrNull() ?: let { _ -> val messageInReply = it.replyTo ?.contentMessageOrNull() ?: let { _ ->
@ -49,7 +47,7 @@ object Plugin : Plugin {
return@onCommand return@onCommand
} }
val postId = postsRepo.getIdByChatAndMessage(messageInReply.chat.id, messageInReply.messageId) val postId = postsRepo.getIdByChatAndMessage(messageInReply.chat.id, messageInReply.messageId) ?: selector ?.take(1) ?.firstOrNull()
if (postId == null) { if (postId == null) {
reply( reply(
it, it,

View File

@ -0,0 +1,24 @@
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(":plaguposter.common")
api project(":plaguposter.posts")
api project(":plaguposter.ratings.selector")
api libs.krontab
}
}
jvmMain {
dependencies {
}
}
}
}

View File

@ -0,0 +1 @@
package dev.inmo.plaguposter.triggers.selector_with_timer

View File

@ -0,0 +1,43 @@
package dev.inmo.plaguposter.triggers.selector_with_timer
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule
import dev.inmo.krontab.utils.asFlow
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.posts.sending.PostPublisher
import dev.inmo.plaguposter.ratings.selector.Selector
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import kotlinx.coroutines.FlowPreview
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
object Plugin : Plugin {
@Serializable
internal data class Config(
@SerialName("krontab")
val krontabTemplate: KrontabTemplate
) {
@Transient
val krontab by lazy {
krontabTemplate.toSchedule()
}
}
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["timer_trigger"] ?: return@single null) }
}
@OptIn(FlowPreview::class)
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val publisher = koin.get<PostPublisher>()
val selector = koin.get<Selector>()
koin.get<Config>().krontab.asFlow().subscribeSafelyWithoutExceptions(this) {
selector.take(now = it).forEach { postId ->
publisher.publish(postId)
}
}
}
}

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.plaguposter.triggers.selector_with_timer"/>