Commit 6abf58466fc47a7efd86a03c5b0b3878edfbce36

Authored by Dmitriy Zaporozhets
1 parent f93c4dc0

Move new_note email logic to NotificationService

app/observers/note_observer.rb
1 class NoteObserver < ActiveRecord::Observer 1 class NoteObserver < ActiveRecord::Observer
2 def after_create(note) 2 def after_create(note)
3 - send_notify_mails(note) 3 + notification.new_note(note)
4 end 4 end
5 5
6 protected 6 protected
7 7
8 - def send_notify_mails(note)  
9 - if note.notify  
10 - notify_team(note)  
11 - elsif note.notify_author  
12 - # Notify only author of resource  
13 - if note.commit_author  
14 - Notify.delay.note_commit_email(note.commit_author.id, note.id)  
15 - end  
16 - else  
17 - # Otherwise ignore it  
18 - nil  
19 - end  
20 - end  
21 -  
22 - # Notifies the whole team except the author of note  
23 - def notify_team(note)  
24 - # Note: wall posts are not "attached" to anything, so fall back to "Wall"  
25 - noteable_type = note.noteable_type.presence || "Wall"  
26 - notify_method = "note_#{noteable_type.underscore}_email".to_sym  
27 -  
28 - if Notify.respond_to? notify_method  
29 - team_without_note_author(note).map do |u|  
30 - Notify.delay.send(notify_method, u.id, note.id)  
31 - end  
32 - end  
33 - end  
34 -  
35 - def team_without_note_author(note)  
36 - note.project.users.reject { |u| u.id == note.author.id }  
37 - end  
38 -  
39 def notification 8 def notification
40 NotificationService.new 9 NotificationService.new
41 end 10 end
app/observers/user_observer.rb
@@ -2,8 +2,7 @@ class UserObserver &lt; ActiveRecord::Observer @@ -2,8 +2,7 @@ class UserObserver &lt; ActiveRecord::Observer
2 def after_create(user) 2 def after_create(user)
3 log_info("User \"#{user.name}\" (#{user.email}) was created") 3 log_info("User \"#{user.name}\" (#{user.email}) was created")
4 4
5 - # Dont email omniauth created users  
6 - Notify.delay.new_user_email(user.id, user.password) unless user.extern_uid? 5 + notification.new_user(user)
7 end 6 end
8 7
9 def after_destroy user 8 def after_destroy user
@@ -25,4 +24,8 @@ class UserObserver &lt; ActiveRecord::Observer @@ -25,4 +24,8 @@ class UserObserver &lt; ActiveRecord::Observer
25 def log_info message 24 def log_info message
26 Gitlab::AppLogger.info message 25 Gitlab::AppLogger.info message
27 end 26 end
  27 +
  28 + def notification
  29 + NotificationService.new
  30 + end
28 end 31 end
app/services/notification_service.rb
@@ -80,4 +80,32 @@ class NotificationService @@ -80,4 +80,32 @@ class NotificationService
80 Notify.delay.reassigned_merge_request_email(recipient_id, merge_request.id, merge_request.assignee_id_was) 80 Notify.delay.reassigned_merge_request_email(recipient_id, merge_request.id, merge_request.assignee_id_was)
81 end 81 end
82 end 82 end
  83 +
  84 + # Notify new user with email after creation
  85 + def new_user(user)
  86 + # Dont email omniauth created users
  87 + Notify.delay.new_user_email(user.id, user.password) unless user.extern_uid?
  88 + end
  89 +
  90 + # Notify users on new note in system
  91 + #
  92 + # TODO: split on methods and refactor
  93 + #
  94 + def new_note(note)
  95 + if note.notify
  96 + users = note.project.users.reject { |u| u.id == note.author.id }
  97 +
  98 + # Note: wall posts are not "attached" to anything, so fall back to "Wall"
  99 + noteable_type = note.noteable_type.presence || "Wall"
  100 + notify_method = "note_#{noteable_type.underscore}_email".to_sym
  101 +
  102 + if Notify.respond_to? notify_method
  103 + team_without_note_author(note).map do |u|
  104 + Notify.delay.send(notify_method, u.id, note.id)
  105 + end
  106 + end
  107 + elsif note.notify_author && note.commit_author
  108 + Notify.delay.note_commit_email(note.commit_author.id, note.id)
  109 + end
  110 + end
83 end 111 end