Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2016-05-19 01:19:25 +0300
committerLin Jen-Shin <godfat@godfat.org>2016-05-19 01:25:45 +0300
commitc337e748d385fea5c768a8e7de55975dca7fa484 (patch)
treef9646fbb68614b633964bc43d725296b61047fec /lib/gitlab/email/handler.rb
parent3f4a6412dcc35c182d993cd1350459e8a4a1b8d1 (diff)
so we use separate classes to handle different tasks
Diffstat (limited to 'lib/gitlab/email/handler.rb')
-rw-r--r--lib/gitlab/email/handler.rb55
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