Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.togetherjava.tjbot.features.UserInteractor;
import org.togetherjava.tjbot.features.componentids.ComponentIdGenerator;
import org.togetherjava.tjbot.features.componentids.ComponentIdInteractor;
import org.togetherjava.tjbot.features.utils.LinkDetection;
import org.togetherjava.tjbot.features.utils.MessageUtils;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -113,8 +115,14 @@ private static boolean isPostedBySelfUser(Message message) {
private RestAction<Message> createAIResponse(ThreadChannel threadChannel) {
RestAction<Message> originalQuestion =
threadChannel.retrieveMessageById(threadChannel.getIdLong());
return originalQuestion.flatMap(message -> helper.constructChatGptAttempt(threadChannel,
getMessageContent(message), componentIdInteractor));
return originalQuestion.flatMap(HelpThreadCreatedListener::isContextSufficient,
message -> helper.constructChatGptAttempt(threadChannel, getMessageContent(message),
componentIdInteractor));
}

private static boolean isContextSufficient(Message message) {
return !MessageUtils.containsImage(message)
&& !LinkDetection.containsLink(message.getContentRaw());
}

private RestAction<Void> pinOriginalQuestion(ThreadChannel threadChannel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.togetherjava.tjbot.features.CommandVisibility;
import org.togetherjava.tjbot.features.SlashCommandAdapter;
import org.togetherjava.tjbot.features.utils.LinkDetection;
import org.togetherjava.tjbot.features.utils.LinkPreview;
import org.togetherjava.tjbot.features.utils.LinkPreviews;
import org.togetherjava.tjbot.features.utils.StringDistances;
Expand Down Expand Up @@ -93,7 +94,10 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
.map(OptionMapping::getAsUser)
.map(User::getAsMention);

List<String> links = LinkPreviews.extractLinks(tagContent)
List<String> links = LinkDetection
.extractLinks(tagContent,
Set.of(LinkDetection.LinkFilter.SUPPRESSED,
LinkDetection.LinkFilter.NON_HTTP_SCHEME))
.stream()
.limit(Message.MAX_EMBED_COUNT - 1L)
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.togetherjava.tjbot.features.utils;

import com.linkedin.urls.Url;
import com.linkedin.urls.detection.UrlDetector;
import com.linkedin.urls.detection.UrlDetectorOptions;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
* Utility class to detect links.
*/
public class LinkDetection {

/**
* Possible ways to filter a link.
*
* @see LinkDetection
*/
public enum LinkFilter {
/**
* Filters links suppressed with {@literal <url>}.
*/
SUPPRESSED,
/**
* Filters links that are not using http scheme.
*/
NON_HTTP_SCHEME
}

private LinkDetection() {
throw new UnsupportedOperationException("Utility class");
}

/**
* Extracts all links from the given content.
*
* @param content the content to search through
* @param filter the filters applied to the urls
* @return a list of all found links, can be empty
*/
public static List<String> extractLinks(String content, Set<LinkFilter> filter) {
return new UrlDetector(content, UrlDetectorOptions.BRACKET_MATCH).detect()
.stream()
.map(url -> toLink(url, filter))
.flatMap(Optional::stream)
.toList();
}

/**
* Checks whether the given content contains a link.
*
* @param content the content to search through
* @return true if the content contains at least one link
*/
public static boolean containsLink(String content) {
return !(new UrlDetector(content, UrlDetectorOptions.BRACKET_MATCH).detect().isEmpty());
}

private static Optional<String> toLink(Url url, Set<LinkFilter> filter) {
String raw = url.getOriginalUrl();
if (filter.contains(LinkFilter.SUPPRESSED) && raw.contains(">")) {
// URL escapes, such as "<http://example.com>" should be skipped
return Optional.empty();
}
// Not interested in other schemes, also to filter out matches without scheme.
// It detects a lot of such false-positives in Java snippets
if (filter.contains(LinkFilter.NON_HTTP_SCHEME) && !raw.startsWith("http")) {
return Optional.empty();
}

String link = url.getFullUrl();

if (link.endsWith(",") || link.endsWith(".")) {
// Remove trailing punctuation
link = link.substring(0, link.length() - 1);
}

return Optional.of(link);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.togetherjava.tjbot.features.utils;

import com.linkedin.urls.Url;
import com.linkedin.urls.detection.UrlDetector;
import com.linkedin.urls.detection.UrlDetectorOptions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
Expand All @@ -27,7 +24,8 @@
import java.util.stream.IntStream;

/**
* Provides means to create previews of links. See {@link #extractLinks(String)} and
* Provides means to create previews of links. See
* {@link LinkDetection#extractLinks(String, boolean, boolean)} and
* {@link #createLinkPreviews(List)}.
*/
public final class LinkPreviews {
Expand All @@ -43,42 +41,6 @@ private LinkPreviews() {
throw new UnsupportedOperationException("Utility class");
}

/**
* Extracts all links from the given content.
*
* @param content the content to search through
* @return a list of all found links, can be empty
*/
public static List<String> extractLinks(String content) {
return new UrlDetector(content, UrlDetectorOptions.BRACKET_MATCH).detect()
.stream()
.map(LinkPreviews::toLink)
.flatMap(Optional::stream)
.toList();
}

private static Optional<String> toLink(Url url) {
String raw = url.getOriginalUrl();
if (raw.contains(">")) {
// URL escapes, such as "<http://example.com>" should be skipped
return Optional.empty();
}
// Not interested in other schemes, also to filter out matches without scheme.
// It detects a lot of such false-positives in Java snippets
if (!raw.startsWith("http")) {
return Optional.empty();
}

String link = url.getFullUrl();

if (link.endsWith(",") || link.endsWith(".")) {
// Remove trailing punctuation
link = link.substring(0, link.length() - 1);
}

return Optional.of(link);
}

/**
* Attempts to create previews of all given links.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,18 @@ public static Optional<CodeFence> extractCode(String fullMessage) {

return Optional.of(new CodeFence(language, code));
}

/**
* Checks if a given message contains any image attachments.
*
* @param message the message to be checked
* @return {@code true} if the message contains at least one image attachment
*
* @see Message
* @see Message.Attachment
*/
public static boolean containsImage(Message message) {
return message.getAttachments().stream().anyMatch(Message.Attachment::isImage);
}

}
8 changes: 0 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,4 @@ subprojects {
test {
useJUnitPlatform()
}

compileJava {
options.encoding = "UTF-8"
}

compileTestJava {
options.encoding = "UTF-8"
}
}