PlaguPoster/ratings/gc/src/jvmMain/kotlin/Plugin.kt

79 lines
2.9 KiB
Kotlin
Raw Normal View History

2022-09-09 11:27:11 +00:00
package dev.inmo.plaguposter.ratings.gc
2023-05-10 05:05:14 +00:00
import com.soywiz.klock.DateTime
2022-09-09 11:27:11 +00:00
import com.soywiz.klock.seconds
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule
2023-03-18 06:35:18 +00:00
import dev.inmo.krontab.utils.asFlowWithDelays
2023-05-10 05:05:14 +00:00
import dev.inmo.micro_utils.coroutines.runCatchingSafely
2022-09-09 11:27:11 +00:00
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
2022-09-09 12:17:02 +00:00
import dev.inmo.micro_utils.repos.*
2022-09-09 11:27:11 +00:00
import dev.inmo.plagubot.Plugin
2023-05-10 05:05:14 +00:00
import dev.inmo.plaguposter.posts.models.PostId
2022-09-09 11:27:11 +00:00
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.models.Rating
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.types.Seconds
import kotlinx.serialization.Serializable
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(
val autoclear: AutoClearOptions? = null,
val immediateDrop: Rating? = null,
) {
@Serializable
data class AutoClearOptions(
val rating: Rating,
val autoClearKrontab: KrontabTemplate,
val skipPostAge: Seconds? = null
)
}
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["gc"] ?: return@single null) }
}
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val ratingsRepo = koin.get<RatingsRepo>()
val postsRepo = koin.get<PostsRepo>()
val config = koin.get<Config>()
config.immediateDrop ?.let { toDrop ->
2023-05-10 05:05:14 +00:00
suspend fun checkAndOptionallyDrop(postId: PostId, rating: Rating) {
if (rating <= toDrop) {
postsRepo.deleteById(postId)
}
}
ratingsRepo.getAll().forEach {
runCatchingSafely {
checkAndOptionallyDrop(it.key, it.value)
2022-09-09 12:17:02 +00:00
}
2022-09-09 11:27:11 +00:00
}
2023-05-10 05:05:14 +00:00
ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) {
checkAndOptionallyDrop(it.first, it.second)
}
2022-09-09 11:27:11 +00:00
}
config.autoclear ?.let { autoclear ->
2023-05-10 05:05:14 +00:00
suspend fun doAutoClear() {
val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
2022-09-09 11:27:11 +00:00
ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach {
if ((postsRepo.getPostCreationTime(it) ?: return@forEach) < dropCreatedBefore) {
postsRepo.deleteById(it)
}
}
}
2023-05-10 05:05:14 +00:00
runCatchingSafely {
doAutoClear()
}
autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
doAutoClear()
}
2022-09-09 11:27:11 +00:00
}
}
}