From 2a64e080a395170875d62d3bebaf4dce3815bb13 Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Sat, 3 Nov 2018 13:14:15 +0300
Subject: ActivityPub: mentions with @user@server.tld
---
.../src/main/java/com/juick/util/MessageUtils.java | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
(limited to 'juick-common/src/main/java/com')
diff --git a/juick-common/src/main/java/com/juick/util/MessageUtils.java b/juick-common/src/main/java/com/juick/util/MessageUtils.java
index 62616bdf..fd357c32 100644
--- a/juick-common/src/main/java/com/juick/util/MessageUtils.java
+++ b/juick-common/src/main/java/com/juick/util/MessageUtils.java
@@ -71,6 +71,9 @@ public class MessageUtils {
private final static String usernameRegex = "((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
private final static Pattern usernamePattern = Pattern.compile(usernameRegex);
+ private final static String jidRegex = "((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
+ private final static Pattern jidPattern = Pattern.compile(jidRegex);
+
public static String formatMessageCode(String msg) {
msg = msg.replaceAll("&", "&");
msg = msg.replaceAll("<", "<");
@@ -138,7 +141,7 @@ public class MessageUtils {
// @username@jabber.org
// @username@jabber.org
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3");
+ msg = msg.replaceAll(jidRegex, "$1@$2$3");
// @username
// @username
@@ -304,12 +307,18 @@ public class MessageUtils {
}
return input;
}
- public static List getMentions(Message msg) {
- Matcher usernameMatcher = usernamePattern.matcher(msg.getText());
+ private static List collectMatches(Pattern pattern, String input) {
+ Matcher matcher = pattern.matcher(input);
List result = new ArrayList<>();
- while (usernameMatcher.find()) {
- result.add(usernameMatcher.group());
+ while (matcher.find()) {
+ result.add(matcher.group());
}
return result;
}
+ public static List getMentions(Message msg) {
+ return collectMatches(usernamePattern, msg.getText());
+ }
+ public static List getGlobalMentions(Message msg) {
+ return collectMatches(jidPattern, msg.getText());
+ }
}
--
cgit v1.2.3