8
8
import bugs .stackoverflow .belisarius .models .Post ;
9
9
import bugs .stackoverflow .belisarius .utils .CheckUtils ;
10
10
import bugs .stackoverflow .belisarius .utils .DatabaseUtils ;
11
-
12
- import org .slf4j .Logger ;
13
- import org .slf4j .LoggerFactory ;
11
+ import bugs .stackoverflow .belisarius .utils .FilterUtils ;
14
12
15
13
public class BlacklistedFilter implements Filter {
16
- private static final Logger LOGGER = LoggerFactory .getLogger (BlacklistedFilter .class );
17
-
18
14
private final int roomId ;
19
15
private final Post post ;
20
16
private final int reasonId = 1 ;
@@ -28,15 +24,36 @@ public BlacklistedFilter(int chatroomId, Post post) {
28
24
@ Override
29
25
public boolean isHit () {
30
26
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
+ );
32
35
}
33
36
34
37
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
+ );
36
46
}
37
47
38
48
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
+ );
40
57
}
41
58
42
59
return getTotalScore () > 0 ;
@@ -50,30 +67,19 @@ public double getScore() {
50
67
@ Override
51
68
public double getTotalScore () {
52
69
int score = 0 ;
70
+
53
71
for (Map <Integer , String > words : blacklistedWords .values ()) {
54
72
score += words .size ();
55
73
}
74
+
56
75
return score ;
57
76
}
58
77
59
78
@ Override
60
79
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" );
77
83
78
84
return message .trim ();
79
85
}
@@ -109,25 +115,38 @@ public Severity getSeverity() {
109
115
110
116
private List <Integer > getCaughtBlacklistedWordIds () {
111
117
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
+
114
123
return blacklistedWordIds ;
115
124
}
116
125
117
126
@ Override
118
127
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 );
125
133
126
134
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 );
131
136
});
132
137
}
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
+ }
133
152
}
0 commit comments