diff --git a/CHANGELOG.md b/CHANGELOG.md index 780fc34..6fcf9f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.8.0 +* Dependencies update +* `Ratings`: + * Add autoclearing of ratings without target posts each half hours + ## 0.7.0 * Dependencies update diff --git a/ratings/build.gradle b/ratings/build.gradle index 3793d30..1c0d789 100644 --- a/ratings/build.gradle +++ b/ratings/build.gradle @@ -11,6 +11,7 @@ kotlin { dependencies { api project(":plaguposter.common") api project(":plaguposter.posts") + api libs.krontab } } } diff --git a/ratings/src/commonMain/kotlin/models/Config.kt b/ratings/src/commonMain/kotlin/models/Config.kt new file mode 100644 index 0000000..6f8b6c7 --- /dev/null +++ b/ratings/src/commonMain/kotlin/models/Config.kt @@ -0,0 +1,18 @@ +package dev.inmo.plaguposter.ratings.models + +import dev.inmo.krontab.EveryHourScheduler +import dev.inmo.krontab.KrontabTemplate +import dev.inmo.krontab.buildSchedule +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient + +@Serializable +internal data class RatingsConfig( + @SerialName("manualRecheckKrontab") + val manualRecheckKrontabTemplate: KrontabTemplate = "0 /30 *" +) { + @Transient + val manualRecheckKrontab + get() = buildSchedule(manualRecheckKrontabTemplate) +} diff --git a/ratings/src/jvmMain/kotlin/Plugin.kt b/ratings/src/jvmMain/kotlin/Plugin.kt index 44ab330..deb5960 100644 --- a/ratings/src/jvmMain/kotlin/Plugin.kt +++ b/ratings/src/jvmMain/kotlin/Plugin.kt @@ -1,20 +1,29 @@ package dev.inmo.plaguposter.ratings +import dev.inmo.krontab.utils.asTzFlowWithDelays +import dev.inmo.kslog.common.TagLogger +import dev.inmo.kslog.common.i import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.koin.singleWithBinds import dev.inmo.micro_utils.repos.unset import dev.inmo.plagubot.Plugin +import dev.inmo.plagubot.config import dev.inmo.plagubot.database +import dev.inmo.plagubot.registerConfig import dev.inmo.plaguposter.common.useCache import dev.inmo.plaguposter.posts.repo.PostsRepo +import dev.inmo.plaguposter.ratings.Plugin.setupBotPlugin import dev.inmo.plaguposter.ratings.exposed.ExposedRatingsRepo +import dev.inmo.plaguposter.ratings.models.RatingsConfig import dev.inmo.plaguposter.ratings.repo.* import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import kotlinx.coroutines.CoroutineScope import kotlinx.serialization.json.* import org.koin.core.Koin import org.koin.core.module.Module object Plugin : Plugin { + private val Log = TagLogger("RatingsPlugin") override fun Module.setupDI(config: JsonObject) { single { ExposedRatingsRepo(database) } singleWithBinds { @@ -26,12 +35,24 @@ object Plugin : Plugin { base } } + + registerConfig(RatingsConfig.serializer(), "ratings") { RatingsConfig() } } - override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { + override suspend fun startPlugin(koin: Koin) { + super.startPlugin(koin) + val config = koin.config() + val scope = koin.get() val ratingsRepo = koin.get() - koin.get().deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(this) { + val postsRepo = koin.get() + postsRepo.deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(scope) { ratingsRepo.unset(it) } + config.manualRecheckKrontab.asTzFlowWithDelays().subscribeSafelyWithoutExceptions(scope) { + Log.i { "Start clearing ratings without registered posts" } + val postsIdsToRemove = ratingsRepo.getAll().keys - postsRepo.getAll().keys + Log.i { "Posts to remove: $postsIdsToRemove" } + ratingsRepo.unset(postsIdsToRemove.toList()) + } } }