mirror of
				https://github.com/InsanusMokrassar/PlaguPoster.git
				synced 2025-11-04 05:20:28 +00:00 
			
		
		
		
	ratings auto clearup
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,7 @@ kotlin {
 | 
			
		||||
            dependencies {
 | 
			
		||||
                api project(":plaguposter.common")
 | 
			
		||||
                api project(":plaguposter.posts")
 | 
			
		||||
                api libs.krontab
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										18
									
								
								ratings/src/commonMain/kotlin/models/Config.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								ratings/src/commonMain/kotlin/models/Config.kt
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
			
		||||
}
 | 
			
		||||
@@ -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<RatingsRepo> {
 | 
			
		||||
@@ -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<RatingsConfig>()
 | 
			
		||||
        val scope = koin.get<CoroutineScope>()
 | 
			
		||||
        val ratingsRepo = koin.get<RatingsRepo>()
 | 
			
		||||
        koin.get<PostsRepo>().deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(this) {
 | 
			
		||||
        val postsRepo = koin.get<PostsRepo>()
 | 
			
		||||
        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())
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user