Moving spam checks to be after the check if already processed events.

This commit is contained in:
Vitor Pamplona
2023-03-04 08:23:12 -05:00
parent e7be1cc8e7
commit 9432135847
2 changed files with 21 additions and 25 deletions
@@ -12,7 +12,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import com.vitorpamplona.amethyst.service.model.Event import com.vitorpamplona.amethyst.service.model.Event
import nostr.postr.toHex
data class Spammer(val pubkeyHex: HexKey, var duplicatedMessages: Set<HexKey>) data class Spammer(val pubkeyHex: HexKey, var duplicatedMessages: Set<HexKey>)
@@ -24,9 +23,6 @@ class AntiSpamFilter {
fun isSpam(event: Event): Boolean { fun isSpam(event: Event): Boolean {
val idHex = event.id val idHex = event.id
// if already processed, ok
if (LocalCache.notes[idHex] != null) return false
// if short message, ok // if short message, ok
if (event.content.length < 50) return false if (event.content.length < 50) return false
@@ -39,7 +39,6 @@ import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import nostr.postr.toHex
import nostr.postr.toNpub import nostr.postr.toNpub
@@ -169,13 +168,6 @@ object LocalCache {
fun consume(event: TextNoteEvent, relay: Relay? = null) { fun consume(event: TextNoteEvent, relay: Relay? = null) {
if (antiSpam.isSpam(event)) {
relay?.let {
it.spamCounter++
}
return
}
val note = getOrCreateNote(event.id) val note = getOrCreateNote(event.id)
val author = getOrCreateUser(event.pubKey) val author = getOrCreateUser(event.pubKey)
@@ -187,6 +179,13 @@ object LocalCache {
// Already processed this event. // Already processed this event.
if (note.event != null) return if (note.event != null) return
if (antiSpam.isSpam(event)) {
relay?.let {
it.spamCounter++
}
return
}
val mentions = event.mentions().mapNotNull { checkGetOrCreateUser(it) } val mentions = event.mentions().mapNotNull { checkGetOrCreateUser(it) }
val replyTo = replyToWithoutCitations(event).mapNotNull { checkGetOrCreateNote(it) } + val replyTo = replyToWithoutCitations(event).mapNotNull { checkGetOrCreateNote(it) } +
event.taggedAddresses().mapNotNull { getOrCreateAddressableNote(it) } event.taggedAddresses().mapNotNull { getOrCreateAddressableNote(it) }
@@ -215,13 +214,6 @@ object LocalCache {
} }
fun consume(event: LongTextNoteEvent, relay: Relay?) { fun consume(event: LongTextNoteEvent, relay: Relay?) {
if (antiSpam.isSpam(event)) {
relay?.let {
it.spamCounter++
}
return
}
val note = getOrCreateAddressableNote(event.address()) val note = getOrCreateAddressableNote(event.address())
val author = getOrCreateUser(event.pubKey) val author = getOrCreateUser(event.pubKey)
@@ -233,6 +225,13 @@ object LocalCache {
// Already processed this event. // Already processed this event.
if (note.event?.id == event.id) return if (note.event?.id == event.id) return
if (antiSpam.isSpam(event)) {
relay?.let {
it.spamCounter++
}
return
}
val mentions = event.mentions().mapNotNull { checkGetOrCreateUser(it) } val mentions = event.mentions().mapNotNull { checkGetOrCreateUser(it) }
val replyTo = replyToWithoutCitations(event).mapNotNull { checkGetOrCreateNote(it) } val replyTo = replyToWithoutCitations(event).mapNotNull { checkGetOrCreateNote(it) }
@@ -565,12 +564,6 @@ object LocalCache {
val channelId = event.channel() val channelId = event.channel()
if (channelId.isNullOrBlank()) return if (channelId.isNullOrBlank()) return
if (antiSpam.isSpam(event)) {
relay?.let {
it.spamCounter++
}
return
}
val channel = checkGetOrCreateChannel(channelId) ?: return val channel = checkGetOrCreateChannel(channelId) ?: return
@@ -587,6 +580,13 @@ object LocalCache {
// Already processed this event. // Already processed this event.
if (note.event != null) return if (note.event != null) return
if (antiSpam.isSpam(event)) {
relay?.let {
it.spamCounter++
}
return
}
val mentions = event.mentions().mapNotNull { checkGetOrCreateUser(it) } val mentions = event.mentions().mapNotNull { checkGetOrCreateUser(it) }
val replyTo = event.replyTos() val replyTo = event.replyTos()
.mapNotNull { checkGetOrCreateNote(it) } .mapNotNull { checkGetOrCreateNote(it) }