[Notice] Announcing the End of Demo Server [Read me]
채수원 2015-03-12
Merge branch 'issue-1023' into 'next'
from pull-request 1433

* refs/heads/issue-1023:
  adds tests for models.NotificationEvent.getNewMentionedUsers method.
  Adds a method to get new mentioned users when issues are edited.

Reviewed-by: 채수원 
@3a461f859e070b0c9cfdbf1f9a0b9c9e4e6d789b
app/models/NotificationEvent.java
--- app/models/NotificationEvent.java
+++ app/models/NotificationEvent.java
@@ -658,7 +658,7 @@
     public static NotificationEvent afterIssueBodyChanged(String oldBody, Issue issue) {
         NotificationEvent notiEvent = createFromCurrentUser(issue);
         notiEvent.title = formatReplyTitle(issue);
-        notiEvent.receivers = getReceivers(issue);
+        notiEvent.receivers = getReceiversForIssueBodyChanged(oldBody, issue);
         notiEvent.eventType = EventType.ISSUE_BODY_CHANGED;
         notiEvent.oldValue = oldBody;
         notiEvent.newValue = issue.body;
@@ -666,6 +666,13 @@
         NotificationEvent.add(notiEvent);
 
         return notiEvent;
+    }
+
+    private static Set<User> getReceiversForIssueBodyChanged(String oldBody, Issue issue) {
+        Set<User> receivers = issue.getWatchers();
+        receivers.addAll(getNewMentionedUsers(oldBody, issue.body));
+        receivers.remove(UserApp.currentUser());
+        return receivers;
     }
 
     public static void afterNewPost(Posting post) {
@@ -999,6 +1006,12 @@
         return Messages.get("notification.member.request.accept.title", organization.name, user.loginId);
     }
 
+    /**
+     * Get mentioned users in {@code body}.
+     *
+     * @param body
+     * @return
+     */
     public static Set<User> getMentionedUsers(String body) {
         Matcher matcher = Pattern.compile("@" + User.LOGIN_ID_PATTERN_ALLOW_FORWARD_SLASH).matcher(body);
         Set<User> users = new HashSet<>();
@@ -1012,6 +1025,25 @@
         return users;
     }
 
+    /**
+     * Get new mentioned users.
+     *
+     * It gets mentioned users from {@code oldBody} and {@code newBody},
+     * subtracts old from new and returns it.
+     *
+     * @param oldBody
+     * @param newBody
+     * @return
+     */
+    public static Set<User> getNewMentionedUsers(String oldBody, String newBody) {
+        Set<User> oldBodyMentionedUsers = getMentionedUsers(oldBody);
+        Set<User> newBodyMentionedUsers = getMentionedUsers(newBody);
+
+        newBodyMentionedUsers.removeAll(oldBodyMentionedUsers);
+
+        return newBodyMentionedUsers;
+    }
+
     private static Set<User> findOrganizationMembers(String mentionWord) {
         Set<User> users = new HashSet<>();
         Organization org = Organization.findByName(mentionWord);
test/models/NotificationEventTest.java
--- test/models/NotificationEventTest.java
+++ test/models/NotificationEventTest.java
@@ -26,6 +26,7 @@
 
 
 import java.util.HashSet;
+import java.util.Set;
 
 import static org.fest.assertions.Assertions.assertThat;
 
@@ -104,4 +105,37 @@
         // Then
         assertThat(event.receivers).containsOnly(off);
     }
+
+    @Test
+    public void getNewMentionedUsers1() {
+        // Given
+        String loginId = "doortts";
+        String oldBody = "I'm @yobi";
+        String newBody = "I'm @" + loginId;
+
+        // When
+        Set<User> newMentionedUsers = NotificationEvent.getNewMentionedUsers(oldBody, newBody);
+
+        // Then
+        User newMentionedUser = User.findByLoginId(loginId);
+        assertThat(newMentionedUsers.size() == 1).isTrue();
+        assertThat(newMentionedUsers.contains(newMentionedUser)).isTrue();
+    }
+
+    @Test
+    public void getNewMentionedUsers2() {
+        // Given
+        String loginId = "laziel";
+        String oldBody = "They are @yobi and @doortts";
+        String newBody = "They are @" + loginId + " and @unknownUserLoginId";
+
+        // When
+        Set<User> newMentionedUsers = NotificationEvent.getNewMentionedUsers(oldBody, newBody);
+
+        // Then
+        User newMentionedUser = User.findByLoginId(loginId);
+        assertThat(newMentionedUsers.size() == 1).isTrue();
+        assertThat(newMentionedUsers.contains(newMentionedUser)).isTrue();
+    }
+
 }
Add a comment
List