summaryrefslogtreecommitdiff
path: root/app/mailers/emails/projects.rb
blob: 43a2a7e80a882a5050d9457dd7a30bc75bbe0632 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module Emails
  module Projects
    def project_access_requested_email(project_member_id)
      setup_project_member_mail(project_member_id)

      @requester = @project_member.created_by

      project_admins = User.where(id: @project.project_members.admins.pluck(:user_id)).pluck(:notification_email)

      mail(to: project_admins,
           subject: subject("Request to join #{@project.name_with_namespace} project"))
    end

    def project_access_granted_email(project_member_id)
      setup_project_member_mail(project_member_id)

      @current_user = @project_member.user

      mail(to: @current_user.notification_email,
           subject: subject("Access to #{@project.name_with_namespace} project was granted"))
    end

    def project_access_denied_email(project_id, user_id)
      @project = Project.find(project_id)
      @current_user = User.find(user_id)
      @target_url = namespace_project_url(@project.namespace, @project)

      mail(to: @current_user.notification_email,
           subject: subject("Access to #{@project.name_with_namespace} project was denied"))
    end

    def project_member_invited_email(project_member_id, token)
      setup_project_member_mail(project_member_id)

      @token = token
      @current_user = @project_member.user

      mail(to: @project_member.invite_email,
           subject: "Invitation to join project #{@project.name_with_namespace}")
    end

    def project_invite_accepted_email(project_member_id)
      setup_project_member_mail(project_member_id)
      return if @project_member.created_by.nil?

      @current_user = @project_member.created_by

      mail(to: @project_member.created_by.notification_email,
           subject: subject("Invitation accepted"))
    end

    def project_invite_declined_email(project_id, invite_email, access_level, created_by_id)
      return if created_by_id.nil?

      @project = Project.find(project_id)
      @current_user = @created_by = User.find(created_by_id)
      @access_level = access_level
      @invite_email = invite_email

      @target_url = namespace_project_url(@project.namespace, @project)

      mail(to: @created_by.notification_email,
           subject: subject("Invitation declined"))
    end

    def project_was_moved_email(project_id, user_id, old_path_with_namespace)
      @current_user = @user = User.find user_id
      @project = Project.find project_id
      @target_url = namespace_project_url(@project.namespace, @project)
      @old_path_with_namespace = old_path_with_namespace
      mail(to: @user.notification_email,
           subject: subject("Project was moved"))
    end

    def repository_push_email(project_id, opts = {})
      @message =
        Gitlab::Email::Message::RepositoryPush.new(self, project_id, opts)

      # used in notify layout
      @target_url = @message.target_url
      @project = Project.find(project_id)
      @diff_notes_disabled = true

      add_project_headers
      headers['X-GitLab-Author'] = @message.author_username

      mail(from:      sender(@message.author_id, @message.send_from_committer_email?),
           reply_to:  @message.reply_to,
           subject:   @message.subject)
    end

    private

    def setup_project_member_mail(project_member_id)
      @project_member = ProjectMember.find(project_member_id)
      @project = @project_member.project
      @target_url = namespace_project_url(@project.namespace, @project)
    end
  end
end