chore: Mark Facebook accounts for reconnection (#2405)

fixes: #2037
This commit is contained in:
Sojan Jose
2021-08-01 18:15:39 +05:30
committed by GitHub
parent 2890339734
commit e46aa1aa64
9 changed files with 112 additions and 22 deletions
@@ -17,10 +17,15 @@ class Messages::Facebook::MessageBuilder
end
def perform
# This channel might require reauthorization, may be owner might have changed the fb password
return if @inbox.channel.reauthorization_required?
ActiveRecord::Base.transaction do
build_contact
build_message
end
rescue Koala::Facebook::AuthenticationError
Rails.logger.info "Facebook Authorization expired for Inbox #{@inbox.id}"
rescue StandardError => e
Raven.capture_exception(e)
true
@@ -136,6 +141,9 @@ class Messages::Facebook::MessageBuilder
begin
k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook?
result = k.get_object(@sender_id) || {}
rescue Koala::Facebook::AuthenticationError
@inbox.channel.authorization_error!
raise
rescue StandardError => e
result = {}
Raven.capture_exception(e)
@@ -1,10 +1,23 @@
class AdministratorNotifications::ChannelNotificationsMailer < ApplicationMailer
def slack_disconnect(account)
def slack_disconnect
return unless smtp_config_set_or_development?
emails = account.administrators.pluck(:email)
subject = 'Your Slack integration has expired'
@action_url = "#{ENV['FRONTEND_URL']}/app/accounts/#{account.id}/settings/integrations/slack"
send_mail_with_liquid(to: emails, subject: subject) and return
@action_url = "#{ENV['FRONTEND_URL']}/app/accounts/#{Current.account.id}/settings/integrations/slack"
send_mail_with_liquid(to: admin_emails, subject: subject) and return
end
def facebook_disconnect(inbox)
return unless smtp_config_set_or_development?
subject = 'Your Facebook page connection has expired'
@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
Current.account.administrators.pluck(:email)
end
end
+6 -2
View File
@@ -36,9 +36,13 @@ module Reauthorizable
# could used to manually prompt reauthorization if auth scope changes
def prompt_reauthorization!
::Redis::Alfred.set(reauthorization_required_key, true)
return unless (is_a? Integrations::Hook) && slack?
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).slack_disconnect(account)&.deliver_later
if (is_a? Integrations::Hook) && slack?
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).slack_disconnect.deliver_later
end
return unless is_a? Channel::FacebookPage
AdministratorNotifications::ChannelNotificationsMailer.with(account: account).facebook_disconnect(inbox).deliver_later
end
# call this after you successfully Reauthorized the object in UI
@@ -0,0 +1,8 @@
<p>Hello,</p>
<p>Your Facebook Inbox Access has expired. </p>
<p>Please reconnect Facebook Page to continue receiving messages in Chatwoot</p>
<p>
Click <a href="{{action_url}}">here</a> to re-connect.
</p>