diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-12-29 15:55:21 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-12-29 15:55:21 +0200 |
commit | cf9573686586fafc199c6178b672f9ee617476d2 (patch) | |
tree | 21aaa5601ead31e3cacd8a3ccf7d2eed5a2e19de | |
parent | 6aec286fca169502edd4c643a6d2202a012fa142 (diff) | |
download | gitlab-ce-cf9573686586fafc199c6178b672f9ee617476d2.tar.gz |
New feature: add 'Mention' notification level
It does disable all emails expect system one or when you was @mentioned
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r-- | app/models/notification.rb | 10 | ||||
-rw-r--r-- | app/services/notification_service.rb | 33 | ||||
-rw-r--r-- | app/views/profiles/notifications/show.html.haml | 7 | ||||
-rw-r--r-- | spec/services/notification_service_spec.rb | 16 |
4 files changed, 63 insertions, 3 deletions
diff --git a/app/models/notification.rb b/app/models/notification.rb index b0f8ed6a4ec..1395274173d 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -6,12 +6,13 @@ class Notification N_PARTICIPATING = 1 N_WATCH = 2 N_GLOBAL = 3 + N_MENTION = 4 attr_accessor :target class << self def notification_levels - [N_DISABLED, N_PARTICIPATING, N_WATCH] + [N_DISABLED, N_PARTICIPATING, N_WATCH, N_MENTION] end def options_with_labels @@ -19,12 +20,13 @@ class Notification disabled: N_DISABLED, participating: N_PARTICIPATING, watch: N_WATCH, + mention: N_MENTION, global: N_GLOBAL } end def project_notification_levels - [N_DISABLED, N_PARTICIPATING, N_WATCH, N_GLOBAL] + [N_DISABLED, N_PARTICIPATING, N_WATCH, N_GLOBAL, N_MENTION] end end @@ -48,6 +50,10 @@ class Notification target.notification_level == N_GLOBAL end + def mention? + target.notification_level == N_MENTION + end + def level target.notification_level end diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index d1aadd741e1..fb8f812dad8 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -144,6 +144,10 @@ class NotificationService # Merge project watchers recipients = recipients.concat(project_watchers(note.project)).compact.uniq + # Reject mention users unless mentioned in comment + recipients = reject_mention_users(recipients - note.mentioned_users, note.project) + recipients = recipients + note.mentioned_users + # Reject mutes users recipients = reject_muted_users(recipients, note.project) @@ -285,13 +289,39 @@ class NotificationService end end + # Remove users with notification level 'Mentioned' + def reject_mention_users(users, project = nil) + users = users.to_a.compact.uniq + + users.reject do |user| + next user.notification.mention? unless project + + tm = project.project_members.find_by(user_id: user.id) + + if !tm && project.group + tm = project.group.group_members.find_by(user_id: user.id) + end + + # reject users who globally set mention notification and has no membership + next user.notification.mention? unless tm + + # reject users who set mention notification in project + next true if tm.notification.mention? + + # reject users who have N_MENTION in project and disabled in global settings + tm.notification.global? && user.notification.mention? + end + end + def new_resource_email(target, project, method) if target.respond_to?(:participants) recipients = target.participants else recipients = [] end + recipients = reject_muted_users(recipients, project) + recipients = reject_mention_users(recipients, project) recipients = recipients.concat(project_watchers(project)).uniq recipients.delete(target.author) @@ -302,6 +332,7 @@ class NotificationService def close_resource_email(target, project, current_user, method) recipients = reject_muted_users([target.author, target.assignee], project) + recipients = reject_mention_users(recipients, project) recipients = recipients.concat(project_watchers(project)).uniq recipients.delete(current_user) @@ -320,6 +351,7 @@ class NotificationService # reject users with disabled notifications recipients = reject_muted_users(recipients, project) + recipients = reject_mention_users(recipients, project) # Reject me from recipients if I reassign an item recipients.delete(current_user) @@ -331,6 +363,7 @@ class NotificationService def reopen_resource_email(target, project, current_user, method, status) recipients = reject_muted_users([target.author, target.assignee], project) + recipients = reject_mention_users(recipients, project) recipients = recipients.concat(project_watchers(project)).uniq recipients.delete(current_user) diff --git a/app/views/profiles/notifications/show.html.haml b/app/views/profiles/notifications/show.html.haml index a044fad8fa3..96fe91b9b20 100644 --- a/app/views/profiles/notifications/show.html.haml +++ b/app/views/profiles/notifications/show.html.haml @@ -17,6 +17,13 @@ .radio = label_tag nil, class: '' do + = radio_button_tag :notification_level, Notification::N_MENTION, @notification.mention?, class: 'trigger-submit' + .level-title + Mention + %p You will receive notifications only for comments where you was @mentioned + + .radio + = label_tag nil, class: '' do = radio_button_tag :notification_level, Notification::N_PARTICIPATING, @notification.participating?, class: 'trigger-submit' .level-title Participating diff --git a/spec/services/notification_service_spec.rb b/spec/services/notification_service_spec.rb index f8377650e0a..e305536f7ee 100644 --- a/spec/services/notification_service_spec.rb +++ b/spec/services/notification_service_spec.rb @@ -116,6 +116,7 @@ describe NotificationService do should_email(note.noteable.assignee_id) should_not_email(note.author_id) + should_not_email(@u_mentioned.id) should_not_email(@u_disabled.id) should_not_email(@u_not_mentioned.id) notification.new_note(note) @@ -168,6 +169,12 @@ describe NotificationService do notification.new_note(note) end + it do + @u_committer.update_attributes(notification_level: Notification::N_MENTION) + should_not_email(@u_committer.id, note) + notification.new_note(note) + end + def should_email(user_id, n) Notify.should_receive(:note_commit_email).with(user_id, n.id) end @@ -190,11 +197,18 @@ describe NotificationService do it do should_email(issue.assignee_id) should_email(@u_watcher.id) + should_not_email(@u_mentioned.id) should_not_email(@u_participating.id) should_not_email(@u_disabled.id) notification.new_issue(issue, @u_disabled) end + it do + issue.assignee.update_attributes(notification_level: Notification::N_MENTION) + should_not_email(issue.assignee_id) + notification.new_issue(issue, @u_disabled) + end + def should_email(user_id) Notify.should_receive(:new_issue_email).with(user_id, issue.id) end @@ -391,7 +405,7 @@ describe NotificationService do @u_watcher = create(:user, notification_level: Notification::N_WATCH) @u_participating = create(:user, notification_level: Notification::N_PARTICIPATING) @u_disabled = create(:user, notification_level: Notification::N_DISABLED) - @u_mentioned = create(:user, username: 'mention', notification_level: Notification::N_PARTICIPATING) + @u_mentioned = create(:user, username: 'mention', notification_level: Notification::N_MENTION) @u_committer = create(:user, username: 'committer') @u_not_mentioned = create(:user, username: 'regular', notification_level: Notification::N_PARTICIPATING) |