[Notice] Announcing the End of Demo Server [Read me]
Changsung Kim 2015-02-05
Adds a method to get new mentioned users when issues are edited.
* Issue

When issues are edited, all mentioned users in issue contents receives notification emails. It is not what Yobi want. The notifications have to be sent to only new mentioned users.

* Solution

A method to get new mentioned users is added. It gets mentioned users from old body and new body, subtracts old from new and returns it.

Private-issue: 1023
@1e3d867a7b5d577643f83108254bbf41b85ad60d
app/models/NotificationEvent.java
--- app/models/NotificationEvent.java
+++ app/models/NotificationEvent.java
@@ -657,7 +657,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;
@@ -665,6 +665,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) {
@@ -998,6 +1005,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<>();
@@ -1011,6 +1024,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);
Add a comment
List