2020-07-14 11:09:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Emails
|
|
|
|
module ServiceDesk
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include MarkupHelper
|
|
|
|
|
|
|
|
included do
|
|
|
|
layout 'service_desk', only: [:service_desk_thank_you_email, :service_desk_new_note_email]
|
|
|
|
end
|
|
|
|
|
|
|
|
def service_desk_thank_you_email(issue_id)
|
|
|
|
setup_service_desk_mail(issue_id)
|
|
|
|
|
|
|
|
email_sender = sender(
|
|
|
|
@support_bot.id,
|
|
|
|
send_from_user_email: false,
|
|
|
|
sender_name: @project.service_desk_setting&.outgoing_name
|
|
|
|
)
|
2021-03-15 11:09:07 -04:00
|
|
|
options = service_desk_options(email_sender, 'thank_you', @issue.external_author)
|
2020-07-14 11:09:05 -04:00
|
|
|
.merge(subject: "Re: #{subject_base}")
|
|
|
|
|
2021-06-21 08:07:45 -04:00
|
|
|
mail_new_thread(@issue, options)
|
2020-07-14 11:09:05 -04:00
|
|
|
end
|
|
|
|
|
2021-03-15 11:09:07 -04:00
|
|
|
def service_desk_new_note_email(issue_id, note_id, recipient)
|
2020-07-14 11:09:05 -04:00
|
|
|
@note = Note.find(note_id)
|
|
|
|
setup_service_desk_mail(issue_id)
|
|
|
|
|
|
|
|
email_sender = sender(@note.author_id)
|
2021-03-15 11:09:07 -04:00
|
|
|
options = service_desk_options(email_sender, 'new_note', recipient)
|
2020-07-14 11:09:05 -04:00
|
|
|
.merge(subject: subject_base)
|
|
|
|
|
2021-06-21 08:07:45 -04:00
|
|
|
mail_answer_thread(@issue, options)
|
2020-07-14 11:09:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def setup_service_desk_mail(issue_id)
|
|
|
|
@issue = Issue.find(issue_id)
|
|
|
|
@project = @issue.project
|
|
|
|
@support_bot = User.support_bot
|
|
|
|
|
|
|
|
@sent_notification = SentNotification.record(@issue, @support_bot.id, reply_key)
|
|
|
|
end
|
|
|
|
|
2021-03-15 11:09:07 -04:00
|
|
|
def service_desk_options(email_sender, email_type, recipient)
|
2020-07-14 11:09:05 -04:00
|
|
|
{
|
|
|
|
from: email_sender,
|
2021-03-15 11:09:07 -04:00
|
|
|
to: recipient
|
2020-07-14 11:09:05 -04:00
|
|
|
}.tap do |options|
|
|
|
|
next unless template_body = template_content(email_type)
|
|
|
|
|
|
|
|
options[:body] = template_body
|
|
|
|
options[:content_type] = 'text/html'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def template_content(email_type)
|
|
|
|
template = Gitlab::Template::ServiceDeskTemplate.find(email_type, @project)
|
|
|
|
text = substitute_template_replacements(template.content)
|
|
|
|
|
2020-10-26 08:08:44 -04:00
|
|
|
context = { project: @project, pipeline: :email }
|
|
|
|
context[:author] = @note.author if email_type == 'new_note'
|
|
|
|
|
|
|
|
markdown(text, context)
|
2020-07-14 11:09:05 -04:00
|
|
|
rescue Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def substitute_template_replacements(template_body)
|
|
|
|
template_body
|
|
|
|
.gsub(/%\{\s*ISSUE_ID\s*\}/, issue_id)
|
|
|
|
.gsub(/%\{\s*ISSUE_PATH\s*\}/, issue_path)
|
|
|
|
.gsub(/%\{\s*NOTE_TEXT\s*\}/, note_text)
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_id
|
|
|
|
"#{Issue.reference_prefix}#{@issue.iid}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_path
|
|
|
|
@issue.to_reference(full: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def note_text
|
|
|
|
@note&.note.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def subject_base
|
|
|
|
"#{@issue.title} (##{@issue.iid})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|