BigW Consortium Gitlab

Commit 6d103a2f by Rémy Coutable

Factorize members mails into a new Emails::Members module

Signed-off-by: 's avatarRémy Coutable <remy@rymai.me>
parent d75edf1a
module Emails
module Groups
def group_access_requested_email(group_member_id)
setup_group_member_mail(group_member_id)
@requester = @group_member.created_by
group_admins = User.where(id: @group.group_members.admins.pluck(:user_id)).pluck(:notification_email)
mail(to: group_admins,
subject: subject("Request to join #{@group.name} group"))
end
def group_access_granted_email(group_member_id)
setup_group_member_mail(group_member_id)
@current_user = @group_member.user
mail(to: @current_user.notification_email,
subject: subject("Access to #{@group.name} group was granted"))
end
def group_access_denied_email(group_id, user_id)
@group = Group.find(group_id)
@current_user = User.find(user_id)
@target_url = group_url(@group)
mail(to: @current_user.notification_email,
subject: subject("Access to #{@group.name} group was denied"))
end
def group_member_invited_email(group_member_id, token)
setup_group_member_mail(group_member_id)
@token = token
@current_user = @group_member.user
mail(to: @group_member.invite_email,
subject: "Invitation to join group #{@group.name}")
end
def group_invite_accepted_email(group_member_id)
setup_group_member_mail(group_member_id)
return if @group_member.created_by.nil?
@current_user = @group_member.created_by
mail(to: @current_user.notification_email,
subject: subject("Invitation accepted"))
end
def group_invite_declined_email(group_id, invite_email, access_level, created_by_id)
return if created_by_id.nil?
@group = Group.find(group_id)
@current_user = @created_by = User.find(created_by_id)
@access_level = access_level
@invite_email = invite_email
@target_url = group_url(@group)
mail(to: @created_by.notification_email,
subject: subject("Invitation declined"))
end
private
def setup_group_member_mail(group_member_id)
@group_member = GroupMember.find(group_member_id)
@group = @group_member.group
@target_url = group_url(@group)
end
end
end
module Emails
module Members
extend ActiveSupport::Concern
included do
attr_reader :member_target_type
helper_method :member, :access_requester, :member_target_type, :member_target_name, :member_target_url
end
def member_access_requested_email(member_target_type, member_id)
@member_target_type = member_target_type
@member_id = member_id
admins = User.where(id: target.public_send(members_association).admins.pluck(:user_id)).pluck(:notification_email)
mail(to: admins,
subject: subject("Request to join the #{member_target_name} #{member_target_type}"))
end
def member_access_granted_email(member_target_type, member_id)
@member_target_type = member_target_type
@member_id = member_id
mail(to: member.user.notification_email,
subject: subject("Access to the #{member_target_name} #{member_target_type} was granted"))
end
def member_access_denied_email(member_target_type, target_id, user_id)
@member_target_type = member_target_type
@target = target_class.find(target_id)
mail(to: User.find(user_id).notification_email,
subject: subject("Access to the #{member_target_name} #{member_target_type} was denied"))
end
def member_invited_email(member_target_type, member_id, token)
@member_target_type = member_target_type
@member_id = member_id
@token = token
mail(to: member.invite_email,
subject: "Invitation to join the #{member_target_name} #{member_target_type}")
end
def member_invite_accepted_email(member_target_type, member_id)
@member_target_type = member_target_type
@member_id = member_id
return if access_requester.nil?
mail(to: access_requester.notification_email,
subject: subject('Invitation accepted'))
end
def member_invite_declined_email(member_target_type, target_id, invite_email, created_by_id)
return if created_by_id.nil?
@member_target_type = member_target_type
@target = target_class.find(target_id)
@invite_email = invite_email
mail(to: User.find(created_by_id).notification_email,
subject: subject('Invitation declined'))
end
def member
@member ||= member_class.find(@member_id)
end
def access_requester
@access_requester ||= member.created_by
end
def member_target_name
case member_target_type
when 'project'
target.name_with_namespace
else
target.name
end
end
def member_target_url
@member_target_url ||= target.web_url
end
private
def target
@target ||= member.public_send(member_target_type)
end
def target_class
@target_class ||= member_target_type.classify.constantize
end
def member_class
@member_class ||= "#{member_target_type.classify}Member".constantize
end
def members_association
@members_association ||= member_class.to_s.tableize
end
end
end
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
......@@ -88,13 +25,5 @@ module Emails
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
......@@ -6,8 +6,8 @@ class Notify < BaseMailer
include Emails::Notes
include Emails::Projects
include Emails::Profile
include Emails::Groups
include Emails::Builds
include Emails::Members
add_template_helper MergeRequestsHelper
add_template_helper DiffHelper
......
......@@ -59,6 +59,10 @@ class Group < Namespace
"#{self.class.reference_prefix}#{name}"
end
def web_url
Gitlab::Routing.url_helpers.group_url(self)
end
def human_name
name
end
......
......@@ -175,23 +175,24 @@ class NotificationService
# Project access request
def new_project_access_request(project_member)
mailer.project_access_requested_email(project_member.id).deliver_later
mailer.member_access_requested_email('project', project_member.id).deliver_later
end
def decline_project_access_request(project, user)
mailer.project_access_denied_email(project.id, user.id).deliver_later
mailer.member_access_denied_email('project', project.id, user.id).deliver_later
end
def invite_project_member(project_member, token)
mailer.project_member_invited_email(project_member.id, token).deliver_later
mailer.member_invited_email('project', project_member.id, token).deliver_later
end
def accept_project_invite(project_member)
mailer.project_invite_accepted_email(project_member.id).deliver_later
mailer.member_invite_accepted_email('project', project_member.id).deliver_later
end
def decline_project_invite(project_member)
mailer.project_invite_declined_email(
mailer.member_invite_declined_email(
'project',
project_member.project.id,
project_member.invite_email,
project_member.access_level,
......@@ -200,32 +201,33 @@ class NotificationService
end
def new_project_member(project_member)
mailer.project_access_granted_email(project_member.id).deliver_later
mailer.member_access_granted_email('project', project_member.id).deliver_later
end
def update_project_member(project_member)
mailer.project_access_granted_email(project_member.id).deliver_later
mailer.member_access_granted_email('project', project_member.id).deliver_later
end
# Group access request
def new_group_access_request(group_member)
mailer.group_access_requested_email(group_member.id).deliver_later
mailer.member_access_requested_email('group', group_member.id).deliver_later
end
def decline_group_access_request(group, user)
mailer.group_access_denied_email(group.id, user.id).deliver_later
mailer.member_access_denied_email('group', group.id, user.id).deliver_later
end
def invite_group_member(group_member, token)
mailer.group_member_invited_email(group_member.id, token).deliver_later
mailer.member_invited_email('group', group_member.id, token).deliver_later
end
def accept_group_invite(group_member)
mailer.group_invite_accepted_email(group_member.id).deliver_later
mailer.member_invite_accepted_email(group_member.id).deliver_later
end
def decline_group_invite(group_member)
mailer.group_invite_declined_email(
mailer.member_invite_declined_email(
'group',
group_member.group.id,
group_member.invite_email,
group_member.access_level,
......@@ -234,11 +236,11 @@ class NotificationService
end
def new_group_member(group_member)
mailer.group_access_granted_email(group_member.id).deliver_later
mailer.member_access_granted_email('group', group_member.id).deliver_later
end
def update_group_member(group_member)
mailer.group_access_granted_email(group_member.id).deliver_later
mailer.member_access_granted_email('group', group_member.id).deliver_later
end
def project_was_moved(project, old_path_with_namespace)
......
%p
Your request to join group #{link_to @group.name, @target_url} has been denied.
Your request to join group <%= @group.name %> has been denied.
<%= @target_url %>
%p
#{link_to @requester.name, @requester} requested #{@group_member.human_access}
access to group #{link_to @group.name, @target_url}.
<%= @requester.name %> (<%= user_url(@requester) %>) requested <%= @group_member.human_access %> access to group <%= @group.name %>
<%= @target_url %>
%p
Your request to join project #{link_to @project.name_with_namespace, @target_url}
Your request to join the
#{link_to member_target_name, member_target_url} #{member_target_type}
has been denied.
Your request to join the <%= member_target_name %> <%= member_target_type %> has been denied.
<%= member_target_url %>
%p
You have been granted #{member.human_access} access to the
#{link_to member_target_name, member_target_url} #{member_target_type}.
You have been granted <%= member.human_access %> access to the <%= member_target_name %> <%= member_target_type %>.
<%= member_target_url %>
%p
#{link_to access_requester.name, access_requester} requested #{member.human_access}
access to the #{link_to member_target_name, member_target_url} #{member_target_type}.
<%= access_requester.name %> (<%= user_url(access_requester) %>) requested <%= member.human_access %> access to the <%= member_target_name %> <%= member_target_type %>.
<%= member_target_url %>
%p
#{member.invite_email}, now known as
#{link_to member.user.name, user_url(member.user)},
has accepted your invitation to join the
#{link_to member_target_name, member_target_url} #{member_target_type}.
<%= member.invite_email %>, now known as <%= member.user.name %>, has accepted your invitation to join the <%= member_target_name %> <%= member_target_type %>.
<%= member_target_url %>
%p
#{@invite_email}
has declined your invitation to join the
#{link_to member_target_name, member_target_url} #{member_target_type}.
<%= @invite_email %> has declined your invitation to join the <%= member_target_name %> <%= member_target_type %>.
<%= member_target_url %>
%p
You have been invited
- if access_requester
by
= link_to access_requester.name, user_url(access_requester)
to join the
= link_to member_target_name, member_target_url
#{member_target_type} as #{member.human_access}.
%p
= link_to 'Accept invitation', invite_url(@token)
or
= link_to 'decline', decline_invite_url(@token)
You have been invited <%= "by #{access_requester.name} " if access_requester %>to join the <%= member_target_name %> <%= member_target_type %> as <%= member.human_access %>.
Accept invitation: <%= invite_url(@token) %>
Decline invitation: <%= decline_invite_url(@token) %>
Your request to join project <%= @project.name_with_namespace %> has been denied.
<%= @target_url %>
%p
You have been granted #{@project_member.human_access} access to project
#{link_to @project.name_with_namespace, @target_url}.
You have been granted <%= @project_member.human_access %> access to project <%= @project.name_with_namespace %>.
<%= @target_url %>
%p
#{link_to @requester.name, @requester} requested #{@project_member.human_access}
access to project #{link_to @project.name_with_namespace, @target_url}.
<%= @requester.name %> (<%= user_url(@requester) %>) requested <%= @project_member.human_access %> access to project <%= @project.name_with_namespace %>.
<%= @target_url %>
%p
#{@project_member.invite_email}, now known as
#{link_to @project_member.user.name, user_url(@project_member.user)},
has accepted your invitation to join project
#{link_to @project.name_with_namespace, namespace_project_url(@project.namespace, @project)}.
<%= @project_member.invite_email %>, now known as <%= @project_member.user.name %>, has accepted your invitation to join project <%= @project.name_with_namespace %>.
<%= namespace_project_url(@project.namespace, @project) %>
%p
#{@invite_email}
has declined your invitation to join project
#{link_to @project.name_with_namespace, namespace_project_url(@project.namespace, @project)}.
<%= @invite_email %> has declined your invitation to join project <%= @project.name_with_namespace %>.
<%= namespace_project_url(@project.namespace, @project) %>
%p
You have been invited
- if inviter = @project_member.created_by
by
= link_to inviter.name, user_url(inviter)
to join project
= link_to @project.name_with_namespace, namespace_project_url(@project.namespace, @project)
as #{@project_member.human_access}.
%p
= link_to 'Accept invitation', invite_url(@token)
or
= link_to 'decline', decline_invite_url(@token)
You have been invited <%= "by #{@project_member.created_by.name} " if @project_member.created_by %>to join project <%= @project.name_with_namespace %> as <%= @project_member.human_access %>.
Accept invitation: <%= invite_url(@token) %>
Decline invitation: <%= decline_invite_url(@token) %>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment