aboutsummaryrefslogtreecommitdiff
path: root/juick-common
diff options
context:
space:
mode:
Diffstat (limited to 'juick-common')
-rw-r--r--juick-common/src/main/java/com/juick/service/SubscriptionService.java2
-rw-r--r--juick-common/src/main/java/com/juick/util/MessageUtils.java13
-rw-r--r--juick-common/src/test/java/com/juick/MessageTest.java10
3 files changed, 23 insertions, 2 deletions
diff --git a/juick-common/src/main/java/com/juick/service/SubscriptionService.java b/juick-common/src/main/java/com/juick/service/SubscriptionService.java
index 8132ec10..98ea59e0 100644
--- a/juick-common/src/main/java/com/juick/service/SubscriptionService.java
+++ b/juick-common/src/main/java/com/juick/service/SubscriptionService.java
@@ -30,7 +30,7 @@ import java.util.List;
public interface SubscriptionService {
List<String> getJIDSubscribedToUser(int uid, boolean friendsonly);
- List<User> getSubscribedUsers(int uid, int mid);
+ List<User> getSubscribedUsers(int uid, Message msg);
List<User> getUsersSubscribedToComments(Message msg, Message reply);
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 bac24507..4de68cb9 100644
--- a/juick-common/src/main/java/com/juick/util/MessageUtils.java
+++ b/juick-common/src/main/java/com/juick/util/MessageUtils.java
@@ -68,6 +68,9 @@ public class MessageUtils {
private final static String replyNumberRegex = "((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
+ private final static String usernameRegex = "((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))";
+ private final static Pattern usernamePattern = Pattern.compile(usernameRegex);
+
public static String formatMessageCode(String msg) {
msg = msg.replaceAll("&", "&amp;");
msg = msg.replaceAll("<", "&lt;");
@@ -139,7 +142,7 @@ public class MessageUtils {
// @username
// <a href="http://juick.com/username/">@username</a>
- msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
+ msg = msg.replaceAll(usernameRegex, "$1<a href=\"https://juick.com/$2/\">@$2</a>$3");
// (http://juick.com/last?page=2)
// (<a href="http://juick.com/last?page=2" rel="nofollow">juick.com</a>)
@@ -301,4 +304,12 @@ public class MessageUtils {
}
return input;
}
+ public static List<String> getMentions(Message msg) {
+ Matcher usernameMatcher = usernamePattern.matcher(msg.getText());
+ List<String> result = new ArrayList<>();
+ while (usernameMatcher.find()) {
+ result.add(usernameMatcher.group());
+ }
+ return result;
+ }
}
diff --git a/juick-common/src/test/java/com/juick/MessageTest.java b/juick-common/src/test/java/com/juick/MessageTest.java
index aaa66af2..d9c83b84 100644
--- a/juick-common/src/test/java/com/juick/MessageTest.java
+++ b/juick-common/src/test/java/com/juick/MessageTest.java
@@ -22,6 +22,8 @@ import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
+import java.util.List;
+
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
@@ -168,4 +170,12 @@ public class MessageTest {
String msg = "[te](http://juick.com/)[st](http://juick.com/)";
assertThat(MessageUtils.stripNonSafeUrls(msg), is(msg));
}
+ @Test
+ public void mentionsCount() {
+ Message msg = new Message();
+ msg.setText("@ugnich go home");
+ List<String> mentions = MessageUtils.getMentions(msg);
+ assertThat(mentions.size(), is(1));
+ assertThat(mentions.get(0), is("@ugnich"));
+ }
}