Commit f8b54295 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'honor_notification_level' into 'master'

Honor notification levels

Fixes #1093
parents a37f8fc6 109ab1a5
...@@ -178,29 +178,29 @@ class NotificationService ...@@ -178,29 +178,29 @@ class NotificationService
# Get project users with WATCH notification level # Get project users with WATCH notification level
def project_watchers(project) def project_watchers(project)
# Gather all user ids that have WATCH notification setting for project project_members = users_project_notification(project)
project_notification_uids = project_notification_list(project, Notification::N_WATCH)
# Gather all user ids that have WATCH notification setting for group users_with_project_level_global = users_project_notification(project, Notification::N_GLOBAL)
group_notification_uids = group_notification_list(project, Notification::N_WATCH) users_with_group_level_global = users_group_notification(project, Notification::N_GLOBAL)
users = users_with_global_level_watch([users_with_project_level_global, users_with_group_level_global].flatten.uniq)
# Gather all user ids that have GLOBAL setting users_with_project_setting = select_users_project_setting(project, users_with_project_level_global, users)
global_notification_uids = global_notification_list(project) users_with_group_setting = select_users_group_setting(project, project_members, users_with_group_level_global, users)
project_and_group_uids = [project_notification_uids, group_notification_uids].flatten.uniq User.where(id: users_with_project_setting.concat(users_with_group_setting).uniq).to_a
group_and_project_watchers = User.where(id: project_and_group_uids) end
# Find all users that have WATCH as their GLOBAL setting def users_project_notification(project, notification_level=nil)
global_watchers = User.where(id: global_notification_uids, notification_level: Notification::N_WATCH) project_members = project.users_projects
[group_and_project_watchers, global_watchers].flatten.uniq if notification_level
project_members.where(notification_level: notification_level).pluck(:user_id)
else
project_members.pluck(:user_id)
end end
def project_notification_list(project, notification_level)
project.users_projects.where(notification_level: notification_level).pluck(:user_id)
end end
def group_notification_list(project, notification_level) def users_group_notification(project, notification_level)
if project.group if project.group
project.group.users_groups.where(notification_level: notification_level).pluck(:user_id) project.group.users_groups.where(notification_level: notification_level).pluck(:user_id)
else else
...@@ -208,11 +208,47 @@ class NotificationService ...@@ -208,11 +208,47 @@ class NotificationService
end end
end end
def global_notification_list(project) def users_with_global_level_watch(ids)
[ User.where(
project_notification_list(project, Notification::N_GLOBAL), id: ids,
group_notification_list(project, Notification::N_GLOBAL) notification_level: Notification::N_WATCH
].flatten ).pluck(:id)
end
# Build a list of users based on project notifcation settings
def select_users_project_setting(project, global_setting, users_global_level_watch)
users = users_project_notification(project, Notification::N_WATCH)
# If project setting is global, add to watch list if global setting is watch
global_setting.each do |user_id|
if users_global_level_watch.include?(user_id)
users << user_id
end
end
users
end
# Build a list of users based on group notifcation settings
def select_users_group_setting(project, project_members, global_setting, users_global_level_watch)
uids = users_group_notification(project, Notification::N_WATCH)
# Group setting is watch, add to users list if user is not project member
users = []
uids.each do |user_id|
if project_members.exclude?(user_id)
users << user_id
end
end
# Group setting is global, add to users list if global setting is watch
global_setting.each do |user_id|
if project_members.exclude?(user_id) && users_global_level_watch.include?(user_id)
users << user_id
end
end
users
end end
# Remove users with disabled notifications from array # Remove users with disabled notifications from array
......
...@@ -57,6 +57,34 @@ describe NotificationService do ...@@ -57,6 +57,34 @@ describe NotificationService do
Notify.should_not_receive(:note_issue_email) Notify.should_not_receive(:note_issue_email)
notification.new_note(mentioned_note) notification.new_note(mentioned_note)
end end
end
describe 'new note on issue in project that belongs to a group' do
let(:group) { create(:group) }
before do
note.project.namespace_id = group.id
note.project.group.add_user(@u_watcher, UsersGroup::MASTER)
note.project.save
user_project = note.project.users_projects.find_by_user_id(@u_watcher.id)
user_project.notification_level = Notification::N_PARTICIPATING
user_project.save
user_group = note.project.group.users_groups.find_by_user_id(@u_watcher.id)
user_group.notification_level = Notification::N_GLOBAL
user_group.save
end
it do
should_email(note.noteable.author_id)
should_email(note.noteable.assignee_id)
should_email(@u_mentioned.id)
should_not_email(@u_watcher.id)
should_not_email(note.author_id)
should_not_email(@u_participating.id)
should_not_email(@u_disabled.id)
notification.new_note(note)
end
end
def should_email(user_id) def should_email(user_id)
Notify.should_receive(:note_issue_email).with(user_id, note.id) Notify.should_receive(:note_issue_email).with(user_id, note.id)
...@@ -66,7 +94,6 @@ describe NotificationService do ...@@ -66,7 +94,6 @@ describe NotificationService do
Notify.should_not_receive(:note_issue_email).with(user_id, note.id) Notify.should_not_receive(:note_issue_email).with(user_id, note.id)
end end
end end
end
context 'commit note' do context 'commit note' do
let(:note) { create(:note_on_commit) } let(:note) { create(:note_on_commit) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment