/app/workers/email_receiver_worker.rb

https://gitlab.com/artofhuman/gitlab-ce · Ruby · 54 lines · 45 code · 8 blank · 1 comment · 1 complexity · b2e038b41c5ed15726421daf22d53da8 MD5 · raw file

  1. # frozen_string_literal: true
  2. class EmailReceiverWorker
  3. include ApplicationWorker
  4. def perform(raw)
  5. return unless Gitlab::IncomingEmail.enabled?
  6. begin
  7. Gitlab::Email::Receiver.new(raw).execute
  8. rescue => e
  9. handle_failure(raw, e)
  10. end
  11. end
  12. private
  13. def handle_failure(raw, error)
  14. Rails.logger.warn("Email can not be processed: #{error}\n\n#{raw}")
  15. return unless raw.present?
  16. can_retry = false
  17. reason =
  18. case error
  19. when Gitlab::Email::UnknownIncomingEmail
  20. "We couldn't figure out what the email is for. Please create your issue or comment through the web interface."
  21. when Gitlab::Email::SentNotificationNotFoundError
  22. "We couldn't figure out what the email is in reply to. Please create your comment through the web interface."
  23. when Gitlab::Email::ProjectNotFound
  24. "We couldn't find the project. Please check if there's any typo."
  25. when Gitlab::Email::EmptyEmailError
  26. can_retry = true
  27. "It appears that the email is blank. Make sure your reply is at the top of the email, we can't process inline replies."
  28. when Gitlab::Email::UserNotFoundError
  29. "We couldn't figure out what user corresponds to the email. Please create your comment through the web interface."
  30. when Gitlab::Email::UserBlockedError
  31. "Your account has been blocked. If you believe this is in error, contact a staff member."
  32. when Gitlab::Email::UserNotAuthorizedError
  33. "You are not allowed to perform this action. If you believe this is in error, contact a staff member."
  34. when Gitlab::Email::NoteableNotFoundError
  35. "The thread you are replying to no longer exists, perhaps it was deleted? If you believe this is in error, contact a staff member."
  36. when Gitlab::Email::InvalidAttachment
  37. error.message
  38. when Gitlab::Email::InvalidRecordError
  39. can_retry = true
  40. error.message
  41. end
  42. if reason
  43. EmailRejectionMailer.rejection(reason, raw, can_retry).deliver_later
  44. end
  45. end
  46. end