Updated users checkers

This commit is contained in:
nise.moe 2024-03-05 13:01:19 +01:00
parent 38be6161a3
commit edbe412f14
2 changed files with 37 additions and 11 deletions

View File

@ -274,6 +274,8 @@ class ImportScores(
.groupBy(SCORES.USER_ID) .groupBy(SCORES.USER_ID)
.fetchInto(Long::class.java) .fetchInto(Long::class.java)
this.logger.info("Found ${suspiciousScores.size} suspicious scores to update.")
val stolenReplays = dslContext val stolenReplays = dslContext
.select(ScoreService.osuScoreAlias1.USER_ID, ScoreService.osuScoreAlias2.USER_ID) .select(ScoreService.osuScoreAlias1.USER_ID, ScoreService.osuScoreAlias2.USER_ID)
.from(SCORES_SIMILARITY) .from(SCORES_SIMILARITY)
@ -296,13 +298,15 @@ class ImportScores(
) )
} + stolenReplays.map { it.get(ScoreService.osuScoreAlias2.USER_ID, Long::class.java) }).distinct() } + 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) val isBanned = this.osuApi.checkIfUserBanned(userId)
if(isBanned == true) { if(isBanned == true) {
dslContext.update(SCORES) dslContext.update(SCORES)
.set(SCORES.IS_BANNED, true) .set(SCORES.IS_BANNED, true)
.where(SCORES.USER_ID.eq(userId)) .where(SCORES.USER_ID.eq(userId))
.execute() .execute()
this.logger.info("User $userId is banned.")
} }
Thread.sleep(SLEEP_AFTER_API_CALL) Thread.sleep(SLEEP_AFTER_API_CALL)
} }

View File

@ -6,6 +6,7 @@ import com.nisemoe.nise.database.UserService
import com.nisemoe.nise.integrations.DiscordEmbed import com.nisemoe.nise.integrations.DiscordEmbed
import com.nisemoe.nise.integrations.DiscordService import com.nisemoe.nise.integrations.DiscordService
import com.nisemoe.nise.osu.OsuApi import com.nisemoe.nise.osu.OsuApi
import org.jooq.Condition
import org.jooq.DSLContext import org.jooq.DSLContext
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value 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.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.OffsetDateTime
@Service @Service
@Profile("import:users") @Profile("import:users")
@ -52,21 +54,36 @@ class ImportUsers(
} }
private fun updateUsers() { private fun updateUsers() {
// Fetch 50 users with the oldest last update time val pageSize = 50
var currentPage = 0
val threeMonthsAgo = LocalDateTime.now().minusMonths(3) do {
val offset = currentPage * pageSize
val bannedUsersCondition = SCORES.IS_BANNED.eq(true)
.and(SCORES.DATE.greaterOrEqual(threeMonthsAgo))
val userIds = dslContext val userIds = dslContext
.select(USERS.USER_ID) .select(USERS.USER_ID)
.from(USERS) .from(USERS)
.leftJoin(SCORES).on(USERS.USER_ID.eq(SCORES.USER_ID)) .leftJoin(SCORES).on(USERS.USER_ID.eq(SCORES.USER_ID))
.orderBy(USERS.SYS_LAST_UPDATE.asc()) .orderBy(USERS.SYS_LAST_UPDATE.asc())
.limit(50) .limit(pageSize)
.offset(offset)
.fetchInto(Long::class.java) .fetchInto(Long::class.java)
// Process the fetched users
if (userIds.isNotEmpty()) {
updateUserIds(userIds)
}
currentPage++
} while (userIds.size == pageSize)
}
private fun updateUserIds(userIds: List<Long>) {
val threeMonthsAgo = LocalDateTime.now().minusMonths(3)
val bannedUsersCondition = SCORES.IS_BANNED.eq(true)
.and(SCORES.DATE.greaterOrEqual(threeMonthsAgo))
val usersResult = this.osuApi.getUsersBatch(userIds) val usersResult = this.osuApi.getUsersBatch(userIds)
if (usersResult == null) { 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. // 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 } } val missingIds = userIds.filter { it !in usersResult.users.map { it.id } }
for (missingId in missingIds) { 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 // 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 continue
} }