BigW Consortium Gitlab

email_receiver_worker.rb 1.97 KB
Newer Older
Douwe Maan committed
1 2 3 4 5 6
class EmailReceiverWorker
  include Sidekiq::Worker

  sidekiq_options queue: :incoming_email

  def perform(raw)
7
    return unless Gitlab::IncomingEmail.enabled?
Douwe Maan committed
8

9
    begin
10
      Gitlab::Email::Receiver.new(raw).execute
11 12 13
    rescue => e
      handle_failure(raw, e)
    end
Douwe Maan committed
14 15 16 17 18 19
  end

  private

  def handle_failure(raw, e)
    Rails.logger.warn("Email can not be processed: #{e}\n\n#{raw}")
20

21 22
    return unless raw.present?

23 24 25 26
    can_retry = false
    reason = nil

    case e
27
    when Gitlab::Email::Receiver::SentNotificationNotFoundError
28
      reason = "We couldn't figure out what the email is in reply to. Please create your comment through the web interface."
29
    when Gitlab::Email::Receiver::EmptyEmailError
30 31
      can_retry = true
      reason = "It appears that the email is blank. Make sure your reply is at the top of the email, we can't process inline replies."
32
    when Gitlab::Email::Receiver::AutoGeneratedEmailError
33
      reason = "The email was marked as 'auto generated', which we can't accept. Please create your comment through the web interface."
34
    when Gitlab::Email::Receiver::UserNotFoundError
35
      reason = "We couldn't figure out what user corresponds to the email. Please create your comment through the web interface."
36 37
    when Gitlab::Email::Receiver::UserBlockedError
      reason = "Your account has been blocked. If you believe this is in error, contact a staff member."
38
    when Gitlab::Email::Receiver::UserNotAuthorizedError
39
      reason = "You are not allowed to respond to the thread you are replying to. If you believe this is in error, contact a staff member."
40
    when Gitlab::Email::Receiver::NoteableNotFoundError
41
      reason = "The thread you are replying to no longer exists, perhaps it was deleted? If you believe this is in error, contact a staff member."
42
    when Gitlab::Email::Receiver::InvalidNoteError
43 44 45 46 47 48
      can_retry = true
      reason = e.message
    else
      return
    end

Valery Sizov committed
49
    EmailRejectionMailer.rejection(reason, raw, can_retry).deliver_later
Douwe Maan committed
50 51
  end
end