2022-09-09 11:27:11 +00:00
|
|
|
package dev.inmo.plaguposter.ratings.gc
|
|
|
|
|
2023-08-12 17:58:28 +00:00
|
|
|
import korlibs.time.DateTime
|
|
|
|
import korlibs.time.seconds
|
2022-09-09 11:27:11 +00:00
|
|
|
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
|
2024-05-13 13:41:17 +00:00
|
|
|
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
|
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
|
2024-05-13 13:41:17 +00:00
|
|
|
import dev.inmo.plagubot.plugins.inline.queries.models.Format
|
|
|
|
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
|
|
|
|
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
|
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
|
2024-05-13 13:41:17 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
2022-09-09 11:27:11 +00:00
|
|
|
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 {
|
2024-05-13 13:36:09 +00:00
|
|
|
val postCreationDateTime = postsRepo.getPostCreationTime(it) ?: (dropCreatedBefore - 1.seconds) // do dropping if post creation time is not available
|
|
|
|
if (postCreationDateTime < dropCreatedBefore) {
|
|
|
|
ratingsRepo.unset(it)
|
2022-09-09 11:27:11 +00:00
|
|
|
postsRepo.deleteById(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 05:05:14 +00:00
|
|
|
runCatchingSafely {
|
|
|
|
doAutoClear()
|
|
|
|
}
|
|
|
|
autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
|
|
|
|
doAutoClear()
|
|
|
|
}
|
2024-05-13 13:41:17 +00:00
|
|
|
|
|
|
|
onCommand("clean_posts_by_ratings") {
|
|
|
|
doAutoClear()
|
|
|
|
}
|
|
|
|
koin.getOrNull<InlineTemplatesRepo>() ?.addTemplate(
|
|
|
|
OfferTemplate("Force autoclear", listOf(Format("/clean_posts_by_ratings")))
|
|
|
|
)
|
2022-09-09 11:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|