mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2024-11-25 08:58:45 +00:00
fixes and fills
This commit is contained in:
parent
f3f7761bf9
commit
53675ca598
@ -7,6 +7,7 @@ plagubot = "2.3.0"
|
||||
tgbotapi = "3.2.0"
|
||||
microutils = "0.12.6"
|
||||
kslog = "0.5.1"
|
||||
krontab = "0.8.0"
|
||||
|
||||
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-cache = { module = "dev.inmo:micro_utils.repos.cache", version.ref = "microutils" }
|
||||
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
|
||||
krontab = { module = "dev.inmo:krontab", version.ref = "krontab" }
|
||||
|
||||
psql = { module = "org.postgresql:postgresql", version.ref = "psql" }
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package dev.inmo.plaguposter.ratings.selector
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.plaguposter.posts.models.PostId
|
||||
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
|
||||
import dev.inmo.plaguposter.ratings.selector.models.SelectorConfig
|
||||
@ -8,11 +9,11 @@ class DefaultSelector (
|
||||
private val config: SelectorConfig,
|
||||
private val repo: RatingsRepo
|
||||
) : Selector {
|
||||
override suspend fun take(n: Int): List<PostId> {
|
||||
override suspend fun take(n: Int, now: DateTime): List<PostId> {
|
||||
val result = mutableListOf<PostId>()
|
||||
|
||||
do {
|
||||
val selected = config.active ?.ratings ?.select(repo, result) ?: break
|
||||
val selected = config.active(now.time) ?.ratings ?.select(repo, result) ?: break
|
||||
result.add(selected)
|
||||
} while (result.size < n)
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
package dev.inmo.plaguposter.ratings.selector
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.plaguposter.posts.models.PostId
|
||||
|
||||
interface Selector {
|
||||
suspend fun take(n: Int = 1): List<PostId>
|
||||
suspend fun take(n: Int = 1, now: DateTime = DateTime.now()): List<PostId>
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
package dev.inmo.plaguposter.ratings.selector.models
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import com.soywiz.klock.Time
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SelectorConfig(
|
||||
val items: List<SelectorConfigItem>
|
||||
) {
|
||||
val active: SelectorConfigItem?
|
||||
get() = items.firstOrNull { it.time.isActive }
|
||||
fun active(now: Time = DateTime.now().time) = items.firstOrNull { it.time.isActive(now) }
|
||||
}
|
||||
|
@ -17,8 +17,7 @@ class TimeConfig(
|
||||
@Transient
|
||||
val range = from .. to
|
||||
|
||||
val isActive: Boolean
|
||||
get() = DateTime.now().time in range
|
||||
fun isActive(now: Time): Boolean = now in range
|
||||
|
||||
|
||||
object TimeSerializer : KSerializer<Time> {
|
||||
|
@ -10,7 +10,9 @@
|
||||
"dev.inmo.plaguposter.posts.Plugin",
|
||||
"dev.inmo.plaguposter.posts.registrar.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": {
|
||||
"targetChat": 12345678,
|
||||
@ -61,5 +63,8 @@
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"timer_trigger": {
|
||||
"krontab": "0 30 2/4 * *"
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ String[] includes = [
|
||||
":ratings:source",
|
||||
":ratings:selector",
|
||||
":triggers:command",
|
||||
":triggers:selector_with_timer",
|
||||
// ":settings",
|
||||
":runner"
|
||||
]
|
||||
|
@ -12,6 +12,7 @@ kotlin {
|
||||
dependencies {
|
||||
api project(":plaguposter.common")
|
||||
api project(":plaguposter.posts")
|
||||
api project(":plaguposter.ratings.selector")
|
||||
}
|
||||
}
|
||||
jvmMain {
|
||||
|
@ -6,6 +6,7 @@ import dev.inmo.plagubot.Plugin
|
||||
import dev.inmo.plaguposter.common.SuccessfulSymbol
|
||||
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.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||
@ -38,10 +39,7 @@ object Plugin : Plugin {
|
||||
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
|
||||
val postsRepo = koin.get<PostsRepo>()
|
||||
val publisher = koin.get<PostPublisher>()
|
||||
strictlyOn { state: PublishState ->
|
||||
|
||||
null
|
||||
}
|
||||
val selector = koin.getOrNull<Selector>()
|
||||
|
||||
onCommand("publish_post") {
|
||||
val messageInReply = it.replyTo ?.contentMessageOrNull() ?: let { _ ->
|
||||
@ -49,7 +47,7 @@ object Plugin : Plugin {
|
||||
|
||||
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) {
|
||||
reply(
|
||||
it,
|
||||
|
24
triggers/selector_with_timer/build.gradle
Normal file
24
triggers/selector_with_timer/build.gradle
Normal 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 {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
package dev.inmo.plaguposter.triggers.selector_with_timer
|
43
triggers/selector_with_timer/src/jvmMain/kotlin/Plugin.kt
Normal file
43
triggers/selector_with_timer/src/jvmMain/kotlin/Plugin.kt
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.plaguposter.triggers.selector_with_timer"/>
|
Loading…
Reference in New Issue
Block a user