Skip to content

Commit 2f2a1db

Browse files
committed
improved readability by moving stuff around
* analyse went into its own TokenAnalyse class * data holder classes were extracted as well * added unit test for non trivial stuff in Attachment class
1 parent f3fdb03 commit 2f2a1db

File tree

5 files changed

+390
-215
lines changed

5 files changed

+390
-215
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.togetherjava.tjbot.features.moderation.scam;
2+
3+
import javax.annotation.Nullable;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.Objects;
8+
import java.util.StringJoiner;
9+
10+
final class AnalyseResults {
11+
private boolean pingsEveryone;
12+
private boolean containsSuspiciousKeyword;
13+
private boolean containsDollarSign;
14+
private boolean onlyContainsUrls = true;
15+
private final Collection<AnalyseUrlResult> urls = new ArrayList<>();
16+
17+
void addUrlResult(AnalyseUrlResult result) {
18+
urls.add(result);
19+
}
20+
21+
boolean hasUrl() {
22+
return !urls.isEmpty();
23+
}
24+
25+
boolean hasSuspiciousUrl() {
26+
return urls.stream().anyMatch(url -> url.isSuspicious);
27+
}
28+
29+
boolean areAllUrlsWithAttachments() {
30+
return urls.stream().allMatch(url -> url.containedAttachment != null);
31+
}
32+
33+
Collection<Attachment> getUrlAttachments() {
34+
return urls.stream().map(url -> url.containedAttachment).filter(Objects::nonNull).toList();
35+
}
36+
37+
boolean pingsEveryone() {
38+
return pingsEveryone;
39+
}
40+
41+
void markPingsEveryone() {
42+
pingsEveryone = true;
43+
}
44+
45+
boolean containsSuspiciousKeyword() {
46+
return containsSuspiciousKeyword;
47+
}
48+
49+
void markContainsSuspiciousKeyword() {
50+
containsSuspiciousKeyword = true;
51+
}
52+
53+
boolean containsDollarSign() {
54+
return containsDollarSign;
55+
}
56+
57+
void markContainsDollarSign() {
58+
containsDollarSign = true;
59+
}
60+
61+
boolean onlyContainsUrls() {
62+
return onlyContainsUrls;
63+
}
64+
65+
void markNonUrlTokenFound() {
66+
onlyContainsUrls = false;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return new StringJoiner(", ", AnalyseResults.class.getSimpleName() + "[", "]")
72+
.add("pingsEveryone=" + pingsEveryone)
73+
.add("containsSuspiciousKeyword=" + containsSuspiciousKeyword)
74+
.add("containsDollarSign=" + containsDollarSign)
75+
.add("onlyContainsUrls=" + onlyContainsUrls)
76+
.add("urls=" + urls)
77+
.toString();
78+
}
79+
80+
static final class AnalyseUrlResult {
81+
private boolean isSuspicious;
82+
@Nullable
83+
private Attachment containedAttachment;
84+
85+
@Override
86+
public String toString() {
87+
return new StringJoiner(", ", AnalyseUrlResult.class.getSimpleName() + "[", "]")
88+
.add("isSuspicious=" + isSuspicious)
89+
.add("containedAttachment=" + containedAttachment)
90+
.toString();
91+
}
92+
93+
boolean isSuspicious() {
94+
return isSuspicious;
95+
}
96+
97+
void markSuspicious() {
98+
isSuspicious = true;
99+
}
100+
101+
void setContainedAttachment(Attachment containedAttachment) {
102+
this.containedAttachment = containedAttachment;
103+
}
104+
}
105+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.togetherjava.tjbot.features.moderation.scam;
2+
3+
import net.dv8tion.jda.api.entities.Message;
4+
5+
import java.util.Optional;
6+
import java.util.Set;
7+
8+
record Attachment(String fileName) {
9+
private static final Set<String> IMAGE_EXTENSIONS =
10+
Set.of("jpg", "jpeg", "png", "gif", "webp", "tiff", "svg", "apng");
11+
12+
boolean isImage() {
13+
return getFileExtension().map(IMAGE_EXTENSIONS::contains).orElse(false);
14+
}
15+
16+
private Optional<String> getFileExtension() {
17+
int dot = fileName.lastIndexOf('.');
18+
if (dot == -1) {
19+
return Optional.empty();
20+
}
21+
String extension = fileName.substring(dot + 1);
22+
return Optional.of(extension);
23+
}
24+
25+
static Attachment fromDiscord(Message.Attachment attachment) {
26+
return new Attachment(attachment.getFileName());
27+
}
28+
29+
static Attachment fromUrlPath(String urlPath) {
30+
int fileNameStart = urlPath.lastIndexOf('/');
31+
String fileName = fileNameStart == -1 ? "" : urlPath.substring(fileNameStart + 1);
32+
return new Attachment(fileName);
33+
}
34+
}

0 commit comments

Comments
 (0)