Skip to content

Commit e4054da

Browse files
committed
[ChatAntiSpamModule] Re-design sql statements, handle same ip players properly
1 parent d33d8fd commit e4054da

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/ChatAntiSpamModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ object ChatAntiSpamModule: BukkitModule<ChatAntiSpamModule.ModuleConfig, ChatAnt
113113
"player", UserParser.parser(), DefaultValue.dynamic { it.sender() as PlayerUser }, UserParser()
114114
).handler { context ->
115115
val playerUser = context.get<PlayerUser>("player")
116-
CasDataManager.deleteAsync(playerUser.dbId)
116+
CasDataManager.deleteExpiredAsync(playerUser.dbId)
117117
CasDataManager.cacheById.remove(playerUser.dbId)
118118
CasDataManager.cacheByIp.remove(playerUser.addr)
119119
context.sender().message(locale, { command.reset.resetPlayer },
@@ -166,7 +166,7 @@ object ChatAntiSpamModule: BukkitModule<ChatAntiSpamModule.ModuleConfig, ChatAnt
166166
handler.accept(CasDataManager.cacheById)
167167
handler.accept(CasDataManager.cacheByIp)
168168
if (toDel.isNotEmpty()) {
169-
CasDataManager.deleteAsync(keys = toDel)
169+
CasDataManager.deleteExpiredAsync(keys = toDel)
170170
}
171171
}
172172

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/chatantispam/user/CasDataManager.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.rothes.esu.bukkit.module.chatantispam.user
22

33
import io.github.rothes.esu.bukkit.module.ChatAntiSpamModule.addr
44
import io.github.rothes.esu.bukkit.module.ChatAntiSpamModule.config
5+
import io.github.rothes.esu.bukkit.module.chatantispam.user.CasDataManager.ChatSpamTable.lastAccess
56
import io.github.rothes.esu.bukkit.module.chatantispam.user.CasDataManager.ChatSpamTable.tableName
67
import io.github.rothes.esu.bukkit.user
78
import io.github.rothes.esu.bukkit.user.PlayerUser
@@ -20,9 +21,10 @@ import org.jetbrains.exposed.v1.core.SqlExpressionBuilder.inList
2021
import org.jetbrains.exposed.v1.datetime.datetime
2122
import org.jetbrains.exposed.v1.jdbc.SchemaUtils
2223
import org.jetbrains.exposed.v1.jdbc.deleteWhere
23-
import org.jetbrains.exposed.v1.jdbc.replace
24+
import org.jetbrains.exposed.v1.jdbc.insertIgnore
2425
import org.jetbrains.exposed.v1.jdbc.selectAll
2526
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
27+
import org.jetbrains.exposed.v1.jdbc.update
2628
import org.jetbrains.exposed.v1.json.json
2729

2830
object 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

Comments
 (0)