2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::CallbacksController < Api::V1::Accounts::BaseController
|
2020-02-26 12:52:08 +00:00
|
|
|
before_action :inbox, only: [:reauthorize_page]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def register_facebook_page
|
|
|
|
user_access_token = params[:user_access_token]
|
|
|
|
page_access_token = params[:page_access_token]
|
|
|
|
page_id = params[:page_id]
|
|
|
|
inbox_name = params[:inbox_name]
|
2020-04-19 18:10:28 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2020-06-07 08:28:05 +00:00
|
|
|
facebook_channel = Current.account.facebook_pages.create!(
|
2020-04-19 18:10:28 +00:00
|
|
|
page_id: page_id, user_access_token: user_access_token,
|
|
|
|
page_access_token: page_access_token
|
|
|
|
)
|
2020-06-07 08:28:05 +00:00
|
|
|
@facebook_inbox = Current.account.inboxes.create!(name: inbox_name, channel: facebook_channel)
|
2021-10-05 09:05:32 +00:00
|
|
|
set_instagram_id(page_access_token, facebook_channel)
|
2020-04-19 18:10:28 +00:00
|
|
|
set_avatar(@facebook_inbox, page_id)
|
|
|
|
rescue StandardError => e
|
2020-05-14 17:21:07 +00:00
|
|
|
Rails.logger.info e
|
2020-04-19 18:10:28 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
def facebook_pages
|
2019-10-20 08:47:26 +00:00
|
|
|
@page_details = mark_already_existing_facebook_pages(fb_object.get_connections('me', 'accounts'))
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2021-10-05 09:05:32 +00:00
|
|
|
def set_instagram_id(page_access_token, facebook_channel)
|
|
|
|
fb_object = Koala::Facebook::API.new(page_access_token)
|
|
|
|
response = fb_object.get_connections('me', '', { fields: 'instagram_business_account' })
|
|
|
|
return if response['instagram_business_account'].blank?
|
|
|
|
|
|
|
|
instagram_id = response['instagram_business_account']['id']
|
|
|
|
facebook_channel.update(instagram_id: instagram_id)
|
|
|
|
end
|
|
|
|
|
2020-06-07 08:28:05 +00:00
|
|
|
# get params[:inbox_id], current_account. params[:omniauth_token]
|
2019-12-12 21:12:16 +00:00
|
|
|
def reauthorize_page
|
2020-02-26 12:52:08 +00:00
|
|
|
if @inbox&.facebook?
|
2019-12-12 21:12:16 +00:00
|
|
|
fb_page_id = @inbox.channel.page_id
|
2019-10-20 08:47:26 +00:00
|
|
|
page_details = fb_object.get_connections('me', 'accounts')
|
2019-12-12 21:12:16 +00:00
|
|
|
|
2020-02-26 12:52:08 +00:00
|
|
|
if (page_detail = (page_details || []).detect { |page| fb_page_id == page['id'] })
|
|
|
|
update_fb_page(fb_page_id, page_detail['access_token'])
|
2020-09-29 19:42:32 +00:00
|
|
|
render and return
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
2019-12-12 21:12:16 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-12 21:12:16 +00:00
|
|
|
def inbox
|
2020-06-07 08:28:05 +00:00
|
|
|
@inbox = Current.account.inboxes.find_by(id: params[:inbox_id])
|
2019-12-12 21:12:16 +00:00
|
|
|
end
|
|
|
|
|
2020-02-26 12:52:08 +00:00
|
|
|
def update_fb_page(fb_page_id, access_token)
|
2020-09-29 19:42:32 +00:00
|
|
|
fb_page = get_fb_page(fb_page_id)
|
|
|
|
fb_page&.update!(user_access_token: @user_access_token, page_access_token: access_token)
|
|
|
|
fb_page&.reauthorized!
|
2019-12-12 21:12:16 +00:00
|
|
|
end
|
|
|
|
|
2020-02-26 12:52:08 +00:00
|
|
|
def get_fb_page(fb_page_id)
|
2020-06-07 08:28:05 +00:00
|
|
|
Current.account.facebook_pages.find_by(page_id: fb_page_id)
|
2019-12-12 21:12:16 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def fb_object
|
|
|
|
@user_access_token = long_lived_token(params[:omniauth_token])
|
|
|
|
Koala::Facebook::API.new(@user_access_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def long_lived_token(omniauth_token)
|
2019-11-23 19:57:39 +00:00
|
|
|
koala = Koala::Facebook::OAuth.new(ENV['FB_APP_ID'], ENV['FB_APP_SECRET'])
|
2020-02-26 12:52:08 +00:00
|
|
|
koala.exchange_access_token_info(omniauth_token)['access_token']
|
2020-03-28 06:13:02 +00:00
|
|
|
rescue StandardError => e
|
2020-05-14 17:21:07 +00:00
|
|
|
Rails.logger.info e
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mark_already_existing_facebook_pages(data)
|
|
|
|
return [] if data.empty?
|
2019-10-20 08:47:26 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
data.inject([]) do |result, page_detail|
|
2021-08-03 14:41:52 +00:00
|
|
|
page_detail[:exists] = Current.account.facebook_pages.exists?(page_id: page_detail['id'])
|
2019-08-14 09:48:44 +00:00
|
|
|
result << page_detail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-19 18:10:28 +00:00
|
|
|
def set_avatar(facebook_inbox, page_id)
|
2021-08-11 11:10:28 +00:00
|
|
|
avatar_file = Down.download(
|
|
|
|
"http://graph.facebook.com/#{page_id}/picture?type=large"
|
|
|
|
)
|
|
|
|
facebook_inbox.avatar.attach(io: avatar_file, filename: avatar_file.original_filename, content_type: avatar_file.content_type)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|