diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-05-21 19:40:08 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-05-21 19:40:08 +0300 |
commit | 75415663f84ac006c5a4d5a6896ece50c299c03e (patch) | |
tree | e263b7dc9aca0200eeb14e079e7dda69e7aad3c8 /lib/gitlab/email/handler | |
parent | ee548b6ed0e8f885cdd7dfcc104ea1471ad62bd0 (diff) |
Rename handlers and introduce Handler.for
Diffstat (limited to 'lib/gitlab/email/handler')
-rw-r--r-- | lib/gitlab/email/handler/base_handler.rb | 57 | ||||
-rw-r--r-- | lib/gitlab/email/handler/create_issue_handler.rb (renamed from lib/gitlab/email/handler/create_issue.rb) | 6 | ||||
-rw-r--r-- | lib/gitlab/email/handler/create_note_handler.rb (renamed from lib/gitlab/email/handler/create_note.rb) | 6 |
3 files changed, 63 insertions, 6 deletions
diff --git a/lib/gitlab/email/handler/base_handler.rb b/lib/gitlab/email/handler/base_handler.rb new file mode 100644 index 00000000000..230d13feea9 --- /dev/null +++ b/lib/gitlab/email/handler/base_handler.rb @@ -0,0 +1,57 @@ + +module Gitlab + module Email + module Handler + class BaseHandler + 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? + raise ProjectNotFound unless author.can?(:read_project, 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 +end diff --git a/lib/gitlab/email/handler/create_issue.rb b/lib/gitlab/email/handler/create_issue_handler.rb index 72d49ec6c96..259d74a83bf 100644 --- a/lib/gitlab/email/handler/create_issue.rb +++ b/lib/gitlab/email/handler/create_issue_handler.rb @@ -1,10 +1,10 @@ -require 'gitlab/email/handler' +require 'gitlab/email/handler/base_handler' module Gitlab module Email - class Handler - class CreateIssue < Handler + module Handler + class CreateIssueHandler < BaseHandler def can_handle? !!project end diff --git a/lib/gitlab/email/handler/create_note.rb b/lib/gitlab/email/handler/create_note_handler.rb index 32deb5a311e..7252906fd48 100644 --- a/lib/gitlab/email/handler/create_note.rb +++ b/lib/gitlab/email/handler/create_note_handler.rb @@ -1,10 +1,10 @@ -require 'gitlab/email/handler' +require 'gitlab/email/handler/base_handler' module Gitlab module Email - class Handler - class CreateNote < Handler + module Handler + class CreateNoteHandler < BaseHandler def can_handle? !!sent_notification end |