-
-
Notifications
You must be signed in to change notification settings - Fork 100
ChatGPT Auto-Answer should not be posted when image #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marko-radosavljevic
merged 10 commits into
develop
from
feature/gpt-prevent-auto-answer-on-image
Feb 29, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7214f5
Move link detection into separate class
SquidXTV 0108236
Remove unused method and fix javadocs
SquidXTV dd6d911
Skip chatgpt response if message contains image or link
SquidXTV 91a2cb9
Fix sonarlint issues
SquidXTV d8eea62
Move fixing encoding issues to its own PR
SquidXTV 6329c50
Remove star import
SquidXTV cd21bb3
Move context logic into custom method
SquidXTV fa120c5
Fix reviews
SquidXTV 6cbd128
Fix reviews
SquidXTV f033325
Rename LinkDetections to LinkDetection
SquidXTV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
application/src/main/java/org/togetherjava/tjbot/features/utils/LinkDetection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
SquidXTV marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.