This commit is contained in:
InsanusMokrassar 2023-05-10 11:05:14 +06:00
parent 87957dba30
commit 57eebb61d5
4 changed files with 37 additions and 6 deletions

View File

@ -1,5 +1,10 @@
# PlaguPoster
## 0.2.2
* `GarbageCollector`:
* Now on start will all clearing job done
## 0.2.1
* `Versions`:

View File

@ -10,4 +10,4 @@ android.enableJetifier=true
# Project data
group=dev.inmo
version=0.2.1
version=0.2.2

View File

@ -1,12 +1,15 @@
package dev.inmo.plaguposter.ratings.gc
import com.soywiz.klock.DateTime
import com.soywiz.klock.seconds
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule
import dev.inmo.krontab.utils.asFlowWithDelays
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.repos.*
import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.models.Rating
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
@ -41,21 +44,35 @@ object Plugin : Plugin {
val config = koin.get<Config>()
config.immediateDrop ?.let { toDrop ->
ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) {
if (it.value <= toDrop) {
postsRepo.deleteById(it.id)
suspend fun checkAndOptionallyDrop(postId: PostId, rating: Rating) {
if (rating <= toDrop) {
postsRepo.deleteById(postId)
}
}
ratingsRepo.getAll().forEach {
runCatchingSafely {
checkAndOptionallyDrop(it.key, it.value)
}
}
ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) {
checkAndOptionallyDrop(it.first, it.second)
}
}
config.autoclear ?.let { autoclear ->
autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
val dropCreatedBefore = it - (autoclear.skipPostAge ?: 0).seconds
suspend fun doAutoClear() {
val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach {
if ((postsRepo.getPostCreationTime(it) ?: return@forEach) < dropCreatedBefore) {
postsRepo.deleteById(it)
}
}
}
runCatchingSafely {
doAutoClear()
}
autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
doAutoClear()
}
}
}
}

View File

@ -12,6 +12,7 @@
"dev.inmo.plaguposter.ratings.Plugin",
"dev.inmo.plaguposter.ratings.source.Plugin",
"dev.inmo.plaguposter.ratings.selector.Plugin",
"dev.inmo.plaguposter.ratings.gc.Plugin",
"dev.inmo.plaguposter.triggers.selector_with_timer.Plugin",
"dev.inmo.plagubot.plugins.inline.queries.Plugin",
"dev.inmo.plaguposter.triggers.command.Plugin",
@ -72,5 +73,13 @@
},
"publish_command": {
"panelButtonText": "Publish"
},
"gc": {
"autoclear": {
"rating": -1,
"autoClearKrontab": "0 0 0 * *",
"skipPostAge": 86400
},
"immediateDrop": -6
}
}