summaryrefslogtreecommitdiff
path: root/engines/support/app/mailers/ticket_mailer.rb
blob: 9a83345a4244f35daa8c91a89ca4766968068d73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class TicketMailer < ActionMailer::Base

  helper :ticket_i18n

  def self.send_notice(ticket, comment, url)
    reply_recipients(ticket, comment.user).each do |email, key|
      TicketMailer.notice(ticket, comment, url, email, key).deliver
    end
  end

  #
  # ticket - the ticket in question
  # author - user who created last comment
  # url - url of the ticket
  # email - email to send reply notice to
  # key - public key for email
  #
  # TODO: OpenPGP encrypt email to the public key.
  #
  def notice(ticket, comment, url, email, key)
    @url = url
    @email = email
    @key = key
    mail({
      :reply_to  => reply_to_address,
      :subject   => "Re: [#{ticket.id}] #{ticket.subject}",
      :to        => email,
      :from      => from_address(ticket, comment)
    })
  end

  private

  #
  # I am not sure what makes the most sense here. For now, since we do not
  # include any reply text in the notification email, it makes sense to make
  # the from address be the robot, not the user.
  #
  def from_address(ticket, comment)
    if true
      reply_to_address
    else
      from_name = if comment.user.present?
        comment.user.login
      elsif ticket.created_by.present?
        ticket.created_by_user.login
      else
        I18n.t(:anonymous)
      end
      "%s <%s>" % [from_name, reply_to_address]
    end
  end

  # TODO: change me to support virtual domains
  def reply_to_address
    [APP_CONFIG[:mailer][:from_address], APP_CONFIG[:domain]].join('@')
  end

  #
  # returns a hash of {'email' => 'public key'}, where key might be nil.
  #
  def self.reply_recipients(ticket, author)
    recipients = {}
    ticket.comments.each do |comment|
      user = comment.posted_by_user
      if user && (author.nil? || user.id != author.id)
        if user.email
          recipients[user.identity.address] = (user.identity.keys[:pgp] if user.identity.keys[:pgp].present?)
        end
        if user.contact_email.present?
          recipients[user.contact_email] = (user.contact_email_key if user.contact_email_key.present?)
        end
      end
    end
    if author && author.is_admin? && ticket.email.present?
      recipients[ticket.email] = nil
    end
    logger.info { "emailing reply regarding ticket #{ticket.id} to #{recipients.keys.join(', ')}" }
    recipients
  end

end