diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-05-18 17:19:25 -0500 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-05-18 17:25:45 -0500 |
commit | c337e748d385fea5c768a8e7de55975dca7fa484 (patch) | |
tree | f9646fbb68614b633964bc43d725296b61047fec /lib/gitlab/email/handler.rb | |
parent | 3f4a6412dcc35c182d993cd1350459e8a4a1b8d1 (diff) | |
download | gitlab-ce-c337e748d385fea5c768a8e7de55975dca7fa484.tar.gz |
so we use separate classes to handle different tasks
Diffstat (limited to 'lib/gitlab/email/handler.rb')
-rw-r--r-- | lib/gitlab/email/handler.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/gitlab/email/handler.rb b/lib/gitlab/email/handler.rb new file mode 100644 index 00000000000..55fbee276b8 --- /dev/null +++ b/lib/gitlab/email/handler.rb @@ -0,0 +1,55 @@ + +module Gitlab + module Email + class Handler + attr_reader :mail, :mail_key + + def initialize(mail, mail_key) + @mail = mail + @mail_key = mail_key + end + + def message + @message ||= process_message + end + + def author + raise NotImplementedError + end + + def project + raise NotImplementedError + end + + private + def validate_permission!(permission) + raise UserNotFoundError unless author + raise UserBlockedError if author.blocked? + # TODO: Give project not found error if author cannot read project + raise UserNotAuthorizedError unless author.can?(permission, project) + end + + def process_message + add_attachments(ReplyParser.new(mail).execute.strip) + 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 verify_record(record, exception, error_title) + return if record.persisted? + + msg = error_title + record.errors.full_messages.map do |error| + "\n\n- #{error}" + end.join + + raise exception, msg + end + end + end +end |