3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.issue.notification;
22 import com.google.common.collect.Multimap;
23 import java.util.Collection;
24 import java.util.Objects;
25 import javax.annotation.Nullable;
26 import org.sonar.api.notifications.Notification;
27 import org.sonar.api.notifications.NotificationChannel;
28 import org.sonar.server.notification.NotificationDispatcher;
29 import org.sonar.server.notification.NotificationDispatcherMetadata;
30 import org.sonar.server.notification.NotificationManager;
32 import static org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER;
35 * This dispatcher means: "notify me when a change is done on an issue that is assigned to me or reported by me".
37 * @since 3.6, but the feature exists since 2.10 ("review-changed" notification)
39 public class ChangesOnMyIssueNotificationDispatcher extends NotificationDispatcher {
41 public static final String KEY = "ChangesOnMyIssue";
42 private NotificationManager notificationManager;
44 public ChangesOnMyIssueNotificationDispatcher(NotificationManager notificationManager) {
45 super(IssueChangeNotification.TYPE);
46 this.notificationManager = notificationManager;
50 public String getKey() {
54 public static NotificationDispatcherMetadata newMetadata() {
55 return NotificationDispatcherMetadata.create(KEY)
56 .setProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION, String.valueOf(true))
57 .setProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION, String.valueOf(true));
61 public void dispatch(Notification notification, Context context) {
62 String projectUuid = notification.getFieldValue("projectUuid");
63 Multimap<String, NotificationChannel> subscribedRecipients = notificationManager.findSubscribedRecipientsForDispatcher(
64 this, projectUuid, ALL_MUST_HAVE_ROLE_USER);
66 // See available fields in the class IssueNotifications.
68 // All the following users can be null
69 String changeAuthor = notification.getFieldValue("changeAuthor");
70 String assignee = notification.getFieldValue("assignee");
72 if (!Objects.equals(changeAuthor, assignee)) {
73 addUserToContextIfSubscribed(context, assignee, subscribedRecipients);
77 private static void addUserToContextIfSubscribed(Context context, @Nullable String user, Multimap<String, NotificationChannel> subscribedRecipients) {
79 Collection<NotificationChannel> channels = subscribedRecipients.get(user);
80 for (NotificationChannel channel : channels) {
81 context.addUser(user, channel);