Chore: Twitter Integration house cleaning (#855)

Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
Sojan Jose
2020-05-14 22:51:07 +05:30
committed by GitHub
parent 1108446974
commit 20f39caa42
11 changed files with 159 additions and 63 deletions

View File

@@ -14,6 +14,18 @@ class ContactBuilder
@account ||= inbox.account
end
def create_contact_inbox(contact)
::ContactInbox.create!(
contact_id: contact.id,
inbox_id: inbox.id,
source_id: source_id
)
end
def update_contact_avatar(contact)
::ContactAvatarJob.perform_later(contact, contact_attributes[:avatar_url]) if contact_attributes[:avatar_url]
end
def build_contact
ActiveRecord::Base.transaction do
contact = account.contacts.create!(
@@ -23,16 +35,12 @@ class ContactBuilder
identifier: contact_attributes[:identifier],
additional_attributes: contact_attributes[:additional_attributes]
)
contact_inbox = ::ContactInbox.create!(
contact_id: contact.id,
inbox_id: inbox.id,
source_id: source_id
)
::ContactAvatarJob.perform_later(contact, contact_attributes[:avatar_url]) if contact_attributes[:avatar_url]
contact_inbox = create_contact_inbox(contact)
update_contact_avatar(contact)
contact_inbox
rescue StandardError => e
Rails.logger e
Rails.logger.info e
end
end
end

View File

@@ -14,7 +14,7 @@ class Api::V1::Accounts::CallbacksController < Api::BaseController
@facebook_inbox = current_account.inboxes.create!(name: inbox_name, channel: facebook_channel)
set_avatar(@facebook_inbox, page_id)
rescue StandardError => e
Rails.logger e
Rails.logger.info e
end
end
@@ -62,7 +62,7 @@ class Api::V1::Accounts::CallbacksController < Api::BaseController
koala = Koala::Facebook::OAuth.new(ENV['FB_APP_ID'], ENV['FB_APP_SECRET'])
koala.exchange_access_token_info(omniauth_token)['access_token']
rescue StandardError => e
Rails.logger e
Rails.logger.info e
end
def mark_already_existing_facebook_pages(data)

View File

@@ -2,18 +2,18 @@ class Twitter::CallbacksController < Twitter::BaseController
def show
return redirect_to twitter_app_redirect_url if permitted_params[:denied]
@response = twitter_client.access_token(
oauth_token: permitted_params[:oauth_token],
oauth_verifier: permitted_params[:oauth_verifier]
)
if @response.status == '200'
inbox = build_inbox
@response = ensure_access_token
return redirect_to twitter_app_redirect_url if @response.status != '200'
ActiveRecord::Base.transaction do
inbox = create_inbox
::Redis::Alfred.delete(permitted_params[:oauth_token])
::Twitter::WebhookSubscribeService.new(inbox_id: inbox.id).perform
redirect_to app_twitter_inbox_agents_url(account_id: account.id, inbox_id: inbox.id)
else
redirect_to twitter_app_redirect_url
end
rescue StandardError => e
Rails.logger.info e
redirect_to twitter_app_redirect_url
end
private
@@ -34,20 +34,23 @@ class Twitter::CallbacksController < Twitter::BaseController
app_new_twitter_inbox_url(account_id: account.id)
end
def build_inbox
ActiveRecord::Base.transaction do
twitter_profile = account.twitter_profiles.create(
twitter_access_token: parsed_body['oauth_token'],
twitter_access_token_secret: parsed_body['oauth_token_secret'],
profile_id: parsed_body['user_id']
)
account.inboxes.create(
name: parsed_body['screen_name'],
channel: twitter_profile
)
rescue StandardError => e
Rails.logger e
end
def ensure_access_token
twitter_client.access_token(
oauth_token: permitted_params[:oauth_token],
oauth_verifier: permitted_params[:oauth_verifier]
)
end
def create_inbox
twitter_profile = account.twitter_profiles.create(
twitter_access_token: parsed_body['oauth_token'],
twitter_access_token_secret: parsed_body['oauth_token_secret'],
profile_id: parsed_body['user_id']
)
account.inboxes.create(
name: parsed_body['screen_name'],
channel: twitter_profile
)
end
def permitted_params

View File

@@ -35,7 +35,7 @@ class Channel::TwitterProfile < ApplicationRecord
source_id: profile_id
)
rescue StandardError => e
Rails.logger e
Rails.logger.info e
end
end
@@ -53,7 +53,10 @@ class Channel::TwitterProfile < ApplicationRecord
private
def unsubscribe
webhooks_list = twitter_client.fetch_webhooks.body
twitter_client.unsubscribe_webhook(id: webhooks_list.first['id']) if webhooks_list.present?
### Fix unsubscription with new endpoint
unsubscribe_response = twitter_client.remove_subscription(user_id: profile_id)
Rails.logger.info 'TWITTER_UNSUBSCRIBE: ' + unsubscribe_response.body.to_s
rescue StandardError => e
Rails.logger.info e
end
end

View File

@@ -55,7 +55,7 @@ class Channel::WebWidget < ApplicationRecord
)
contact_inbox
rescue StandardError => e
Rails.logger e
Rails.logger.info e
end
end
end

View File

@@ -47,7 +47,7 @@ class Twitter::SendReplyService
tweet_data = response.body
message.update!(source_id: tweet_data['id_str'])
else
Rails.logger 'TWITTER_TWEET_REPLY_ERROR' + response.body
Rails.logger.info 'TWITTER_TWEET_REPLY_ERROR' + response.body
end
end

View File

@@ -4,9 +4,13 @@ class Twitter::WebhookSubscribeService
pattr_initialize [:inbox_id]
def perform
register_response = twitter_client.register_webhook(url: webhooks_twitter_url(protocol: 'https'))
twitter_client.subscribe_webhook if register_response.status == '200'
Rails.logger.info 'TWITTER_REGISTER_WEBHOOK_FAILURE: ' + register_response.body.to_s
ensure_webhook
unless subscription?
subscribe_response = twitter_client.create_subscription
raise StandardError, 'Twitter Subscription Failed' unless subscribe_response.status == '204'
end
true
end
private
@@ -17,4 +21,37 @@ class Twitter::WebhookSubscribeService
def inbox
Inbox.find_by!(id: inbox_id)
end
def twitter_url
webhooks_twitter_url(protocol: 'https')
end
def ensure_webhook
webhooks = fetch_webhooks
return true if webhooks&.first&.try(:[], 'url') == twitter_url
# twitter supports only one webhook url per environment
# so we will delete the existing one if its not chatwoot
unregister_webhook(webhooks.first) if webhooks&.first
register_webhook
end
def unregister_webhook(webhook)
unregister_response = twitter_client.unregister_webhook(id: webhook.try(:[], 'id'))
Rails.logger.info 'TWITTER_UNREGISTER_WEBHOOK: ' + unregister_response.body.to_s
end
def register_webhook
register_response = twitter_client.register_webhook(url: twitter_url)
Rails.logger.info 'TWITTER_UNREGISTER_WEBHOOK: ' + register_response.body.to_s
end
def subscription?
response = twitter_client.fetch_subscriptions
response.status == '204'
end
def fetch_webhooks
twitter_client.fetch_webhooks.body
end
end