|
| 1 | +package org.togetherjava.tjbot.features.utils; |
| 2 | + |
| 3 | +import com.linkedin.urls.Url; |
| 4 | +import com.linkedin.urls.detection.UrlDetector; |
| 5 | +import com.linkedin.urls.detection.UrlDetectorOptions; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +/** |
| 12 | + * Utility class to detect links. |
| 13 | + */ |
| 14 | +public class LinkDetection { |
| 15 | + |
| 16 | + /** |
| 17 | + * Possible ways to filter a link. |
| 18 | + * |
| 19 | + * @see LinkDetection |
| 20 | + */ |
| 21 | + public enum LinkFilter { |
| 22 | + /** |
| 23 | + * Filters links suppressed with {@literal <url>}. |
| 24 | + */ |
| 25 | + SUPPRESSED, |
| 26 | + /** |
| 27 | + * Filters links that are not using http scheme. |
| 28 | + */ |
| 29 | + NON_HTTP_SCHEME |
| 30 | + } |
| 31 | + |
| 32 | + private LinkDetection() { |
| 33 | + throw new UnsupportedOperationException("Utility class"); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Extracts all links from the given content. |
| 38 | + * |
| 39 | + * @param content the content to search through |
| 40 | + * @param filter the filters applied to the urls |
| 41 | + * @return a list of all found links, can be empty |
| 42 | + */ |
| 43 | + public static List<String> extractLinks(String content, Set<LinkFilter> filter) { |
| 44 | + return new UrlDetector(content, UrlDetectorOptions.BRACKET_MATCH).detect() |
| 45 | + .stream() |
| 46 | + .map(url -> toLink(url, filter)) |
| 47 | + .flatMap(Optional::stream) |
| 48 | + .toList(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Checks whether the given content contains a link. |
| 53 | + * |
| 54 | + * @param content the content to search through |
| 55 | + * @return true if the content contains at least one link |
| 56 | + */ |
| 57 | + public static boolean containsLink(String content) { |
| 58 | + return !(new UrlDetector(content, UrlDetectorOptions.BRACKET_MATCH).detect().isEmpty()); |
| 59 | + } |
| 60 | + |
| 61 | + private static Optional<String> toLink(Url url, Set<LinkFilter> filter) { |
| 62 | + String raw = url.getOriginalUrl(); |
| 63 | + if (filter.contains(LinkFilter.SUPPRESSED) && raw.contains(">")) { |
| 64 | + // URL escapes, such as "<http://example.com>" should be skipped |
| 65 | + return Optional.empty(); |
| 66 | + } |
| 67 | + // Not interested in other schemes, also to filter out matches without scheme. |
| 68 | + // It detects a lot of such false-positives in Java snippets |
| 69 | + if (filter.contains(LinkFilter.NON_HTTP_SCHEME) && !raw.startsWith("http")) { |
| 70 | + return Optional.empty(); |
| 71 | + } |
| 72 | + |
| 73 | + String link = url.getFullUrl(); |
| 74 | + |
| 75 | + if (link.endsWith(",") || link.endsWith(".")) { |
| 76 | + // Remove trailing punctuation |
| 77 | + link = link.substring(0, link.length() - 1); |
| 78 | + } |
| 79 | + |
| 80 | + return Optional.of(link); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments