2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2016-06-16 23:09:34 +05:30
module Emails
module Members
extend ActiveSupport :: Concern
include MembersHelper
2021-03-11 19:13:27 +05:30
include Gitlab :: Experiment :: Dsl
2016-06-16 23:09:34 +05:30
2021-10-27 15:23:28 +05:30
INITIAL_INVITE = 'initial_email'
2016-06-16 23:09:34 +05:30
included do
helper_method :member_source , :member
2021-03-11 19:13:27 +05:30
helper_method :experiment
2016-06-16 23:09:34 +05:30
end
2019-10-12 21:52:04 +05:30
def member_access_requested_email ( member_source_type , member_id , recipient_id )
2016-06-16 23:09:34 +05:30
@member_source_type = member_source_type
@member_id = member_id
2020-08-18 19:51:02 +05:30
return unless member_exists?
2019-12-21 20:55:43 +05:30
user = User . find ( recipient_id )
2022-07-23 23:45:48 +05:30
email_with_layout (
2019-12-26 22:10:19 +05:30
to : user . notification_email_for ( notification_group ) ,
subject : subject ( " Request to join the #{ member_source . human_name } #{ member_source . model_name . singular } " ) )
2016-06-16 23:09:34 +05:30
end
def member_access_granted_email ( member_source_type , member_id )
@member_source_type = member_source_type
@member_id = member_id
2020-08-18 19:51:02 +05:30
return unless member_exists?
2022-07-23 23:45:48 +05:30
email_with_layout (
2019-12-26 22:10:19 +05:30
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-16 23:09:34 +05:30
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 )
2019-12-21 20:55:43 +05:30
user = User . find ( user_id )
2021-12-11 22:18:48 +05:30
@source_hidden = ! member_source . readable_by? ( user )
human_name = @source_hidden ? 'Hidden' : member_source . human_name
2022-07-23 23:45:48 +05:30
email_with_layout (
2019-12-26 22:10:19 +05:30
to : user . notification_email_for ( notification_group ) ,
2021-12-11 22:18:48 +05:30
subject : subject ( " Access to the #{ human_name } #{ member_source . model_name . singular } was denied " ) )
2016-06-16 23:09:34 +05:30
end
def member_invited_email ( member_source_type , member_id , token )
@member_source_type = member_source_type
@member_id = member_id
@token = token
2020-08-18 19:51:02 +05:30
return unless member_exists?
2021-10-27 15:23:28 +05:30
Gitlab :: Tracking . event ( self . class . name , 'invite_email_sent' , label : 'invite_email' , property : member_id . to_s )
2022-10-11 01:57:18 +05:30
mail_with_locale ( to : member . invite_email , subject : invite_email_subject , ** invite_email_headers ) do | format |
2021-09-04 01:27:46 +05:30
format . html { render layout : 'unknown_user_mailer' }
format . text { render layout : 'unknown_user_mailer' }
end
2021-01-03 14:25:43 +05:30
end
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 }
2022-07-23 23:45:48 +05:30
email_with_layout (
2021-01-29 00:20:46 +05:30
layout : 'unknown_user_mailer' ,
2021-01-03 14:25:43 +05:30
to : member . invite_email ,
subject : subject ( subject_line )
)
2016-06-16 23:09:34 +05:30
end
def member_invite_accepted_email ( member_source_type , member_id )
@member_source_type = member_source_type
@member_id = member_id
2020-08-18 19:51:02 +05:30
return unless member_exists?
2016-06-16 23:09:34 +05:30
return unless member . created_by
2022-07-23 23:45:48 +05:30
email_with_layout (
2019-12-26 22:10:19 +05:30
to : member . created_by . notification_email_for ( notification_group ) ,
subject : subject ( 'Invitation accepted' ) )
2016-06-16 23:09:34 +05:30
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
2019-12-21 20:55:43 +05:30
user = User . find ( created_by_id )
2022-07-23 23:45:48 +05:30
email_with_layout (
2019-12-26 22:10:19 +05:30
to : user . notification_email_for ( notification_group ) ,
subject : subject ( 'Invitation declined' ) )
2016-06-16 23:09:34 +05:30
end
2021-03-08 18:12:59 +05:30
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
2022-07-23 23:45:48 +05:30
email_with_layout (
2021-03-08 18:12:59 +05:30
to : member . user . notification_email_for ( notification_group ) ,
subject : subject ( subject ) )
end
2020-08-18 19:51:02 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-06-16 23:09:34 +05:30
def member
2020-08-18 19:51:02 +05:30
@member || = Member . find_by ( id : @member_id )
2016-06-16 23:09:34 +05:30
end
2020-08-18 19:51:02 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-06-16 23:09:34 +05:30
def member_source
@member_source || = member . source
end
2019-10-12 21:52:04 +05:30
def notification_group
@member_source_type . casecmp? ( 'project' ) ? member_source . group : member_source
end
2016-06-16 23:09:34 +05:30
private
2021-09-04 01:27:46 +05:30
def invite_email_subject
if member . created_by
2022-01-26 12:08:38 +05:30
subject ( s_ ( " MemberInviteEmail|%{member_name} invited you to join GitLab " ) % { member_name : member . created_by . name } )
2021-09-04 01:27:46 +05:30
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-11-11 11:23:49 +05:30
if Gitlab :: CurrentSettings . mailgun_events_enabled?
2021-09-04 01:27:46 +05:30
{
2021-11-11 11:23:49 +05:30
'X-Mailgun-Tag' = > :: Members :: Mailgun :: INVITE_EMAIL_TAG ,
'X-Mailgun-Variables' = > { :: Members :: Mailgun :: INVITE_EMAIL_TOKEN_KEY = > @token } . to_json
2021-09-04 01:27:46 +05:30
}
else
{ }
end
end
2020-08-18 19:51:02 +05:30
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-16 23:09:34 +05:30
def member_source_class
@member_source_type . classify . constantize
end
end
end
2021-03-08 18:12:59 +05:30
2021-06-08 01:23:25 +05:30
Emails :: Members . prepend_mod_with ( 'Emails::Members' )