Commit 6d103a2f authored by Rémy Coutable's avatar Rémy Coutable

Factorize members mails into a new Emails::Members module

Signed-off-by: default 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 Emails
module Projects 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) def project_was_moved_email(project_id, user_id, old_path_with_namespace)
@current_user = @user = User.find user_id @current_user = @user = User.find user_id
@project = Project.find project_id @project = Project.find project_id
...@@ -88,13 +25,5 @@ module Emails ...@@ -88,13 +25,5 @@ module Emails
reply_to: @message.reply_to, reply_to: @message.reply_to,
subject: @message.subject) subject: @message.subject)
end 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
end end
...@@ -6,8 +6,8 @@ class Notify < BaseMailer ...@@ -6,8 +6,8 @@ class Notify < BaseMailer
include Emails::Notes include Emails::Notes
include Emails::Projects include Emails::Projects
include Emails::Profile include Emails::Profile
include Emails::Groups
include Emails::Builds include Emails::Builds
include Emails::Members
add_template_helper MergeRequestsHelper add_template_helper MergeRequestsHelper
add_template_helper DiffHelper add_template_helper DiffHelper
......
...@@ -59,6 +59,10 @@ class Group < Namespace ...@@ -59,6 +59,10 @@ class Group < Namespace
"#{self.class.reference_prefix}#{name}" "#{self.class.reference_prefix}#{name}"
end end
def web_url
Gitlab::Routing.url_helpers.group_url(self)
end
def human_name def human_name
name name
end end
......
...@@ -175,23 +175,24 @@ class NotificationService ...@@ -175,23 +175,24 @@ class NotificationService
# Project access request # Project access request
def new_project_access_request(project_member) 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 end
def decline_project_access_request(project, user) 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 end
def invite_project_member(project_member, token) 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 end
def accept_project_invite(project_member) 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 end
def decline_project_invite(project_member) def decline_project_invite(project_member)
mailer.project_invite_declined_email( mailer.member_invite_declined_email(
'project',
project_member.project.id, project_member.project.id,
project_member.invite_email, project_member.invite_email,
project_member.access_level, project_member.access_level,
...@@ -200,32 +201,33 @@ class NotificationService ...@@ -200,32 +201,33 @@ class NotificationService
end end
def new_project_member(project_member) 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 end
def update_project_member(project_member) 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 end
# Group access request # Group access request
def new_group_access_request(group_member) 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 end
def decline_group_access_request(group, user) 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 end
def invite_group_member(group_member, token) 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 end
def accept_group_invite(group_member) 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 end
def decline_group_invite(group_member) def decline_group_invite(group_member)
mailer.group_invite_declined_email( mailer.member_invite_declined_email(
'group',
group_member.group.id, group_member.group.id,
group_member.invite_email, group_member.invite_email,
group_member.access_level, group_member.access_level,
...@@ -234,11 +236,11 @@ class NotificationService ...@@ -234,11 +236,11 @@ class NotificationService
end end
def new_group_member(group_member) 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 end
def update_group_member(group_member) 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 end
def project_was_moved(project, old_path_with_namespace) 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 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 %p
You have been invited You have been invited
- if inviter = @project_member.created_by - if access_requester
by by
= link_to inviter.name, user_url(inviter) = link_to access_requester.name, user_url(access_requester)
to join project to join the
= link_to @project.name_with_namespace, namespace_project_url(@project.namespace, @project) = link_to member_target_name, member_target_url
as #{@project_member.human_access}. #{member_target_type} as #{member.human_access}.
%p %p
= link_to 'Accept invitation', invite_url(@token) = link_to 'Accept invitation', 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 %>. 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) %> Accept invitation: <%= invite_url(@token) %>
Decline invitation: <%= decline_invite_url(@token) %> Decline invitation: <%= decline_invite_url(@token) %>
%p
Your request to join project #{link_to @project.name_with_namespace, @target_url}
has been denied.
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) %>
This diff is collapsed.
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