2012-10-10 00:25:29 +02:00
|
|
|
class NoteObserver < ActiveRecord::Observer
|
|
|
|
|
|
|
|
def after_create(note)
|
2012-10-13 22:46:27 +02:00
|
|
|
send_notify_mails(note)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def send_notify_mails(note)
|
2012-10-10 00:25:29 +02:00
|
|
|
if note.notify
|
2012-10-13 22:46:27 +02:00
|
|
|
notify_team(note)
|
2012-10-10 00:25:29 +02:00
|
|
|
elsif note.notify_author
|
|
|
|
# Notify only author of resource
|
2013-02-01 08:33:12 +01:00
|
|
|
if note.commit_author
|
|
|
|
Notify.delay.note_commit_email(note.commit_author.id, note.id)
|
|
|
|
end
|
2012-10-10 00:25:29 +02:00
|
|
|
else
|
|
|
|
# Otherwise ignore it
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-13 22:46:27 +02:00
|
|
|
# Notifies the whole team except the author of note
|
|
|
|
def notify_team(note)
|
|
|
|
# Note: wall posts are not "attached" to anything, so fall back to "Wall"
|
2012-12-07 14:55:56 +01:00
|
|
|
noteable_type = note.noteable_type.presence || "Wall"
|
2012-10-13 22:46:27 +02:00
|
|
|
notify_method = "note_#{noteable_type.underscore}_email".to_sym
|
2012-10-11 19:27:58 +02:00
|
|
|
|
|
|
|
if Notify.respond_to? notify_method
|
|
|
|
team_without_note_author(note).map do |u|
|
2013-01-09 06:44:05 +01:00
|
|
|
Notify.delay.send(notify_method, u.id, note.id)
|
2012-10-10 00:25:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def team_without_note_author(note)
|
|
|
|
note.project.users.reject { |u| u.id == note.author.id }
|
|
|
|
end
|
|
|
|
end
|