mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2024-11-17 13:33:47 +00:00
improve of autoclear
This commit is contained in:
parent
16bd62da51
commit
e6a0d67444
@ -5,6 +5,8 @@ import korlibs.time.seconds
|
|||||||
import dev.inmo.krontab.KrontabTemplate
|
import dev.inmo.krontab.KrontabTemplate
|
||||||
import dev.inmo.krontab.toSchedule
|
import dev.inmo.krontab.toSchedule
|
||||||
import dev.inmo.krontab.utils.asFlowWithDelays
|
import dev.inmo.krontab.utils.asFlowWithDelays
|
||||||
|
import dev.inmo.kslog.common.KSLog
|
||||||
|
import dev.inmo.kslog.common.i
|
||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
|
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
|
||||||
@ -64,14 +66,21 @@ object Plugin : Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
config.autoclear ?.let { autoclear ->
|
config.autoclear ?.let { autoclear ->
|
||||||
|
val autoClearLogger = KSLog("autoclear")
|
||||||
suspend fun doAutoClear() {
|
suspend fun doAutoClear() {
|
||||||
|
autoClearLogger.i { "Start autoclear" }
|
||||||
val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
|
val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
|
||||||
ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach {
|
autoClearLogger.i { "Posts drop created before: ${dropCreatedBefore.toStringDefault()}" }
|
||||||
|
val idsToDelete = ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.filter {
|
||||||
val postCreationDateTime = postsRepo.getPostCreationTime(it) ?: (dropCreatedBefore - 1.seconds) // do dropping if post creation time is not available
|
val postCreationDateTime = postsRepo.getPostCreationTime(it) ?: (dropCreatedBefore - 1.seconds) // do dropping if post creation time is not available
|
||||||
if (postCreationDateTime < dropCreatedBefore) {
|
postCreationDateTime < dropCreatedBefore
|
||||||
ratingsRepo.unset(it)
|
|
||||||
postsRepo.deleteById(it)
|
|
||||||
}
|
}
|
||||||
|
autoClearLogger.i { "Posts to drop: $idsToDelete" }
|
||||||
|
if (idsToDelete.isNotEmpty()) {
|
||||||
|
runCatching { ratingsRepo.unset(idsToDelete) }
|
||||||
|
autoClearLogger.i { "Ratings dropped" }
|
||||||
|
runCatching { postsRepo.deleteById(idsToDelete) }
|
||||||
|
autoClearLogger.i { "Posts dropped" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
runCatchingSafely {
|
runCatchingSafely {
|
||||||
|
Loading…
Reference in New Issue
Block a user