chore: Disable fetching new emails after mailbox error (#4176)

- Disabled email fetch job if credentials for the channel isn't working
- notify customers when the email channel isn't working

fixes: https://github.com/chatwoot/chatwoot/issues/4174
This commit is contained in:
Sojan Jose
2022-03-22 12:14:17 +05:30
committed by GitHub
parent 715400f7ab
commit 467f3b9191
8 changed files with 48 additions and 8 deletions

View File

@@ -1,9 +1,28 @@
require 'net/imap'
class Inboxes::FetchImapEmailsJob < ApplicationJob
queue_as :low
def perform(channel)
return unless channel.imap_enabled?
return unless should_fetch_email?(channel)
process_mail_for_channel(channel)
rescue Errno::ECONNREFUSED, Net::OpenTimeout, Net::IMAP::NoResponseError
channel.authorization_error!
rescue StandardError => e
channel.authorization_error!
Sentry.capture_exception(e)
end
private
def should_fetch_email?(channel)
channel.imap_enabled? && !channel.reauthorization_required?
end
def process_mail_for_channel(channel)
# TODO: rather than setting this as default method for all mail objects, lets if can do new mail object
# using Mail.retriever_method.new(params)
Mail.defaults do
retriever_method :imap, address: channel.imap_address,
port: channel.imap_port,
@@ -21,6 +40,6 @@ class Inboxes::FetchImapEmailsJob < ApplicationJob
end
end
Channel::Email.update(channel.id, imap_inbox_synced_at: Time.now.utc) if new_mails
channel.update(imap_inbox_synced_at: Time.now.utc) if new_mails
end
end