2018-08-23 05:22:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-12 13:07:36 -04:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
module ReplyProcessing
|
|
|
|
private
|
|
|
|
|
2018-12-28 13:53:39 -05:00
|
|
|
attr_reader :project_id, :project_slug, :project_path, :incoming_email_token
|
|
|
|
|
2016-10-12 13:07:36 -04:00
|
|
|
def author
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2018-12-28 13:53:39 -05:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-10-12 13:07:36 -04:00
|
|
|
def project
|
2018-12-28 13:53:39 -05:00
|
|
|
return @project if instance_variable_defined?(:@project)
|
|
|
|
|
|
|
|
if project_id
|
|
|
|
@project = Project.find_by_id(project_id)
|
|
|
|
@project = nil unless valid_project_slug?(@project)
|
|
|
|
else
|
|
|
|
@project = Project.find_by_full_path(project_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
2016-10-12 13:07:36 -04:00
|
|
|
end
|
2018-12-28 13:53:39 -05:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-10-12 13:07:36 -04:00
|
|
|
|
|
|
|
def message
|
|
|
|
@message ||= process_message
|
|
|
|
end
|
|
|
|
|
2018-05-09 16:19:16 -04:00
|
|
|
def message_including_reply
|
|
|
|
@message_with_reply ||= process_message(trim_reply: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_message(**kwargs)
|
|
|
|
message = ReplyParser.new(mail, **kwargs).execute.strip
|
2016-10-12 13:07:36 -04:00
|
|
|
add_attachments(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_attachments(reply)
|
|
|
|
attachments = Email::AttachmentUploader.new(mail).execute(project)
|
|
|
|
|
|
|
|
reply + attachments.map do |link|
|
|
|
|
"\n\n#{link[:markdown]}"
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_permission!(permission)
|
|
|
|
raise UserNotFoundError unless author
|
|
|
|
raise UserBlockedError if author.blocked?
|
2018-05-03 17:32:20 -04:00
|
|
|
|
|
|
|
if project
|
|
|
|
raise ProjectNotFound unless author.can?(:read_project, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
raise UserNotAuthorizedError unless author.can?(permission, project || noteable)
|
2016-10-12 13:07:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify_record!(record:, invalid_exception:, record_name:)
|
|
|
|
return if record.persisted?
|
|
|
|
return if record.errors.key?(:commands_only)
|
|
|
|
|
|
|
|
error_title = "The #{record_name} could not be created for the following reasons:"
|
|
|
|
|
|
|
|
msg = error_title + record.errors.full_messages.map do |error|
|
|
|
|
"\n\n- #{error}"
|
|
|
|
end.join
|
|
|
|
|
|
|
|
raise invalid_exception, msg
|
|
|
|
end
|
2018-12-28 13:53:39 -05:00
|
|
|
|
|
|
|
def valid_project_slug?(found_project)
|
|
|
|
project_slug == found_project.full_path_slug
|
|
|
|
end
|
2016-10-12 13:07:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|