summaryrefslogtreecommitdiff
path: root/app/mailers/emails/members.rb
blob: 453116902937b23f9b191ab4b5d0f11adc2c6a6f (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
83
84
85
86
module Emails
  module Members
    extend ActiveSupport::Concern
    include MembersHelper

    included do
      helper_method :member_source, :member
    end

    def member_access_requested_email(member_source_type, member_id)
      @member_source_type = member_source_type
      @member_id = member_id

      admins = member_source.members.owners_and_masters.includes(:user).pluck(:notification_email)
      # A project in a group can have no explicit owners/masters, in that case
      # we fallbacks to the group's owners/masters.
      if admins.empty? && member_source.respond_to?(:group) && member_source.group
        admins = member_source.group.members.owners_and_masters.includes(:user).pluck(:notification_email)
      end

      mail(to: admins,
           subject: subject("Request to join the #{member_source.human_name} #{member_source.model_name.singular}"))
    end

    def member_access_granted_email(member_source_type, member_id)
      @member_source_type = member_source_type
      @member_id = member_id

      mail(to: member.user.notification_email,
           subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was granted"))
    end

    def member_access_denied_email(member_source_type, source_id, user_id)
      @member_source_type = member_source_type
      @member_source = member_source_class.find(source_id)
      requester = User.find(user_id)

      mail(to: requester.notification_email,
           subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was denied"))
    end

    def member_invited_email(member_source_type, member_id, token)
      @member_source_type = member_source_type
      @member_id = member_id
      @token = token

      mail(to: member.invite_email,
           subject: "Invitation to join the #{member_source.human_name} #{member_source.model_name.singular}")
    end

    def member_invite_accepted_email(member_source_type, member_id)
      @member_source_type = member_source_type
      @member_id = member_id
      return unless member.created_by

      mail(to: member.created_by.notification_email,
           subject: subject('Invitation accepted'))
    end

    def member_invite_declined_email(member_source_type, source_id, invite_email, created_by_id)
      return unless created_by_id

      @member_source_type = member_source_type
      @member_source = member_source_class.find(source_id)
      @invite_email = invite_email
      inviter = User.find(created_by_id)

      mail(to: inviter.notification_email,
           subject: subject('Invitation declined'))
    end

    def member
      @member ||= Member.find(@member_id)
    end

    def member_source
      @member_source ||= member.source
    end

    private

    def member_source_class
      @member_source_type.classify.constantize
    end
  end
end