Commit 75415663 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Rename handlers and introduce Handler.for

parent ee548b6e
require 'gitlab/email/handler/create_note_handler'
require 'gitlab/email/handler/create_issue_handler'
module Gitlab module Gitlab
module Email module Email
class Handler module Handler
attr_reader :mail, :mail_key def self.for(mail, mail_key)
[CreateNoteHandler, CreateIssueHandler].find do |klass|
def initialize(mail, mail_key) handler = klass.new(mail, mail_key)
@mail = mail break handler if handler.can_handle?
@mail_key = mail_key end
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 end
......
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
require 'gitlab/email/handler' require 'gitlab/email/handler/base_handler'
module Gitlab module Gitlab
module Email module Email
class Handler module Handler
class CreateIssue < Handler class CreateIssueHandler < BaseHandler
def can_handle? def can_handle?
!!project !!project
end end
......
require 'gitlab/email/handler' require 'gitlab/email/handler/base_handler'
module Gitlab module Gitlab
module Email module Email
class Handler module Handler
class CreateNote < Handler class CreateNoteHandler < BaseHandler
def can_handle? def can_handle?
!!sent_notification !!sent_notification
end end
......
require 'gitlab/email/handler/create_note' require 'gitlab/email/handler'
require 'gitlab/email/handler/create_issue'
# Inspired in great part by Discourse's Email::Receiver # Inspired in great part by Discourse's Email::Receiver
module Gitlab module Gitlab
...@@ -31,7 +30,7 @@ module Gitlab ...@@ -31,7 +30,7 @@ module Gitlab
raise SentNotificationNotFoundError unless mail_key raise SentNotificationNotFoundError unless mail_key
if handler = find_handler(mail, mail_key) if handler = Handler.for(mail, mail_key)
handler.execute handler.execute
elsif mail_key =~ %r{/|\+} elsif mail_key =~ %r{/|\+}
# Sent Notification mail_key would not have / or + # Sent Notification mail_key would not have / or +
...@@ -65,13 +64,6 @@ module Gitlab ...@@ -65,13 +64,6 @@ module Gitlab
break key if key break key if key
end end
end end
def find_handler(mail, mail_key)
[Handler::CreateNote, Handler::CreateIssue].find do |klass|
handler = klass.new(mail, mail_key)
break handler if handler.can_handle?
end
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment