@@ -2,6 +2,7 @@ package io.github.rothes.esu.bukkit.module.chatantispam.user
22
33import io.github.rothes.esu.bukkit.module.ChatAntiSpamModule.addr
44import io.github.rothes.esu.bukkit.module.ChatAntiSpamModule.config
5+ import io.github.rothes.esu.bukkit.module.chatantispam.user.CasDataManager.ChatSpamTable.lastAccess
56import io.github.rothes.esu.bukkit.module.chatantispam.user.CasDataManager.ChatSpamTable.tableName
67import io.github.rothes.esu.bukkit.user
78import io.github.rothes.esu.bukkit.user.PlayerUser
@@ -20,9 +21,10 @@ import org.jetbrains.exposed.v1.core.SqlExpressionBuilder.inList
2021import org.jetbrains.exposed.v1.datetime.datetime
2122import org.jetbrains.exposed.v1.jdbc.SchemaUtils
2223import org.jetbrains.exposed.v1.jdbc.deleteWhere
23- import org.jetbrains.exposed.v1.jdbc.replace
24+ import org.jetbrains.exposed.v1.jdbc.insertIgnore
2425import org.jetbrains.exposed.v1.jdbc.selectAll
2526import org.jetbrains.exposed.v1.jdbc.transactions.transaction
27+ import org.jetbrains.exposed.v1.jdbc.update
2628import org.jetbrains.exposed.v1.json.json
2729
2830object CasDataManager {
@@ -63,9 +65,7 @@ object CasDataManager {
6365 })
6466 // </editor-fold>
6567 SchemaUtils .create(ChatSpamTable )
66- ChatSpamTable .deleteWhere {
67- lastAccess.between((- 1L ).localDateTime, (System .currentTimeMillis() - config.userDataExpiresAfter.toMillis()).localDateTime)
68- }
68+ ChatSpamTable .deleteWhere { expiredOp }
6969 }
7070 Bukkit .getOnlinePlayers().forEach { loadSpamData(it.user) }
7171 }
@@ -115,13 +115,20 @@ object CasDataManager {
115115
116116 fun saveSpamData (where : PlayerUser ) {
117117 val spamData = latest(cacheById[where.dbId], cacheByIp[where.addr]) ? : return
118+ val lastAccessValue = kotlin.math.max(spamData.lastAccess, spamData.muteUntil).localDateTime
118119 with (ChatSpamTable ) {
119120 transaction(database) {
120- replace {
121+ val inserted = insertIgnore {
121122 it[user] = where.dbId
122123 it[ip] = where.addr
123- it[lastAccess] = kotlin.math.max(spamData.lastAccess, spamData.muteUntil).localDateTime
124+ it[lastAccess] = lastAccessValue
124125 it[data] = spamData
126+ }.insertedCount
127+ if (inserted == 0 ) {
128+ update({ ((user eq where.dbId) or (ip eq where.addr)) and (lastAccess less lastAccessValue) }) {
129+ it[lastAccess] = lastAccessValue
130+ it[data] = spamData
131+ }
125132 }
126133 }
127134 }
@@ -133,7 +140,7 @@ object CasDataManager {
133140 }
134141 }
135142
136- fun deleteAsync (keys : List <Any ?>) {
143+ fun deleteExpiredAsync (keys : List <Any ?>) {
137144 val byId = mutableListOf<Int >()
138145 val byIp = mutableListOf<String >()
139146 for (any in keys) {
@@ -149,23 +156,26 @@ object CasDataManager {
149156 buildList {
150157 if (byId.isNotEmpty()) add(user inList byId)
151158 if (byIp.isNotEmpty()) add(ip inList byIp)
152- }.compoundOr()
159+ }.compoundOr() and expiredOp
153160 }
154161 }
155162 }
156163 }
157164
158- fun deleteAsync (key : Any? ) {
165+ fun deleteExpiredAsync (key : Any? ) {
159166 val where = when (key) {
160167 is Int -> ChatSpamTable .user eq key
161168 is String -> ChatSpamTable .ip eq key
162169 else -> error(" Unknown key type ${key?.javaClass?.name} ($key )" )
163170 }
164171 StorageManager .coroutineScope.launch {
165172 transaction(database) {
166- ChatSpamTable .deleteWhere { where }
173+ ChatSpamTable .deleteWhere { where and expiredOp }
167174 }
168175 }
169176 }
170177
178+ private val expiredOp
179+ get() = lastAccess.between((- 1L ).localDateTime, (System .currentTimeMillis() - config.userDataExpiresAfter.toMillis()).localDateTime)
180+
171181}
0 commit comments