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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 8 deletions

View file

@ -48,7 +48,10 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
# Inbox update doesn't necessarily need channel attributes
return if permitted_params(channel_attributes)[:channel].blank?
validate_email_channel(channel_attributes) if @inbox.inbox_type == 'Email'
if @inbox.inbox_type == 'Email'
validate_email_channel(channel_attributes)
@inbox.channel.reauthorized!
end
@inbox.channel.update!(permitted_params(channel_attributes)[:channel])
update_channel_feature_flags

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

View file

@ -5,7 +5,7 @@ module MailboxHelper
@message = @conversation.messages.create(
account_id: @conversation.account_id,
sender: @conversation.contact,
content: mail_content,
content: mail_content&.truncate(150_000),
inbox_id: @conversation.inbox_id,
message_type: 'incoming',
content_type: 'incoming_email',

View file

@ -15,6 +15,14 @@ class AdministratorNotifications::ChannelNotificationsMailer < ApplicationMailer
send_mail_with_liquid(to: admin_emails, subject: subject) and return
end
def email_disconnect(inbox)
return unless smtp_config_set_or_development?
subject = 'Your email inbox has been disconnected. Please update the credentials for SMTP/IMAP'
@action_url = "#{ENV['FRONTEND_URL']}/app/accounts/#{Current.account.id}/settings/inboxes/#{inbox.id}"
send_mail_with_liquid(to: admin_emails, subject: subject) and return
end
private
def admin_emails

View file

@ -34,6 +34,7 @@
class Channel::Email < ApplicationRecord
include Channelable
include Reauthorizable
self.table_name = 'channel_email'
EDITABLE_ATTRS = [:email, :imap_enabled, :imap_email, :imap_password, :imap_address, :imap_port, :imap_enable_ssl, :imap_inbox_synced_at,

View file

@ -39,10 +39,11 @@ module Reauthorizable
if (is_a? Integrations::Hook) && slack?
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).slack_disconnect.deliver_later
end
return unless is_a? Channel::FacebookPage
elsif is_a? Channel::FacebookPage
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).facebook_disconnect(inbox).deliver_later
elsif is_a? Channel::Email
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).email_disconnect(inbox).deliver_later
end
end
# call this after you successfully Reauthorized the object in UI

View file

@ -0,0 +1,8 @@
<p>Hello,</p>
<p>Your email inbox has been disconnected due to configuration errors. </p>
<p>Please update it to continue receiving messages.</p>
<p>
Click <a href="{{action_url}}">here</a> to re-connect.
</p>

View file

@ -1,7 +1,7 @@
<p>Hello,</p>
<p>Your Facebook Inbox Access has expired. </p>
<p>Please reconnect Facebook Page to continue receiving messages in Chatwoot</p>
<p>Please reconnect Facebook Page to continue receiving messages.</p>
<p>
Click <a href="{{action_url}}">here</a> to re-connect.