refactored mail observer
This commit is contained in:
parent
f5ee330a13
commit
72b9c14ebc
|
@ -27,25 +27,31 @@ class MailerObserver < ActiveRecord::Observer
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_note(note)
|
def new_note(note)
|
||||||
# Notify whole team except author of note
|
|
||||||
if note.notify
|
if note.notify
|
||||||
note.project.users.reject { |u| u.id == current_user.id } .each do |u|
|
# Notify whole team except author of note
|
||||||
case note.noteable_type
|
notify_note(note)
|
||||||
when "Commit" then
|
|
||||||
Notify.note_commit_email(u.id, note.id).deliver
|
|
||||||
when "Issue" then
|
|
||||||
Notify.note_issue_email(u.id, note.id).deliver
|
|
||||||
when "MergeRequest" then
|
|
||||||
Notify.note_merge_request_email(u.id, note.id).deliver
|
|
||||||
when "Snippet"
|
|
||||||
true
|
|
||||||
else
|
|
||||||
Notify.note_wall_email(u.id, note.id).deliver
|
|
||||||
end
|
|
||||||
end
|
|
||||||
# Notify only author of resource
|
|
||||||
elsif note.notify_author
|
elsif note.notify_author
|
||||||
|
# Notify only author of resource
|
||||||
Notify.note_commit_email(note.commit_author.id, note.id).deliver
|
Notify.note_commit_email(note.commit_author.id, note.id).deliver
|
||||||
|
else
|
||||||
|
# Otherwise ignore it
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def notify_note note
|
||||||
|
# reject author of note from mail list
|
||||||
|
users = note.project.users.reject { |u| u.id == current_user.id }
|
||||||
|
|
||||||
|
users.each do |u|
|
||||||
|
case note.noteable_type
|
||||||
|
when "Commit"; Notify.note_commit_email(u.id, note.id).deliver
|
||||||
|
when "Issue"; Notify.note_issue_email(u.id, note.id).deliver
|
||||||
|
when "MergeRequest"; Notify.note_merge_request_email(u.id, note.id).deliver
|
||||||
|
when "Snippet"; true
|
||||||
|
else
|
||||||
|
Notify.note_wall_email(u.id, note.id).deliver
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -56,37 +62,34 @@ class MailerObserver < ActiveRecord::Observer
|
||||||
end
|
end
|
||||||
|
|
||||||
def changed_merge_request(merge_request)
|
def changed_merge_request(merge_request)
|
||||||
if merge_request.assignee_id_changed?
|
status_notify_and_comment issue, :reassigned_merge_request_email
|
||||||
recipients_ids = merge_request.assignee_id_was, merge_request.assignee_id
|
|
||||||
recipients_ids.delete current_user.id
|
|
||||||
|
|
||||||
User.find(recipients_ids).each do |user|
|
|
||||||
Notify.reassigned_merge_request_email(user.id, merge_request.id, merge_request.assignee_id_was).deliver
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if merge_request.closed_changed?
|
|
||||||
note = Note.new(:noteable => merge_request, :project => merge_request.project)
|
|
||||||
note.author = current_user
|
|
||||||
note.note = "_Status changed to #{merge_request.closed ? 'closed' : 'reopened'}_"
|
|
||||||
note.save()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def changed_issue(issue)
|
def changed_issue(issue)
|
||||||
if issue.assignee_id_changed?
|
status_notify_and_comment issue, :reassigned_issue_email
|
||||||
recipients_ids = issue.assignee_id_was, issue.assignee_id
|
end
|
||||||
|
|
||||||
|
# This method used for Issues & Merge Requests
|
||||||
|
#
|
||||||
|
# It create a comment for Issue or MR if someone close/reopen.
|
||||||
|
# It also notify via email if assignee was changed
|
||||||
|
#
|
||||||
|
def status_notify_and_comment target, mail_method
|
||||||
|
# If assigne changed - notify to recipients
|
||||||
|
if target.assignee_id_changed?
|
||||||
|
recipients_ids = target.assignee_id_was, target.assignee_id
|
||||||
recipients_ids.delete current_user.id
|
recipients_ids.delete current_user.id
|
||||||
|
|
||||||
recipients_ids.each do |recipient_id|
|
recipients_ids.each do |recipient_id|
|
||||||
Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver
|
Notify.send(mail_method, recipient_id, target.id, target.assignee_id_was).deliver
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if issue.closed_changed?
|
# Create comment about status changed
|
||||||
note = Note.new(:noteable => issue, :project => issue.project)
|
if target.closed_changed?
|
||||||
|
note = Note.new(:noteable => target, :project => target.project)
|
||||||
note.author = current_user
|
note.author = current_user
|
||||||
note.note = "_Status changed to #{issue.closed ? 'closed' : 'reopened'}_"
|
note.note = "_Status changed to #{target.closed ? 'closed' : 'reopened'}_"
|
||||||
note.save()
|
note.save()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue