Skip to content

Commit 19b3503

Browse files
committed
Fix reviews
1 parent 08f4dbf commit 19b3503

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

application/src/main/java/org/togetherjava/tjbot/features/tags/TagCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.togetherjava.tjbot.features.CommandVisibility;
2020
import org.togetherjava.tjbot.features.SlashCommandAdapter;
2121
import org.togetherjava.tjbot.features.utils.LinkDetections;
22+
import org.togetherjava.tjbot.features.utils.LinkFilter;
2223
import org.togetherjava.tjbot.features.utils.LinkPreview;
2324
import org.togetherjava.tjbot.features.utils.LinkPreviews;
2425
import org.togetherjava.tjbot.features.utils.StringDistances;
@@ -94,7 +95,8 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
9495
.map(OptionMapping::getAsUser)
9596
.map(User::getAsMention);
9697

97-
List<String> links = LinkDetections.extractLinks(tagContent, true, true)
98+
List<String> links = LinkDetections
99+
.extractLinks(tagContent, Set.of(LinkFilter.SUPPRESSED, LinkFilter.NON_HTTP_SCHEME))
98100
.stream()
99101
.limit(Message.MAX_EMBED_COUNT - 1L)
100102
.toList();

application/src/main/java/org/togetherjava/tjbot/features/utils/LinkDetections.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
import java.util.List;
88
import java.util.Optional;
9+
import java.util.Set;
910

1011
/**
1112
* Utility class to detect links.
1213
*/
1314
public class LinkDetections {
1415

16+
1517
private LinkDetections() {
1618
throw new UnsupportedOperationException("Utility class");
1719
}
@@ -20,15 +22,13 @@ private LinkDetections() {
2022
* Extracts all links from the given content.
2123
*
2224
* @param content the content to search through
23-
* @param filterSuppressed filters links suppressed with {@literal <url>}
24-
* @param filterNonHttpSchemes filters links that are not using http scheme
25+
* @param filter the filters applied to the urls
2526
* @return a list of all found links, can be empty
2627
*/
27-
public static List<String> extractLinks(String content, boolean filterSuppressed,
28-
boolean filterNonHttpSchemes) {
28+
public static List<String> extractLinks(String content, Set<LinkFilter> filter) {
2929
return new UrlDetector(content, UrlDetectorOptions.BRACKET_MATCH).detect()
3030
.stream()
31-
.map(url -> toLink(url, filterSuppressed, filterNonHttpSchemes))
31+
.map(url -> toLink(url, filter))
3232
.flatMap(Optional::stream)
3333
.toList();
3434
}
@@ -43,16 +43,15 @@ public static boolean containsLink(String content) {
4343
return !(new UrlDetector(content, UrlDetectorOptions.BRACKET_MATCH).detect().isEmpty());
4444
}
4545

46-
private static Optional<String> toLink(Url url, boolean filterSuppressed,
47-
boolean filterNonHttpSchemes) {
46+
private static Optional<String> toLink(Url url, Set<LinkFilter> filter) {
4847
String raw = url.getOriginalUrl();
49-
if (filterSuppressed && raw.contains(">")) {
48+
if (filter.contains(LinkFilter.SUPPRESSED) && raw.contains(">")) {
5049
// URL escapes, such as "<http://example.com>" should be skipped
5150
return Optional.empty();
5251
}
5352
// Not interested in other schemes, also to filter out matches without scheme.
5453
// It detects a lot of such false-positives in Java snippets
55-
if (filterNonHttpSchemes && !raw.startsWith("http")) {
54+
if (filter.contains(LinkFilter.NON_HTTP_SCHEME) && !raw.startsWith("http")) {
5655
return Optional.empty();
5756
}
5857

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.togetherjava.tjbot.features.utils;
2+
3+
/**
4+
* Possible ways to filter a link.
5+
*
6+
* @see LinkDetections
7+
*/
8+
public enum LinkFilter {
9+
/**
10+
* Filters links suppressed with {@literal <url>}.
11+
*/
12+
SUPPRESSED,
13+
/**
14+
* Filters links that are not using http scheme.
15+
*/
16+
NON_HTTP_SCHEME;
17+
}

0 commit comments

Comments
 (0)