mailer_observer.rb 2.73 KB
Newer Older
Valeriy's avatar
Valeriy committed
1
class MailerObserver < ActiveRecord::Observer
2
  observe :issue, :user, :note, :merge_request
Valeriy's avatar
Valeriy committed
3 4 5 6 7 8
  cattr_accessor :current_user

  def after_create(model)
    new_issue(model) if model.kind_of?(Issue)
    new_user(model) if model.kind_of?(User)
    new_note(model) if model.kind_of?(Note)
9
    new_merge_request(model) if model.kind_of?(MergeRequest)
Valeriy's avatar
Valeriy committed
10 11
  end

12 13
  def after_update(model)
    changed_merge_request(model) if model.kind_of?(MergeRequest)
14
    changed_issue(model) if model.kind_of?(Issue)
15 16
  end

Valeriy's avatar
Valeriy committed
17 18
  protected

19 20 21
  def new_issue(issue)
    if issue.assignee != current_user
      Notify.new_issue_email(issue).deliver
Valeriy's avatar
Valeriy committed
22
    end
23
  end
Valeriy's avatar
Valeriy committed
24

25
  def new_user(user)
26
    Notify.new_user_email(user.id, user.password).deliver
27
  end
Valeriy's avatar
Valeriy committed
28

29
  def new_note(note)
30 31 32 33 34 35 36 37 38
    # Notify whole team except author of note
    if note.notify
      note.project.users.reject { |u| u.id == current_user.id } .each do |u|
        case note.noteable_type
        when "Commit" then
          Notify.note_commit_email(u, note).deliver
        when "Issue" then
          Notify.note_issue_email(u, note).deliver
        when "MergeRequest" then
39
          Notify.note_merge_request_email(u.id, note.id).deliver
40 41 42 43 44
        when "Snippet"
          true
        else
          Notify.note_wall_email(u, note).deliver
        end
Valeriy's avatar
Valeriy committed
45
      end
46 47 48
    # Notify only author of resource
    elsif note.notify_author
      Notify.note_commit_email(note.commit_author, note).deliver
Valeriy's avatar
Valeriy committed
49
    end
50
  end
Valeriy's avatar
Valeriy committed
51

52 53 54
  def new_merge_request(merge_request)
    if merge_request.assignee != current_user
      Notify.new_merge_request_email(merge_request).deliver
55
    end
56
  end
57

58 59 60 61
  def changed_merge_request(merge_request)
    if merge_request.assignee_id_changed?
      recipients_ids = merge_request.assignee_id_was, merge_request.assignee_id
      recipients_ids.delete current_user.id
62

63 64
      User.find(recipients_ids).each do |user|
        Notify.changed_merge_request_email(user, merge_request).deliver
65 66 67
      end
    end

68 69 70 71 72 73 74
    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
75

76 77 78 79
  def changed_issue(issue)
    if issue.assignee_id_changed?
      recipients_ids = issue.assignee_id_was, issue.assignee_id
      recipients_ids.delete current_user.id
80

81 82
      recipients_ids.each do |recipient_id|
        Notify.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was).deliver
83
      end
84 85 86 87 88 89 90 91
    end

    if issue.closed_changed?
      note = Note.new(:noteable => issue, :project => issue.project)
      note.author = current_user
      note.note = "_Status changed to #{issue.closed ? 'closed' : 'reopened'}_"
      note.save()
    end
92
  end
Valeriy's avatar
Valeriy committed
93
end