Skip to content

Commit 81caa06

Browse files
committed
refactor: reorganise filters
1 parent 0e98e48 commit 81caa06

14 files changed

+256
-143
lines changed

src/main/java/bugs/stackoverflow/belisarius/filters/BlacklistedFilter.java

+55-36
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
import bugs.stackoverflow.belisarius.models.Post;
99
import bugs.stackoverflow.belisarius.utils.CheckUtils;
1010
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;
11-
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
11+
import bugs.stackoverflow.belisarius.utils.FilterUtils;
1412

1513
public class BlacklistedFilter implements Filter {
16-
private static final Logger LOGGER = LoggerFactory.getLogger(BlacklistedFilter.class);
17-
1814
private final int roomId;
1915
private final Post post;
2016
private final int reasonId = 1;
@@ -28,15 +24,36 @@ public BlacklistedFilter(int chatroomId, Post post) {
2824
@Override
2925
public boolean isHit() {
3026
if (post.getLastTitle() != null && "question".equals(post.getPostType())) {
31-
blacklistedWords.put("title", CheckUtils.checkForBlackListedWords(post.getTitle(), post.getLastTitle(), "question_title"));
27+
blacklistedWords.put(
28+
"title",
29+
CheckUtils.checkForBlackListedWords(
30+
post.getTitle(),
31+
post.getLastTitle(),
32+
"question_title"
33+
)
34+
);
3235
}
3336

3437
if (post.getBody() != null) {
35-
blacklistedWords.put("body", CheckUtils.checkForBlackListedWords(post.getBody(), post.getLastBody(), post.getPostType()));
38+
blacklistedWords.put(
39+
"body",
40+
CheckUtils.checkForBlackListedWords(
41+
post.getBody(),
42+
post.getLastBody(),
43+
post.getPostType()
44+
)
45+
);
3646
}
3747

3848
if (post.getComment() != null) {
39-
blacklistedWords.put("comment", CheckUtils.checkForBlackListedWords(post.getComment(), null, post.getPostType()));
49+
blacklistedWords.put(
50+
"comment",
51+
CheckUtils.checkForBlackListedWords(
52+
post.getComment(),
53+
null,
54+
post.getPostType()
55+
)
56+
);
4057
}
4158

4259
return getTotalScore() > 0;
@@ -50,30 +67,19 @@ public double getScore() {
5067
@Override
5168
public double getTotalScore() {
5269
int score = 0;
70+
5371
for (Map<Integer, String> words : blacklistedWords.values()) {
5472
score += words.size();
5573
}
74+
5675
return score;
5776
}
5877

5978
@Override
6079
public String getFormattedReasonMessage() {
61-
String message = "";
62-
63-
if (this.blacklistedWords.containsKey("title") && !this.blacklistedWords.get("title").isEmpty()) {
64-
message += "**Title contain blacklisted " + (this.blacklistedWords.get("title").size() > 1 ? "words" : "word") + ":** ";
65-
message += getBlacklistedWords("title") + " ";
66-
}
67-
68-
if (this.blacklistedWords.containsKey("body") && !this.blacklistedWords.get("body").isEmpty()) {
69-
message += "**Body contains blacklisted " + (this.blacklistedWords.get("body").size() > 1 ? "words" : "word") + ":** ";
70-
message += getBlacklistedWords("body") + " ";
71-
}
72-
73-
if (this.blacklistedWords.containsKey("comment") && !this.blacklistedWords.get("comment").isEmpty()) {
74-
message += "**Edit summary contains blacklisted " + (this.blacklistedWords.get("comment").size() > 1 ? "words" : "word") + ":** ";
75-
message += getBlacklistedWords("comment") + " ";
76-
}
80+
String message = getFormattedMessage("title", "title")
81+
+ getFormattedMessage("body", "body")
82+
+ getFormattedMessage("comment", "Edit summary");
7783

7884
return message.trim();
7985
}
@@ -109,25 +115,38 @@ public Severity getSeverity() {
109115

110116
private List<Integer> getCaughtBlacklistedWordIds() {
111117
List<Integer> blacklistedWordIds = new ArrayList<>();
112-
// for each of the hashmap of the blacklistedWords hashmap, add the keys to blacklistedWordIds
113-
blacklistedWords.values().forEach(wordsMap -> blacklistedWordIds.addAll(wordsMap.keySet()));
118+
119+
blacklistedWords
120+
.values()
121+
.forEach(wordsMap -> blacklistedWordIds.addAll(wordsMap.keySet()));
122+
114123
return blacklistedWordIds;
115124
}
116125

117126
@Override
118127
public void storeHit() {
119-
long postId = this.post.getPostId();
120-
int revisionNumber = this.post.getRevisionNumber();
121-
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
122-
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
123-
LOGGER.info("Successfully stored reason BlacklistedFilter for post " + postId + " to database.");
124-
}
128+
long postId = post.getPostId();
129+
int revisionNumber = post.getRevisionNumber();
130+
double score = getScore();
131+
132+
DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
125133

126134
this.getCaughtBlacklistedWordIds().forEach(id -> {
127-
if (!DatabaseUtils.checkBlacklistedWordCaughtExists(postId, revisionNumber, this.roomId, id)) {
128-
DatabaseUtils.storeCaughtBlacklistedWord(postId, revisionNumber, this.roomId, id);
129-
LOGGER.info("Successfully stored blacklisted word id for post " + postId + " to database.");
130-
}
135+
DatabaseUtils.storeBlacklistedWord(postId, revisionNumber, this.roomId, id);
131136
});
132137
}
138+
139+
private String getFormattedMessage(
140+
String field,
141+
String description
142+
) {
143+
if (blacklistedWords.containsKey(field) && !blacklistedWords.get(field).isEmpty()) {
144+
return "**" + description + " contains blacklisted "
145+
+ FilterUtils.pluralise("word", blacklistedWords.get(field).size())
146+
+ ":** "
147+
+ getBlacklistedWords(field) + " ";
148+
}
149+
150+
return "";
151+
}
133152
}

src/main/java/bugs/stackoverflow/belisarius/filters/CodeRemovedFilter.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
import bugs.stackoverflow.belisarius.utils.CheckUtils;
99
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;
1010

11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
13-
1411
public class CodeRemovedFilter implements Filter {
15-
private static final Logger LOGGER = LoggerFactory.getLogger(CodeRemovedFilter.class);
16-
1712
private final int roomId;
1813
private final Post post;
1914
private final int reasonId = 2;
@@ -25,10 +20,16 @@ public CodeRemovedFilter(int chatroomId, Post post) {
2520

2621
@Override
2722
public boolean isHit() {
28-
// Check if code has been removed when the post is a question (https://chat.stackoverflow.com/transcript/message/50208463)
29-
if (post.getLastBody() != null && post.getBody() != null && "question".equals(post.getPostType())) {
30-
return !CheckUtils.checkIfNoCodeBlock(post.getLastBody()) && CheckUtils.checkIfNoCodeBlock(post.getBody());
23+
// Check if code has been removed when the post is a question
24+
// (https://chat.stackoverflow.com/transcript/message/50208463)
25+
if (post.getLastBody() != null
26+
&& post.getBody() != null
27+
&& "question".equals(post.getPostType())
28+
) {
29+
return !CheckUtils.checkIfNoCodeBlock(post.getLastBody())
30+
&& CheckUtils.checkIfNoCodeBlock(post.getBody());
3131
}
32+
3233
return false;
3334
}
3435

@@ -59,11 +60,10 @@ public Severity getSeverity() {
5960

6061
@Override
6162
public void storeHit() {
62-
long postId = this.post.getPostId();
63-
int revisionNumber = this.post.getRevisionNumber();
64-
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
65-
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
66-
LOGGER.info("Successfully stored reason CodeRemovedFilter for post " + postId + " to database.");
67-
}
63+
long postId = post.getPostId();
64+
int revisionNumber = post.getRevisionNumber();
65+
double score = getScore();
66+
67+
DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
6868
}
6969
}

src/main/java/bugs/stackoverflow/belisarius/filters/FewUniqueCharactersFilter.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;
1010
import bugs.stackoverflow.belisarius.utils.JsonUtils;
1111

12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
14-
1512
public class FewUniqueCharactersFilter implements Filter {
16-
private static final Logger LOGGER = LoggerFactory.getLogger(FewUniqueCharactersFilter.class);
17-
1813
private final int roomId;
1914
private final Post post;
2015
private final int reasonId = 3;
@@ -28,10 +23,13 @@ public FewUniqueCharactersFilter(int chatroomId, Post post) {
2823
@Override
2924
public boolean isHit() {
3025
this.listedWord = "";
26+
3127
if (post.getBody() != null) {
3228
this.listedWord = CheckUtils.checkForFewUniqueCharacters(post.getBody());
29+
3330
return this.listedWord != null;
3431
}
32+
3533
return false;
3634
}
3735

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

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

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

6463
@Override
6564
public void storeHit() {
66-
long postId = this.post.getPostId();
67-
int revisionNumber = this.post.getRevisionNumber();
68-
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
69-
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
70-
LOGGER.info("Successfully stored reasonFewUniqueCharactersFilter for post " + postId + " to database.");
71-
}
72-
}
65+
long postId = post.getPostId();
66+
int revisionNumber = post.getRevisionNumber();
67+
double score = getScore();
7368

69+
DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
70+
}
7471
}

src/main/java/bugs/stackoverflow/belisarius/filters/OffensiveWordFilter.java

+16-19
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
import bugs.stackoverflow.belisarius.models.Post;
99
import bugs.stackoverflow.belisarius.utils.CheckUtils;
1010
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;
11-
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
11+
import bugs.stackoverflow.belisarius.utils.FilterUtils;
1412

1513
public class OffensiveWordFilter implements Filter {
16-
private static final Logger LOGGER = LoggerFactory.getLogger(OffensiveWordFilter.class);
17-
1814
private final int roomId;
1915
private final Post post;
2016
private final int reasonId = 4;
@@ -28,8 +24,7 @@ public OffensiveWordFilter(int chatroomId, Post post) {
2824

2925
@Override
3026
public boolean isHit() {
31-
32-
if (this.post.getComment() != null) {
27+
if (post.getComment() != null) {
3328
offensiveWords = CheckUtils.checkForOffensiveWords(this.post.getComment());
3429
}
3530

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

4439
@Override
4540
public double getTotalScore() {
46-
return this.offensiveWords.size();
41+
return offensiveWords.size();
4742
}
4843

4944
@Override
5045
public String getFormattedReasonMessage() {
51-
return "**Edit summary contains offensive " + (this.offensiveWords.size() > 1 ? "words" : "word") + ":** " + getOffensiveWords();
46+
return "**Edit summary contains offensive "
47+
+ FilterUtils.pluralise("word", offensiveWords.size()) + ":** "
48+
+ getOffensiveWords();
5249
}
5350

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

5956
// add name + word to the words list
60-
offensiveWords.values().forEach(word -> words.add(name + word));
57+
offensiveWords
58+
.values()
59+
.forEach(word -> words.add(name + word));
60+
6161
return words;
6262
}
6363

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

8383
@Override
8484
public void storeHit() {
85-
long postId = this.post.getPostId();
86-
int revisionNumber = this.post.getRevisionNumber();
87-
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
88-
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
89-
}
85+
long postId = post.getPostId();
86+
int revisionNumber = post.getRevisionNumber();
87+
double score = getScore();
88+
89+
DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
9090

9191
this.getCaughtOffensiveWordIds().forEach(id -> {
92-
if (!DatabaseUtils.checkOffensiveWordCaughtExists(postId, revisionNumber, this.roomId, id)) {
93-
DatabaseUtils.storeCaughtOffensiveWord(postId, revisionNumber, this.roomId, id);
94-
LOGGER.info("Successfully stored offensive word id for post " + postId + " to database.");
95-
}
92+
DatabaseUtils.storeBlacklistedWord(postId, revisionNumber, this.roomId, id);
9693
});
9794
}
9895
}

src/main/java/bugs/stackoverflow/belisarius/filters/RepeatedWordFilter.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
import bugs.stackoverflow.belisarius.models.Post;
1010
import bugs.stackoverflow.belisarius.utils.CheckUtils;
1111
import bugs.stackoverflow.belisarius.utils.DatabaseUtils;
12-
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
12+
import bugs.stackoverflow.belisarius.utils.FilterUtils;
1513

1614
public class RepeatedWordFilter implements Filter {
17-
private static final Logger LOGGER = LoggerFactory.getLogger(RepeatedWordFilter.class);
18-
1915
private final int roomId;
2016
private final Post post;
2117
private final int reasonId = 5;
@@ -28,12 +24,12 @@ public RepeatedWordFilter(int chatroomId, Post post) {
2824

2925
@Override
3026
public boolean isHit() {
31-
32-
if (this.post.getBody() != null) {
27+
if (post.getBody() != null) {
3328
repeatedWords = CheckUtils.checkRepeatedWords(this.post.getBody());
3429
}
3530

3631
double score = getScore();
32+
3733
return score > 0 && score <= 5;
3834
}
3935

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

54-
if (this.repeatedWords.size() > 0) {
55-
message += "**Post contains repeated " + (this.repeatedWords.size() > 1 ? "words" : "word") + ":** " + getRepeatedWords() + " ";
50+
if (!repeatedWords.isEmpty()) {
51+
message += "**Post contains repeated "
52+
+ FilterUtils.pluralise("word", repeatedWords.size())
53+
+ ":** " + getRepeatedWords() + " ";
5654
}
5755

5856
return message.trim();
@@ -80,11 +78,10 @@ public Severity getSeverity() {
8078

8179
@Override
8280
public void storeHit() {
83-
long postId = this.post.getPostId();
84-
int revisionNumber = this.post.getRevisionNumber();
85-
if (!DatabaseUtils.checkReasonCaughtExists(postId, revisionNumber, this.roomId, this.reasonId)) {
86-
DatabaseUtils.storeReasonCaught(postId, revisionNumber, this.roomId, this.reasonId, this.getScore());
87-
LOGGER.info("Successfully stored reason RepeatedWordFilter for post " + postId + " to database.");
88-
}
81+
long postId = post.getPostId();
82+
int revisionNumber = post.getRevisionNumber();
83+
double score = getScore();
84+
85+
DatabaseUtils.storeReason(postId, revisionNumber, roomId, reasonId, score);
8986
}
9087
}

0 commit comments

Comments
 (0)