aboutsummaryrefslogtreecommitdiff
path: root/juick-common
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-11-03 13:14:15 +0300
committerGravatar Vitaly Takmazov2018-11-03 13:14:15 +0300
commit2a64e080a395170875d62d3bebaf4dce3815bb13 (patch)
treee9c34caf2a694899ea67ab13d6bb2e0c20bee20d /juick-common
parent5cf3c20ed4463c6f35d51fc4e10366fe2b850f29 (diff)
ActivityPub: mentions with @user@server.tld
Diffstat (limited to 'juick-common')
-rw-r--r--juick-common/src/main/java/com/juick/util/MessageUtils.java19
-rw-r--r--juick-common/src/test/java/com/juick/MessageTest.java2
2 files changed, 16 insertions, 5 deletions
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("&", "&amp;");
msg = msg.replaceAll("<", "&lt;");
@@ -138,7 +141,7 @@ public class MessageUtils {
// @username@jabber.org
// <a href="http://juick.com/username@jabber.org/">@username@jabber.org</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
+ msg = msg.replaceAll(jidRegex, "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
// @username
// <a href="http://juick.com/username/">@username</a>
@@ -304,12 +307,18 @@ public class MessageUtils {
}
return input;
}
- public static List<String> getMentions(Message msg) {
- Matcher usernameMatcher = usernamePattern.matcher(msg.getText());
+ private static List<String> collectMatches(Pattern pattern, String input) {
+ Matcher matcher = pattern.matcher(input);
List<String> result = new ArrayList<>();
- while (usernameMatcher.find()) {
- result.add(usernameMatcher.group());
+ while (matcher.find()) {
+ result.add(matcher.group());
}
return result;
}
+ public static List<String> getMentions(Message msg) {
+ return collectMatches(usernamePattern, msg.getText());
+ }
+ public static List<String> getGlobalMentions(Message msg) {
+ return collectMatches(jidPattern, msg.getText());
+ }
}
diff --git a/juick-common/src/test/java/com/juick/MessageTest.java b/juick-common/src/test/java/com/juick/MessageTest.java
index d9c83b84..6197f861 100644
--- a/juick-common/src/test/java/com/juick/MessageTest.java
+++ b/juick-common/src/test/java/com/juick/MessageTest.java
@@ -177,5 +177,7 @@ public class MessageTest {
List<String> mentions = MessageUtils.getMentions(msg);
assertThat(mentions.size(), is(1));
assertThat(mentions.get(0), is("@ugnich"));
+ msg.setText("And dick is @ugnich@jabber.zp.ua");
+ assertThat(MessageUtils.getGlobalMentions(msg).size(), is(1));
}
}