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 ++++++++++++++----- juick-common/src/test/java/com/juick/MessageTest.java | 2 ++ 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'juick-common') 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()); + } } 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 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)); } } -- cgit v1.2.3