| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
Good stuff, thanks! I got some remarks regarding readability mostly 👍
Sorry, something went wrong.
…ngs and refactored existing code
|
Sorry, something went wrong.
| if (alreadyFlaggedUsers.contains(userId)) { | ||
| return true; | ||
| } | ||
| if (shouldIgnore(event.getMessage())) { | ||
| return false; | ||
| } | ||
| String hash = addToMessageCache(event).messageHash(); | ||
| if (hasPostedTooManySimilarMessages(userId, hash)) { | ||
| alreadyFlaggedUsers.add(userId); | ||
| return true; | ||
| } else { | ||
| return false; |
There was a problem hiding this comment.
i am not sure if that alreadyFlaggedUsers user thing really works.. the bot has a really long uptime usually, sometimes many months.
the way i read the code is that if a user got flagged once, they will then be immune to this check until the bot is restarted again. u could argue that its probably not an issue in practice but it smells a bit. you would need a time based cleanup for alreadyFlaggedUsers as well.
at which point u have like 30 lines of code dealing with what caffeeine gets done in one line, i guess.
private final Cache<String, Instant> userIdToAskedAtCache = Caffeine.newBuilder()
.maximumSize(1_000)
.expireAfterWrite(Duration.of(10, ChronoUnit.SECONDS))
.build();the interface is essentially that of a Map, so you have various get and put variants. internally its a LRU map, so it kicks out the entries that werent touched the longest when it reaches the max limit.
Sorry, something went wrong.
There was a problem hiding this comment.
how do i make it a set ? if the cache already handles he cleanup, then storing the instant is pointless.
edit: i saw your other comment, i'll see that
Sorry, something went wrong.
There was a problem hiding this comment.
caffeine only has a Map. sometimes in the past we also only needed a Set and then mapped it to something. for example to itself, i.e. key == value.
Sorry, something went wrong.
| * @param text the UTF 8 text to hash | ||
| * @return the computed hash | ||
| */ | ||
| public static byte[] hashUTF8(String method, String text) { |
There was a problem hiding this comment.
NIT: should probably be renamed into hashUtf8 for better camelCase readability
Sorry, something went wrong.
| private MessageInfo addToMessageCache(MessageReceivedEvent event) { | ||
| long userId = event.getAuthor().getIdLong(); | ||
| long channelId = event.getChannel().getIdLong(); | ||
| String messageHash = getHash(event.getMessage()); | ||
| Instant timestamp = event.getMessage().getTimeCreated().toInstant(); | ||
| MessageInfo messageInfo = new MessageInfo(userId, channelId, messageHash, timestamp); | ||
| messageCache.add(messageInfo); | ||
| return messageInfo; |
There was a problem hiding this comment.
id still refactor this and move all the stuff dealing with MessageInfo into the record. so:
record MessageInfo(...) {
static MessageInfo fromMessageEvent(MessageReceivedEvent event) {...}
}then this addToMessageCache method becomes obsolete and can be replaced with just messageCache.add(MessageInfo.fromMessageEvent(event)); at the call-site
the hash creation would then also move into the records fromMessageEvent method (or as private static helper in the record)
Sorry, something went wrong.
There was a problem hiding this comment.
I don't quite like adding a unnecessary dependencies to a record, and even less logic like how to create a hash or how to create it from an event imo.
Sorry, something went wrong.
There was a problem hiding this comment.
mh... but its a factory to create instances of this record. so imo this is exactly the place it should be. what do others here think?
Sorry, something went wrong.
| * @param event the message event | ||
| * @return true if the user spammed the message in several channels, false otherwise | ||
| */ | ||
| public boolean doSimilarMessageCheck(MessageReceivedEvent event) { |
There was a problem hiding this comment.
could u do some reordering so the flow reads top to bottom. this method would then be at the top, as entry-point. and the private methods then below it in the order they are used. the runRoutine can stay at the very bottom, i guess, since its a detached flow.
Sorry, something went wrong.
| "suspiciousAttachmentNamePattern": "(image|\\d{1,2})\\.[^.]{0,5}" | ||
| "suspiciousAttachmentNamePattern": "(image|\\d{1,2})\\.[^.]{0,5}", | ||
| "maxAllowedSimilarMessages": 2, | ||
| "similarMessagesWindow": 1, |
There was a problem hiding this comment.
missing time unit. please add it to the name.
Sorry, something went wrong.
| "maxAllowedSimilarMessages": 2, | ||
| "similarMessagesWindow": 1, | ||
| "similarMessageLengthIgnore": 10, | ||
| "similarMessagesWhitelist": [] |
There was a problem hiding this comment.
i dont think we need that, please remove.
the UX for it for maintainers would be really weird, having to bloat the config with 100 character long messages and whatnot. and then it didnt work bc of a smiley or other stuff that ends up slightly different.
Sorry, something went wrong.
There was a problem hiding this comment.
All 4? or just the whitelist ?
Sorry, something went wrong.
There was a problem hiding this comment.
sorry, the preview didnt turn out the way i wanted. i was referring to the message whitelist.
Sorry, something went wrong.
| isSafe = false; | ||
| } | ||
|
|
||
| if (isSafe && similarMessagesDetector.doSimilarMessageCheck(event)) { |
There was a problem hiding this comment.
doSimilarMessageCheck should be renamed. maybe hasPostedSimilarMessageTooOften(event)
if you dont want it to start with a question-word bc of the side effects, maybe handleHasPostedSimilarMessagesTooOften(event).
but the name should clarify what the boolean it returns means and how it fits into the logic of ScamBlocker
Sorry, something went wrong.
| /** | ||
| * Has to be called often to clear the cache. | ||
| */ | ||
| public void runRoutine() { |
There was a problem hiding this comment.
rename to clearCache or cleanupCache or something.
Sorry, something went wrong.
| return similarMessageCount > scamBlockerConfig.getMaxAllowedSimilarMessages(); | ||
| } | ||
|
|
||
| private boolean isObsolete(MessageInfo messageInfo) { |
There was a problem hiding this comment.
rename to isOutdated or isExpired
Sorry, something went wrong.
| return true; | ||
| } | ||
| return scamBlockerConfig.getSimilarMessagesWhitelist() | ||
| .contains(message.getContentRaw().toLowerCase()); |
There was a problem hiding this comment.
.toLowerCase needs Locale.US
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
From in #1283 with those differences: