Skip to content

Commit

Permalink
refactor: reorganise filters
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep committed Aug 8, 2024
1 parent 0e98e48 commit 81caa06
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
import bugs.stackoverflow.belisarius.models.Post;
import bugs.stackoverflow.belisarius.utils.CheckUtils;
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import bugs.stackoverflow.belisarius.utils.FilterUtils;

public class BlacklistedFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(BlacklistedFilter.class);

private final int roomId;
private final Post post;
private final int reasonId = 1;
Expand All @@ -28,15 +24,36 @@ public BlacklistedFilter(int chatroomId, Post post) {
@Override
public boolean isHit() {
if (post.getLastTitle() != null && "question".equals(post.getPostType())) {
blacklistedWords.put("title", CheckUtils.checkForBlackListedWords(post.getTitle(), post.getLastTitle(), "question_title"));
blacklistedWords.put(
"title",
CheckUtils.checkForBlackListedWords(
post.getTitle(),
post.getLastTitle(),
"question_title"
)
);
}

if (post.getBody() != null) {
blacklistedWords.put("body", CheckUtils.checkForBlackListedWords(post.getBody(), post.getLastBody(), post.getPostType()));
blacklistedWords.put(
"body",
CheckUtils.checkForBlackListedWords(
post.getBody(),
post.getLastBody(),
post.getPostType()
)
);
}

if (post.getComment() != null) {
blacklistedWords.put("comment", CheckUtils.checkForBlackListedWords(post.getComment(), null, post.getPostType()));
blacklistedWords.put(
"comment",
CheckUtils.checkForBlackListedWords(
post.getComment(),
null,
post.getPostType()
)
);
}

return getTotalScore() > 0;
Expand All @@ -50,30 +67,19 @@ public double getScore() {
@Override
public double getTotalScore() {
int score = 0;

for (Map<Integer, String> words : blacklistedWords.values()) {
score += words.size();
}

return score;
}

@Override
public String getFormattedReasonMessage() {
String message = "";

if (this.blacklistedWords.containsKey("title") && !this.blacklistedWords.get("title").isEmpty()) {
message += "**Title contain blacklisted " + (this.blacklistedWords.get("title").size() > 1 ? "words" : "word") + ":** ";
message += getBlacklistedWords("title") + " ";
}

if (this.blacklistedWords.containsKey("body") && !this.blacklistedWords.get("body").isEmpty()) {
message += "**Body contains blacklisted " + (this.blacklistedWords.get("body").size() > 1 ? "words" : "word") + ":** ";
message += getBlacklistedWords("body") + " ";
}

if (this.blacklistedWords.containsKey("comment") && !this.blacklistedWords.get("comment").isEmpty()) {
message += "**Edit summary contains blacklisted " + (this.blacklistedWords.get("comment").size() > 1 ? "words" : "word") + ":** ";
message += getBlacklistedWords("comment") + " ";
}
String message = getFormattedMessage("title", "title")
+ getFormattedMessage("body", "body")
+ getFormattedMessage("comment", "Edit summary");

return message.trim();
}
Expand Down Expand Up @@ -109,25 +115,38 @@ public Severity getSeverity() {

private List<Integer> getCaughtBlacklistedWordIds() {
List<Integer> blacklistedWordIds = new ArrayList<>();
// for each of the hashmap of the blacklistedWords hashmap, add the keys to blacklistedWordIds
blacklistedWords.values().forEach(wordsMap -> blacklistedWordIds.addAll(wordsMap.keySet()));

blacklistedWords
.values()
.forEach(wordsMap -> blacklistedWordIds.addAll(wordsMap.keySet()));

return blacklistedWordIds;
}

@Override
public void storeHit() {
long postId = this.post.getPostId();
int revisionNumber = this.post.getRevisionNumber();
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
LOGGER.info("Successfully stored reason BlacklistedFilter for post " + postId + " to database.");
}
long postId = post.getPostId();
int revisionNumber = post.getRevisionNumber();
double score = getScore();

DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);

this.getCaughtBlacklistedWordIds().forEach(id -> {
if (!DatabaseUtils.checkBlacklistedWordCaughtExists(postId, revisionNumber, this.roomId, id)) {
DatabaseUtils.storeCaughtBlacklistedWord(postId, revisionNumber, this.roomId, id);
LOGGER.info("Successfully stored blacklisted word id for post " + postId + " to database.");
}
DatabaseUtils.storeBlacklistedWord(postId, revisionNumber, this.roomId, id);
});
}

private String getFormattedMessage(
String field,
String description
) {
if (blacklistedWords.containsKey(field) && !blacklistedWords.get(field).isEmpty()) {
return "**" + description + " contains blacklisted "
+ FilterUtils.pluralise("word", blacklistedWords.get(field).size())
+ ":** "
+ getBlacklistedWords(field) + " ";
}

return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
import bugs.stackoverflow.belisarius.utils.CheckUtils;
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CodeRemovedFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(CodeRemovedFilter.class);

private final int roomId;
private final Post post;
private final int reasonId = 2;
Expand All @@ -25,10 +20,16 @@ public CodeRemovedFilter(int chatroomId, Post post) {

@Override
public boolean isHit() {
// Check if code has been removed when the post is a question (https://chat.stackoverflow.com/transcript/message/50208463)
if (post.getLastBody() != null && post.getBody() != null && "question".equals(post.getPostType())) {
return !CheckUtils.checkIfNoCodeBlock(post.getLastBody()) && CheckUtils.checkIfNoCodeBlock(post.getBody());
// Check if code has been removed when the post is a question
// (https://chat.stackoverflow.com/transcript/message/50208463)
if (post.getLastBody() != null
&& post.getBody() != null
&& "question".equals(post.getPostType())
) {
return !CheckUtils.checkIfNoCodeBlock(post.getLastBody())
&& CheckUtils.checkIfNoCodeBlock(post.getBody());
}

return false;
}

Expand Down Expand Up @@ -59,11 +60,10 @@ public Severity getSeverity() {

@Override
public void storeHit() {
long postId = this.post.getPostId();
int revisionNumber = this.post.getRevisionNumber();
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
LOGGER.info("Successfully stored reason CodeRemovedFilter for post " + postId + " to database.");
}
long postId = post.getPostId();
int revisionNumber = post.getRevisionNumber();
double score = getScore();

DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;
import bugs.stackoverflow.belisarius.utils.JsonUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FewUniqueCharactersFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(FewUniqueCharactersFilter.class);

private final int roomId;
private final Post post;
private final int reasonId = 3;
Expand All @@ -28,10 +23,13 @@ public FewUniqueCharactersFilter(int chatroomId, Post post) {
@Override
public boolean isHit() {
this.listedWord = "";

if (post.getBody() != null) {
this.listedWord = CheckUtils.checkForFewUniqueCharacters(post.getBody());

return this.listedWord != null;
}

return false;
}

Expand All @@ -47,7 +45,8 @@ public double getTotalScore() {

@Override
public String getFormattedReasonMessage() {
// make sure to escape characters like [], see https://chat.stackoverflow.com/messages/51803561/history
// make sure to escape characters like [], see
// https://chat.stackoverflow.com/messages/51803561/history
return "**Few unique characters detected:** " + JsonUtils.sanitizeChatMessage(this.listedWord);
}

Expand All @@ -63,12 +62,10 @@ public Severity getSeverity() {

@Override
public void storeHit() {
long postId = this.post.getPostId();
int revisionNumber = this.post.getRevisionNumber();
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
LOGGER.info("Successfully stored reasonFewUniqueCharactersFilter for post " + postId + " to database.");
}
}
long postId = post.getPostId();
int revisionNumber = post.getRevisionNumber();
double score = getScore();

DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
import bugs.stackoverflow.belisarius.models.Post;
import bugs.stackoverflow.belisarius.utils.CheckUtils;
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import bugs.stackoverflow.belisarius.utils.FilterUtils;

public class OffensiveWordFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(OffensiveWordFilter.class);

private final int roomId;
private final Post post;
private final int reasonId = 4;
Expand All @@ -28,8 +24,7 @@ public OffensiveWordFilter(int chatroomId, Post post) {

@Override
public boolean isHit() {

if (this.post.getComment() != null) {
if (post.getComment() != null) {
offensiveWords = CheckUtils.checkForOffensiveWords(this.post.getComment());
}

Expand All @@ -43,12 +38,14 @@ public double getScore() {

@Override
public double getTotalScore() {
return this.offensiveWords.size();
return offensiveWords.size();
}

@Override
public String getFormattedReasonMessage() {
return "**Edit summary contains offensive " + (this.offensiveWords.size() > 1 ? "words" : "word") + ":** " + getOffensiveWords();
return "**Edit summary contains offensive "
+ FilterUtils.pluralise("word", offensiveWords.size()) + ":** "
+ getOffensiveWords();
}

@Override
Expand All @@ -57,7 +54,10 @@ public List<String> getReasonName() {
List<String> words = new ArrayList<>();

// add name + word to the words list
offensiveWords.values().forEach(word -> words.add(name + word));
offensiveWords
.values()
.forEach(word -> words.add(name + word));

return words;
}

Expand All @@ -82,17 +82,14 @@ private List<Integer> getCaughtOffensiveWordIds() {

@Override
public void storeHit() {
long postId = this.post.getPostId();
int revisionNumber = this.post.getRevisionNumber();
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
}
long postId = post.getPostId();
int revisionNumber = post.getRevisionNumber();
double score = getScore();

DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);

this.getCaughtOffensiveWordIds().forEach(id -> {
if (!DatabaseUtils.checkOffensiveWordCaughtExists(postId, revisionNumber, this.roomId, id)) {
DatabaseUtils.storeCaughtOffensiveWord(postId, revisionNumber, this.roomId, id);
LOGGER.info("Successfully stored offensive word id for post " + postId + " to database.");
}
DatabaseUtils.storeBlacklistedWord(postId, revisionNumber, this.roomId, id);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
import bugs.stackoverflow.belisarius.models.Post;
import bugs.stackoverflow.belisarius.utils.CheckUtils;
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import bugs.stackoverflow.belisarius.utils.FilterUtils;

public class RepeatedWordFilter implements Filter {
private static final Logger LOGGER = LoggerFactory.getLogger(RepeatedWordFilter.class);

private final int roomId;
private final Post post;
private final int reasonId = 5;
Expand All @@ -28,12 +24,12 @@ public RepeatedWordFilter(int chatroomId, Post post) {

@Override
public boolean isHit() {

if (this.post.getBody() != null) {
if (post.getBody() != null) {
repeatedWords = CheckUtils.checkRepeatedWords(this.post.getBody());
}

double score = getScore();

return score > 0 && score <= 5;
}

Expand All @@ -51,8 +47,10 @@ public double getTotalScore() {
public String getFormattedReasonMessage() {
String message = "";

if (this.repeatedWords.size() > 0) {
message += "**Post contains repeated " + (this.repeatedWords.size() > 1 ? "words" : "word") + ":** " + getRepeatedWords() + " ";
if (!repeatedWords.isEmpty()) {
message += "**Post contains repeated "
+ FilterUtils.pluralise("word", repeatedWords.size())
+ ":** " + getRepeatedWords() + " ";
}

return message.trim();
Expand Down Expand Up @@ -80,11 +78,10 @@ public Severity getSeverity() {

@Override
public void storeHit() {
long postId = this.post.getPostId();
int revisionNumber = this.post.getRevisionNumber();
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
LOGGER.info("Successfully stored reason RepeatedWordFilter for post " + postId + " to database.");
}
long postId = post.getPostId();
int revisionNumber = post.getRevisionNumber();
double score = getScore();

DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
}
}
Loading

0 comments on commit 81caa06

Please sign in to comment.