2018-08-15 17:45:57 -04:00
# frozen_string_literal: true
2016-06-02 10:14:02 -04:00
module Emails
module Members
extend ActiveSupport :: Concern
2016-06-02 12:05:06 -04:00
include MembersHelper
2021-02-03 10:09:24 -05:00
include Gitlab :: Experiment :: Dsl
2016-06-02 10:14:02 -04:00
2021-07-29 05:08:46 -04:00
INITIAL_INVITE = 'initial_email'
2016-06-02 10:14:02 -04:00
included do
2016-06-02 12:05:06 -04:00
helper_method :member_source , :member
2021-02-03 10:09:24 -05:00
helper_method :experiment
2016-06-02 10:14:02 -04:00
end
2019-07-23 23:36:07 -04:00
def member_access_requested_email ( member_source_type , member_id , recipient_id )
2016-06-02 12:05:06 -04:00
@member_source_type = member_source_type
2016-06-02 10:14:02 -04:00
@member_id = member_id
2020-08-05 17:09:40 -04:00
return unless member_exists?
2019-09-30 08:06:01 -04:00
user = User . find ( recipient_id )
2019-11-12 16:06:30 -05:00
member_email_with_layout (
to : user . notification_email_for ( notification_group ) ,
subject : subject ( " Request to join the #{ member_source . human_name } #{ member_source . model_name . singular } " ) )
2016-06-02 10:14:02 -04:00
end
2016-06-02 12:05:06 -04:00
def member_access_granted_email ( member_source_type , member_id )
@member_source_type = member_source_type
2016-06-02 10:14:02 -04:00
@member_id = member_id
2020-08-05 17:09:40 -04:00
return unless member_exists?
2019-11-12 16:06:30 -05:00
member_email_with_layout (
to : member . user . notification_email_for ( notification_group ) ,
subject : subject ( " Access to the #{ member_source . human_name } #{ member_source . model_name . singular } was granted " ) )
2016-06-02 10:14:02 -04:00
end
2016-06-02 12:05:06 -04:00
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 )
2016-06-02 10:14:02 -04:00
2019-09-30 08:06:01 -04:00
user = User . find ( user_id )
2019-11-12 16:06:30 -05:00
member_email_with_layout (
to : user . notification_email_for ( notification_group ) ,
subject : subject ( " Access to the #{ member_source . human_name } #{ member_source . model_name . singular } was denied " ) )
2016-06-02 10:14:02 -04:00
end
2016-06-02 12:05:06 -04:00
def member_invited_email ( member_source_type , member_id , token )
@member_source_type = member_source_type
2016-06-02 10:14:02 -04:00
@member_id = member_id
@token = token
2020-08-05 17:09:40 -04:00
return unless member_exists?
2021-07-29 05:08:46 -04:00
Gitlab :: Tracking . event ( self . class . name , 'invite_email_sent' , label : 'invite_email' , property : member_id . to_s )
2021-08-25 05:10:52 -04:00
mail ( to : member . invite_email , subject : invite_email_subject , ** invite_email_headers . merge ( additional_invite_settings ) ) do | format |
2021-05-26 08:10:41 -04:00
format . html { render layout : 'unknown_user_mailer' }
format . text { render layout : 'unknown_user_mailer' }
end
2016-06-02 10:14:02 -04:00
end
2020-10-05 14:08:51 -04:00
def member_invited_reminder_email ( member_source_type , member_id , token , reminder_index )
@member_source_type = member_source_type
@member_id = member_id
@token = token
@reminder_index = reminder_index
return unless member_exists? && member . created_by && member . invite_to_unknown_user?
subjects = {
0 = > s_ ( " InviteReminderEmail|%{inviter}'s invitation to GitLab is pending " ) ,
1 = > s_ ( 'InviteReminderEmail|%{inviter} is waiting for you to join GitLab' ) ,
2 = > s_ ( 'InviteReminderEmail|%{inviter} is still waiting for you to join GitLab' )
}
subject_line = subjects [ reminder_index ] % { inviter : member . created_by . name }
member_email_with_layout (
2020-10-20 08:08:54 -04:00
layout : 'unknown_user_mailer' ,
2020-10-05 14:08:51 -04:00
to : member . invite_email ,
subject : subject ( subject_line )
)
end
2016-06-02 12:05:06 -04:00
def member_invite_accepted_email ( member_source_type , member_id )
@member_source_type = member_source_type
2016-06-02 10:14:02 -04:00
@member_id = member_id
2020-08-05 17:09:40 -04:00
return unless member_exists?
2016-06-02 12:05:06 -04:00
return unless member . created_by
2016-06-02 10:14:02 -04:00
2019-11-12 16:06:30 -05:00
member_email_with_layout (
to : member . created_by . notification_email_for ( notification_group ) ,
subject : subject ( 'Invitation accepted' ) )
2016-06-02 10:14:02 -04:00
end
2016-06-02 12:05:06 -04:00
def member_invite_declined_email ( member_source_type , source_id , invite_email , created_by_id )
return unless created_by_id
2016-06-02 10:14:02 -04:00
2016-06-02 12:05:06 -04:00
@member_source_type = member_source_type
@member_source = member_source_class . find ( source_id )
2016-06-02 10:14:02 -04:00
@invite_email = invite_email
2019-09-30 08:06:01 -04:00
user = User . find ( created_by_id )
2019-11-12 16:06:30 -05:00
member_email_with_layout (
to : user . notification_email_for ( notification_group ) ,
subject : subject ( 'Invitation declined' ) )
2016-06-02 10:14:02 -04:00
end
2021-01-14 13:10:59 -05:00
def member_expiration_date_updated_email ( member_source_type , member_id )
@member_source_type = member_source_type
@member_id = member_id
return unless member_exists?
subject = if member . expires?
_ ( 'Group membership expiration date changed' )
else
_ ( 'Group membership expiration date removed' )
end
member_email_with_layout (
to : member . user . notification_email_for ( notification_group ) ,
subject : subject ( subject ) )
end
2020-08-05 17:09:40 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2016-06-02 10:14:02 -04:00
def member
2020-08-05 17:09:40 -04:00
@member || = Member . find_by ( id : @member_id )
2016-06-02 10:14:02 -04:00
end
2020-08-05 17:09:40 -04:00
# rubocop: enable CodeReuse/ActiveRecord
2016-06-02 10:14:02 -04:00
2016-06-02 12:05:06 -04:00
def member_source
@member_source || = member . source
2016-06-02 10:14:02 -04:00
end
2019-07-23 23:36:07 -04:00
def notification_group
@member_source_type . casecmp? ( 'project' ) ? member_source . group : member_source
end
2016-06-02 10:14:02 -04:00
private
2021-05-26 08:10:41 -04:00
def invite_email_subject
if member . created_by
2021-08-25 05:10:52 -04:00
experiment ( :invite_email_from , actor : member ) do | experiment_instance |
experiment_instance . use do
subject ( s_ ( " MemberInviteEmail|%{member_name} invited you to join GitLab " ) % { member_name : member . created_by . name } )
end
experiment_instance . candidate do
subject ( s_ ( " MemberInviteEmail|I've invited you to join me in GitLab " ) )
end
experiment_instance . run
end
2021-05-26 08:10:41 -04:00
else
subject ( s_ ( " MemberInviteEmail|Invitation to join the %{project_or_group} %{project_or_group_name} " ) % { project_or_group : member_source . human_name , project_or_group_name : member_source . model_name . singular } )
end
end
def invite_email_headers
2021-07-30 17:10:15 -04:00
if Gitlab . dev_env_or_com?
2021-05-26 08:10:41 -04:00
{
2021-07-30 17:10:15 -04:00
'X-Mailgun-Tag' = > 'invite_email' ,
'X-Mailgun-Variables' = > { 'invite_token' = > @token } . to_json
2021-05-26 08:10:41 -04:00
}
else
{ }
end
end
2021-08-25 05:10:52 -04:00
def additional_invite_settings
return { } unless member . created_by
experiment ( :invite_email_from , actor : member ) do | experiment_instance |
experiment_instance . use { { } }
experiment_instance . candidate do
{
from : " #{ member . created_by . name } < #{ member . created_by . email } > "
}
end
experiment_instance . run
end
end
2020-08-05 17:09:40 -04:00
def member_exists?
Gitlab :: AppLogger . info ( " Tried to send an email invitation for a deleted group. Member id: #{ @member_id } " ) if member . blank?
member . present?
end
2016-06-02 12:05:06 -04:00
def member_source_class
@member_source_type . classify . constantize
2016-06-02 10:14:02 -04:00
end
2019-11-12 16:06:30 -05:00
2020-10-20 08:08:54 -04:00
def member_email_with_layout ( to : , subject : , layout : 'mailer' )
2019-11-12 16:06:30 -05:00
mail ( to : to , subject : subject ) do | format |
2020-10-20 08:08:54 -04:00
format . html { render layout : layout }
format . text { render layout : layout }
2019-11-12 16:06:30 -05:00
end
end
2016-06-02 10:14:02 -04:00
end
end
2021-01-13 13:10:55 -05:00
2021-05-11 17:10:21 -04:00
Emails :: Members . prepend_mod_with ( 'Emails::Members' )