diff --git a/Gemfile b/Gemfile index f7eaba34a..7a61c39fe 100644 --- a/Gemfile +++ b/Gemfile @@ -87,7 +87,7 @@ gem 'line-bot-api' gem 'twilio-ruby', '~> 5.66' # twitty will handle subscription of twitter account events # gem 'twitty', git: 'https://github.com/chatwoot/twitty' -gem 'twitty' +gem 'twitty', '~> 0.1.5' # facebook client gem 'koala' # slack client diff --git a/Gemfile.lock b/Gemfile.lock index ad8eca0b3..3fa0dddca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -717,7 +717,7 @@ GEM faraday (>= 0.9, < 3.0) jwt (>= 1.5, <= 2.5) nokogiri (>= 1.6, < 2.0) - twitty (0.1.4) + twitty (0.1.5) oauth tzinfo (2.0.6) concurrent-ruby (~> 1.0) @@ -883,7 +883,7 @@ DEPENDENCIES test-prof time_diff twilio-ruby (~> 5.66) - twitty + twitty (~> 0.1.5) tzinfo-data uglifier valid_email2 diff --git a/app/controllers/twitter/callbacks_controller.rb b/app/controllers/twitter/callbacks_controller.rb index 0c2dc8263..e92b89d1b 100644 --- a/app/controllers/twitter/callbacks_controller.rb +++ b/app/controllers/twitter/callbacks_controller.rb @@ -49,10 +49,22 @@ class Twitter::CallbacksController < Twitter::BaseController twitter_access_token_secret: parsed_body['oauth_token_secret'], profile_id: parsed_body['user_id'] ) - account.inboxes.create!( + inbox = account.inboxes.create!( name: parsed_body['screen_name'], channel: twitter_profile ) + save_profile_image(inbox) + inbox + end + + def save_profile_image(inbox) + response = twitter_client.user_show(screen_name: inbox.name) + + return unless response.success? + + parsed_user_profile = JSON.parse(response.read_body) + + ::Avatar::AvatarFromUrlJob.perform_later(inbox, parsed_user_profile['profile_image_url_https']) end def permitted_params diff --git a/spec/controllers/twitter/callbacks_controller_spec.rb b/spec/controllers/twitter/callbacks_controller_spec.rb index 12a73953b..3ca4a46f6 100644 --- a/spec/controllers/twitter/callbacks_controller_spec.rb +++ b/spec/controllers/twitter/callbacks_controller_spec.rb @@ -4,6 +4,13 @@ RSpec.describe 'Twitter::CallbacksController', type: :request do let(:twitter_client) { instance_double(::Twitty::Facade) } let(:twitter_response) { instance_double(::Twitty::Response, status: '200', body: { message: 'Valid' }) } let(:raw_response) { double } + let(:user_object_rsponse) do + OpenStruct.new( + read_body: '{"profile_background_color":"000000","profile_background_image_url":"http:\\/\\/abs.twimg.com\\/images\\/themes\\/theme1\\/bg.png"}', + status: 200, + success?: true + ) + end let(:account) { create(:account) } let(:webhook_service) { double } @@ -14,12 +21,15 @@ RSpec.describe 'Twitter::CallbacksController', type: :request do allow(twitter_client).to receive(:access_token).and_return(twitter_response) allow(twitter_response).to receive(:raw_response).and_return(raw_response) allow(raw_response).to receive(:body).and_return('oauth_token=1&oauth_token_secret=1&user_id=100&screen_name=chatwoot') + allow(twitter_client).to receive(:user_show).and_return(user_object_rsponse) + allow(JSON).to receive(:parse).and_return(user_object_rsponse) allow(::Twitter::WebhookSubscribeService).to receive(:new).and_return(webhook_service) end describe 'GET /twitter/callback' do it 'creates inboxes if subscription is successful' do allow(webhook_service).to receive(:perform).and_return true + expect(Avatar::AvatarFromUrlJob).to receive(:perform_later).once get twitter_callback_url account.reload expect(response).to redirect_to app_twitter_inbox_agents_url(account_id: account.id, inbox_id: account.inboxes.last.id)