From edbe412f1407856091fe7ae48883fa1d1c65e6bc Mon Sep 17 00:00:00 2001 From: "nise.moe" Date: Tue, 5 Mar 2024 13:01:19 +0100 Subject: [PATCH] Updated users checkers --- .../nisemoe/nise/scheduler/ImportScores.kt | 6 ++- .../com/nisemoe/nise/scheduler/ImportUsers.kt | 42 ++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportScores.kt b/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportScores.kt index 39f475d..e374f59 100644 --- a/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportScores.kt +++ b/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportScores.kt @@ -274,6 +274,8 @@ class ImportScores( .groupBy(SCORES.USER_ID) .fetchInto(Long::class.java) + this.logger.info("Found ${suspiciousScores.size} suspicious scores to update.") + val stolenReplays = dslContext .select(ScoreService.osuScoreAlias1.USER_ID, ScoreService.osuScoreAlias2.USER_ID) .from(SCORES_SIMILARITY) @@ -296,13 +298,15 @@ class ImportScores( ) } + stolenReplays.map { it.get(ScoreService.osuScoreAlias2.USER_ID, Long::class.java) }).distinct() - for(userId in suspiciousUserIds) { + suspiciousUserIds.forEachIndexed { index, userId -> + this.logger.info("Checking user $userId [$index/${suspiciousUserIds.size}]") val isBanned = this.osuApi.checkIfUserBanned(userId) if(isBanned == true) { dslContext.update(SCORES) .set(SCORES.IS_BANNED, true) .where(SCORES.USER_ID.eq(userId)) .execute() + this.logger.info("User $userId is banned.") } Thread.sleep(SLEEP_AFTER_API_CALL) } diff --git a/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportUsers.kt b/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportUsers.kt index 855f365..5d52039 100644 --- a/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportUsers.kt +++ b/nise-backend/src/main/kotlin/com/nisemoe/nise/scheduler/ImportUsers.kt @@ -6,6 +6,7 @@ import com.nisemoe.nise.database.UserService import com.nisemoe.nise.integrations.DiscordEmbed import com.nisemoe.nise.integrations.DiscordService import com.nisemoe.nise.osu.OsuApi +import org.jooq.Condition import org.jooq.DSLContext import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Value @@ -13,6 +14,7 @@ import org.springframework.context.annotation.Profile import org.springframework.scheduling.annotation.Scheduled import org.springframework.stereotype.Service import java.time.LocalDateTime +import java.time.OffsetDateTime @Service @Profile("import:users") @@ -52,21 +54,36 @@ class ImportUsers( } private fun updateUsers() { - // Fetch 50 users with the oldest last update time + val pageSize = 50 + var currentPage = 0 + do { + val offset = currentPage * pageSize + + val userIds = dslContext + .select(USERS.USER_ID) + .from(USERS) + .leftJoin(SCORES).on(USERS.USER_ID.eq(SCORES.USER_ID)) + .orderBy(USERS.SYS_LAST_UPDATE.asc()) + .limit(pageSize) + .offset(offset) + .fetchInto(Long::class.java) + + // Process the fetched users + if (userIds.isNotEmpty()) { + updateUserIds(userIds) + } + + currentPage++ + } while (userIds.size == pageSize) + } + + private fun updateUserIds(userIds: List) { val threeMonthsAgo = LocalDateTime.now().minusMonths(3) val bannedUsersCondition = SCORES.IS_BANNED.eq(true) .and(SCORES.DATE.greaterOrEqual(threeMonthsAgo)) - val userIds = dslContext - .select(USERS.USER_ID) - .from(USERS) - .leftJoin(SCORES).on(USERS.USER_ID.eq(SCORES.USER_ID)) - .orderBy(USERS.SYS_LAST_UPDATE.asc()) - .limit(50) - .fetchInto(Long::class.java) - val usersResult = this.osuApi.getUsersBatch(userIds) if (usersResult == null) { @@ -77,8 +94,13 @@ class ImportUsers( // Check which ids are missing; if any are missing, we explicitly check if they're banned. val missingIds = userIds.filter { it !in usersResult.users.map { it.id } } for (missingId in missingIds) { + dslContext.update(USERS) + .set(USERS.SYS_LAST_UPDATE, OffsetDateTime.now()) + .where(USERS.USER_ID.eq(missingId)) + .execute() + // We exclude all users with at least 1 banned score in the last 3 months - if(dslContext.fetchExists(SCORES, SCORES.USER_ID.eq(missingId).and(bannedUsersCondition))) { + if (dslContext.fetchExists(SCORES, SCORES.USER_ID.eq(missingId).and(bannedUsersCondition))) { continue }