Commit 618249734b1bcb8886b9d96714d278119037261c

Authored by Dmitriy Zaporozhets
1 parent 63e6f055

rebuild notification on notes logic

app/services/notification_service.rb
... ... @@ -99,22 +99,34 @@ class NotificationService
99 99 # TODO: split on methods and refactor
100 100 #
101 101 def new_note(note)
102   - if note.notify
103   - users = note.project.users
104   - users = reject_muted_users(users)
105   - users.delete(note.author)
106   -
107   - # Note: wall posts are not "attached" to anything, so fall back to "Wall"
108   - noteable_type = note.noteable_type.presence || "Wall"
109   - notify_method = "note_#{noteable_type.underscore}_email".to_sym
110   -
111   - if Notify.respond_to? notify_method
112   - users.each do |user|
113   - Notify.delay.send(notify_method, user.id, note.id)
114   - end
115   - end
116   - elsif note.notify_author && note.commit_author
117   - Notify.delay.note_commit_email(note.commit_author.id, note.id)
  102 + # ignore wall messages
  103 + return true unless note.noteable_type.present?
  104 +
  105 + opts = { noteable_type: note.noteable_type, project_id: note.project_id }
  106 +
  107 + if note.commit_id
  108 + opts.merge!(commit_id: note.commit_id)
  109 + else
  110 + opts.merge!(noteable_id: note.noteable_id)
  111 + end
  112 +
  113 + # Get users who left comment in thread
  114 + recipients = User.where(id: Note.where(opts).pluck(:author_id))
  115 +
  116 + # Merge project watchers
  117 + recipients = recipients.concat(project_watchers(note.project)).compact.uniq
  118 +
  119 + # Reject mutes users
  120 + recipients = reject_muted_users(recipients)
  121 +
  122 + # Reject author
  123 + recipients.delete(note.author)
  124 +
  125 + # build notify method like 'note_commit_email'
  126 + notify_method = "note_#{note.noteable_type.underscore}_email".to_sym
  127 +
  128 + recipients.each do |recipient|
  129 + Notify.delay.send(notify_method, recipient.id, note.id)
118 130 end
119 131 end
120 132  
... ...
spec/services/notification_service_spec.rb
... ... @@ -20,6 +20,45 @@ describe NotificationService do
20 20 end
21 21 end
22 22  
  23 + describe 'Notes' do
  24 + let(:note) { create :note_on_commit }
  25 +
  26 + before do
  27 + build_team(note.project)
  28 + end
  29 +
  30 + describe :new_note do
  31 + it do
  32 + should_email(@u_watcher.id)
  33 + should_not_email(note.author_id)
  34 + should_not_email(@u_participating.id)
  35 + should_not_email(@u_disabled.id)
  36 + notification.new_note(note)
  37 + end
  38 +
  39 + it do
  40 + create(:note_on_commit,
  41 + author: @u_participating,
  42 + project_id: note.project_id,
  43 + commit_id: note.commit_id)
  44 +
  45 + should_email(@u_watcher.id)
  46 + should_email(@u_participating.id)
  47 + should_not_email(note.author_id)
  48 + should_not_email(@u_disabled.id)
  49 + notification.new_note(note)
  50 + end
  51 +
  52 + def should_email(user_id)
  53 + Notify.should_receive(:note_commit_email).with(user_id, note.id)
  54 + end
  55 +
  56 + def should_not_email(user_id)
  57 + Notify.should_not_receive(:note_commit_email).with(user_id, note.id)
  58 + end
  59 + end
  60 + end
  61 +
23 62 describe 'Issues' do
24 63 let(:issue) { create :issue, assignee: create(:user) }
25 64  
... ...