diff --git a/.rubocop.yml b/.rubocop.yml index a21f6eada..09b36e576 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -20,9 +20,6 @@ Style/GlobalVars: Exclude: - 'config/initializers/redis.rb' - 'lib/redis/alfred.rb' - - 'app/controllers/api/v1/webhooks_controller.rb' - - 'app/services/twitter/send_reply_service.rb' - - 'spec/services/twitter/send_reply_service_spec.rb' Metrics/BlockLength: Exclude: - spec/**/* diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index 8cad3f87b..6d3a7d39b 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -2,7 +2,7 @@ require 'open-uri' # This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo` # Assumptions -# 1. Incase of an outgoing message which is echo, fb_id will NOT be nil, +# 1. Incase of an outgoing message which is echo, source_id will NOT be nil, # based on this we are showing "not sent from chatwoot" message in frontend # Hence there is no need to set user_id in message for outgoing echo messages. @@ -121,7 +121,7 @@ class Messages::MessageBuilder inbox_id: conversation.inbox_id, message_type: @message_type, content: response.content, - fb_id: response.identifier + source_id: response.identifier } end diff --git a/app/builders/messages/outgoing/normal_builder.rb b/app/builders/messages/outgoing/normal_builder.rb index 8517f4504..b75e8f84b 100644 --- a/app/builders/messages/outgoing/normal_builder.rb +++ b/app/builders/messages/outgoing/normal_builder.rb @@ -23,7 +23,7 @@ class Messages::Outgoing::NormalBuilder content: @content, private: @private, user_id: @user.id, - fb_id: @fb_id + source_id: @fb_id } end end diff --git a/app/controllers/api/v1/webhooks_controller.rb b/app/controllers/api/v1/webhooks_controller.rb index 10bcfc25a..49ee1ec92 100644 --- a/app/controllers/api/v1/webhooks_controller.rb +++ b/app/controllers/api/v1/webhooks_controller.rb @@ -13,7 +13,7 @@ class Api::V1::WebhooksController < ApplicationController end def twitter_crc - render json: { response_token: "sha256=#{$twitter.generate_crc(params[:crc_token])}" } + render json: { response_token: "sha256=#{twitter_client.generate_crc(params[:crc_token])}" } end def twitter_events @@ -26,6 +26,12 @@ class Api::V1::WebhooksController < ApplicationController private + def twitter_client + Twitty::Facade.new do |config| + config.consumer_secret = ENV.fetch('TWITTER_CONSUMER_SECRET', nil) + end + end + def login_from_basic_auth authenticate_or_request_with_http_basic do |username, password| username == ENV['CHARGEBEE_WEBHOOK_USERNAME'] && password == ENV['CHARGEBEE_WEBHOOK_PASSWORD'] diff --git a/app/javascript/dashboard/components/widgets/Thumbnail.vue b/app/javascript/dashboard/components/widgets/Thumbnail.vue index dff9f0f77..b46e9395b 100644 --- a/app/javascript/dashboard/components/widgets/Thumbnail.vue +++ b/app/javascript/dashboard/components/widgets/Thumbnail.vue @@ -16,19 +16,19 @@ :size="avatarSize" />
{{ contact.email }} + +
+ {{ `@${contact.additional_attributes.screen_name}` }} +
{{ contact.location }}
@@ -28,6 +38,15 @@
{{ contact.bio }}
+
+ {{ contact.additional_attributes.description }} +
contacts.id) # class Message < ApplicationRecord diff --git a/app/services/facebook/send_reply_service.rb b/app/services/facebook/send_reply_service.rb index 99a67e46b..ab535fd0a 100644 --- a/app/services/facebook/send_reply_service.rb +++ b/app/services/facebook/send_reply_service.rb @@ -22,8 +22,8 @@ class Facebook::SendReplyService end def outgoing_message_from_chatwoot? - # messages sent directly from chatwoot won't have fb_id. - message.outgoing? && !message.fb_id + # messages sent directly from chatwoot won't have source_id. + message.outgoing? && !message.source_id end # def reopen_lock diff --git a/app/services/twitter/send_reply_service.rb b/app/services/twitter/send_reply_service.rb index 5acab07b6..8c103c07f 100644 --- a/app/services/twitter/send_reply_service.rb +++ b/app/services/twitter/send_reply_service.rb @@ -3,22 +3,60 @@ class Twitter::SendReplyService def perform return if message.private + return if message.source_id return if inbox.channel.class.to_s != 'Channel::TwitterProfile' return unless outgoing_message_from_chatwoot? - $twitter.send_direct_message( + send_reply + end + + private + + def twitter_client + Twitty::Facade.new do |config| + config.consumer_key = ENV.fetch('TWITTER_CONSUMER_KEY', nil) + config.consumer_secret = ENV.fetch('TWITTER_CONSUMER_SECRET', nil) + config.access_token = channel.twitter_access_token + config.access_token_secret = channel.twitter_access_token_secret + config.base_url = 'https://api.twitter.com' + config.environment = ENV.fetch('TWITTER_ENVIRONMENT', '') + end + end + + def conversation_type + conversation.additional_attributes['type'] + end + + def screen_name + "@#{additional_attributes ? additional_attributes['screen_name'] : ''} " + end + + def send_direct_message + twitter_client.send_direct_message( recipient_id: contact_inbox.source_id, message: message.content ) end - private + def send_tweet_reply + twitter_client.send_tweet_reply( + reply_to_tweet_id: conversation.additional_attributes['tweet_id'], + tweet: screen_name + message.content + ) + end + + def send_reply + conversation_type == 'tweet' ? send_tweet_reply : send_direct_message + end def outgoing_message_from_chatwoot? message.outgoing? end + delegate :additional_attributes, to: :contact + delegate :contact, to: :conversation delegate :contact_inbox, to: :conversation delegate :conversation, to: :message delegate :inbox, to: :conversation + delegate :channel, to: :inbox end diff --git a/app/services/twitter/tweet_parser_service.rb b/app/services/twitter/tweet_parser_service.rb new file mode 100644 index 000000000..79dc13015 --- /dev/null +++ b/app/services/twitter/tweet_parser_service.rb @@ -0,0 +1,69 @@ +class Twitter::TweetParserService < Twitter::WebhooksBaseService + pattr_initialize [:payload] + + def perform + set_inbox + find_or_create_contact(user) + set_conversation + @conversation.messages.create( + account_id: @inbox.account_id, + contact_id: @contact.id, + content: tweet_text, + inbox_id: @inbox.id, + message_type: message_type, + source_id: tweet_data['id'].to_s + ) + end + + private + + def message_type + user['id'] == profile_id ? :outgoing : :incoming + end + + def tweet_text + tweet_data['truncated'] ? tweet_data['extended_tweet']['full_text'] : tweet_data['text'] + end + + def tweet_create_events_params + payload['tweet_create_events'] + end + + def tweet_data + tweet_create_events_params.first + end + + def user + tweet_data['user'] + end + + def parent_tweet_id + tweet_data['in_reply_to_status_id_str'].nil? ? tweet_data['id'].to_s : tweet_data['in_reply_to_status_id_str'] + end + + def conversation_params + { + account_id: @inbox.account_id, + inbox_id: @inbox.id, + contact_id: @contact.id, + contact_inbox_id: @contact_inbox.id, + additional_attributes: { + type: 'tweet', + tweet_id: parent_tweet_id, + tweet_source: tweet_data['source'] + } + } + end + + def set_conversation + tweet_conversations = @contact_inbox.conversations.where("additional_attributes ->> 'tweet_id' = ?", parent_tweet_id) + @conversation = tweet_conversations.first + return if @conversation + + tweet_message = @inbox.messages.find_by(source_id: parent_tweet_id) + @conversation = tweet_message.conversation if tweet_message + return if @conversation + + @conversation = ::Conversation.create!(conversation_params) + end +end diff --git a/app/services/twitter/webhooks_base_service.rb b/app/services/twitter/webhooks_base_service.rb index ffaba327c..2f3d09290 100644 --- a/app/services/twitter/webhooks_base_service.rb +++ b/app/services/twitter/webhooks_base_service.rb @@ -5,6 +5,17 @@ class Twitter::WebhooksBaseService payload[:for_user_id] end + def additional_contact_attributes(user) + { + screen_name: user['screen_name'], + location: user['location'], + url: user['url'], + description: user['description'], + followers_count: user['followers_count'], + friends_count: user['friends_count'] + } + end + def set_inbox twitter_profile = ::Channel::TwitterProfile.find_by(profile_id: profile_id) @inbox = ::Inbox.find_by!(channel: twitter_profile) @@ -15,7 +26,9 @@ class Twitter::WebhooksBaseService @contact = @contact_inbox.contact if @contact_inbox return if @contact - @contact_inbox = @inbox.channel.create_contact_inbox(user['id'], user['name']) + @contact_inbox = @inbox.channel.create_contact_inbox( + user['id'], user['name'], additional_contact_attributes(user) + ) @contact = @contact_inbox.contact avatar_resource = LocalResource.new(user['profile_image_url']) @contact.avatar.attach( diff --git a/app/views/api/v1/contacts/index.json.jbuilder b/app/views/api/v1/contacts/index.json.jbuilder index 98923cde2..519940354 100644 --- a/app/views/api/v1/contacts/index.json.jbuilder +++ b/app/views/api/v1/contacts/index.json.jbuilder @@ -5,5 +5,6 @@ json.payload do json.email contact.email json.phone_number contact.phone_number json.thumbnail contact.avatar.profile_thumb.url + json.additional_attributes contact.additional_attributes end end diff --git a/app/views/api/v1/contacts/show.json.jbuilder b/app/views/api/v1/contacts/show.json.jbuilder index f9b8b19ca..e4cb9b016 100644 --- a/app/views/api/v1/contacts/show.json.jbuilder +++ b/app/views/api/v1/contacts/show.json.jbuilder @@ -5,4 +5,5 @@ json.payload do json.name @contact.name json.phone_number @contact.phone_number json.thumbnail @contact.avatar_url + json.additional_attributes @contact.additional_attributes end diff --git a/app/views/api/v1/contacts/update.json.jbuilder b/app/views/api/v1/contacts/update.json.jbuilder index c7aebe886..c97f7b68c 100644 --- a/app/views/api/v1/contacts/update.json.jbuilder +++ b/app/views/api/v1/contacts/update.json.jbuilder @@ -4,4 +4,5 @@ json.payload do json.email @contact.email json.phone_number @contact.phone_number json.thumbnail @contact.avatar.thumb.url + json.additional_attributes @contact.additional_attributes end diff --git a/app/views/api/v1/conversations/get_messages.json.jbuilder b/app/views/api/v1/conversations/get_messages.json.jbuilder index ccfaf84d4..4fa80ff18 100644 --- a/app/views/api/v1/conversations/get_messages.json.jbuilder +++ b/app/views/api/v1/conversations/get_messages.json.jbuilder @@ -7,8 +7,9 @@ json.payload do json.message_type message.message_type_before_type_cast json.created_at message.created_at.to_i json.private message.private - json.fb_id message.fb_id + json.source_id message.source_id json.attachment message.attachment.push_event_data if message.attachment json.sender message.user.push_event_data if message.user + json.contact message.contact end end diff --git a/app/views/api/v1/conversations/show.json.jbuilder b/app/views/api/v1/conversations/show.json.jbuilder index f1cc09a26..f9b88c36c 100644 --- a/app/views/api/v1/conversations/show.json.jbuilder +++ b/app/views/api/v1/conversations/show.json.jbuilder @@ -13,7 +13,7 @@ json.payload do json.message_type message.message_type_before_type_cast json.created_at message.created_at.to_i json.private message.private - json.fb_id message.fb_id + json.source_id message.source_id json.attachment message.attachment.push_event_data if message.attachment json.sender message.user.push_event_data if message.user end diff --git a/config/initializers/twitter.rb b/config/initializers/twitter.rb deleted file mode 100644 index 9fabcbeb8..000000000 --- a/config/initializers/twitter.rb +++ /dev/null @@ -1,8 +0,0 @@ -$twitter = Twitty::Facade.new do |config| - config.consumer_key = ENV.fetch('TWITTER_CONSUMER_KEY', nil) - config.consumer_secret = ENV.fetch('TWITTER_CONSUMER_SECRET', nil) - config.access_token = ENV.fetch('TWITTER_ACCESS_TOKEN', nil) - config.access_token_secret = ENV.fetch('TWITTER_ACCESS_TOKEN_SECRET', nil) - config.base_url = 'https://api.twitter.com' - config.environment = ENV.fetch('TWITTER_ENVIRONMENT', '') -end diff --git a/db/migrate/20200206180408_add_contact_to_message.rb b/db/migrate/20200206180408_add_contact_to_message.rb new file mode 100644 index 000000000..5b3dc4080 --- /dev/null +++ b/db/migrate/20200206180408_add_contact_to_message.rb @@ -0,0 +1,12 @@ +class AddContactToMessage < ActiveRecord::Migration[6.0] + def change + add_reference(:messages, :contact, foreign_key: true, index: true) + + ::Message.all.each do |message| + conversation = message.conversation + next if conversation.contact.nil? + + message.update!(contact_id: conversation.contact.id) + end + end +end diff --git a/db/migrate/20200206180927_rename_fb_id_to_source_id.rb b/db/migrate/20200206180927_rename_fb_id_to_source_id.rb new file mode 100644 index 000000000..99d7a1eaa --- /dev/null +++ b/db/migrate/20200206180927_rename_fb_id_to_source_id.rb @@ -0,0 +1,7 @@ +class RenameFbIdToSourceId < ActiveRecord::Migration[6.0] + def change + rename_column :messages, :fb_id, :source_id + + add_index(:messages, :source_id) + end +end diff --git a/db/migrate/20200206182948_add_additional_attributes_to_contact.rb b/db/migrate/20200206182948_add_additional_attributes_to_contact.rb new file mode 100644 index 000000000..34a8ca9bf --- /dev/null +++ b/db/migrate/20200206182948_add_additional_attributes_to_contact.rb @@ -0,0 +1,5 @@ +class AddAdditionalAttributesToContact < ActiveRecord::Migration[6.0] + def change + add_column :contacts, :additional_attributes, :jsonb + end +end diff --git a/db/schema.rb b/db/schema.rb index 263952f41..80f268b90 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_01_24_190208) do +ActiveRecord::Schema.define(version: 2020_02_06_182948) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -116,6 +116,7 @@ ActiveRecord::Schema.define(version: 2020_01_24_190208) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "pubsub_token" + t.jsonb "additional_attributes" t.index ["account_id"], name: "index_contacts_on_account_id" t.index ["pubsub_token"], name: "index_contacts_on_pubsub_token", unique: true end @@ -168,10 +169,13 @@ ActiveRecord::Schema.define(version: 2020_01_24_190208) do t.boolean "private", default: false t.integer "user_id" t.integer "status", default: 0 - t.string "fb_id" + t.string "source_id" t.integer "content_type", default: 0 t.json "content_attributes", default: {} + t.bigint "contact_id" + t.index ["contact_id"], name: "index_messages_on_contact_id" t.index ["conversation_id"], name: "index_messages_on_conversation_id" + t.index ["source_id"], name: "index_messages_on_source_id" end create_table "subscriptions", id: :serial, force: :cascade do |t| @@ -200,11 +204,9 @@ ActiveRecord::Schema.define(version: 2020_01_24_190208) do t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context" t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy" t.index ["taggable_id"], name: "index_taggings_on_taggable_id" - t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id" t.index ["taggable_type"], name: "index_taggings_on_taggable_type" t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type" t.index ["tagger_id"], name: "index_taggings_on_tagger_id" - t.index ["tagger_type", "tagger_id"], name: "index_taggings_on_tagger_type_and_tagger_id" end create_table "tags", id: :serial, force: :cascade do |t| @@ -258,5 +260,6 @@ ActiveRecord::Schema.define(version: 2020_01_24_190208) do add_foreign_key "contact_inboxes", "contacts" add_foreign_key "contact_inboxes", "inboxes" add_foreign_key "conversations", "contact_inboxes" + add_foreign_key "messages", "contacts" add_foreign_key "users", "users", column: "inviter_id", on_delete: :nullify end diff --git a/lib/webhooks/twitter.rb b/lib/webhooks/twitter.rb index f7480732e..4ee0badfe 100644 --- a/lib/webhooks/twitter.rb +++ b/lib/webhooks/twitter.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class Webhooks::Twitter - SUPPORTED_EVENTS = [:direct_message_events].freeze + SUPPORTED_EVENTS = [:direct_message_events, :tweet_create_events].freeze attr_accessor :params, :account @@ -22,4 +22,8 @@ class Webhooks::Twitter def direct_message_events ::Twitter::DirectMessageParserService.new(payload: @params).perform end + + def tweet_create_events + ::Twitter::TweetParserService.new(payload: @params).perform + end end diff --git a/spec/factories/twitter/tweet_create_event.rb b/spec/factories/twitter/tweet_create_event.rb new file mode 100644 index 000000000..24ade88c5 --- /dev/null +++ b/spec/factories/twitter/tweet_create_event.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :tweet_create_event, class: Hash do + for_user_id { '1' } + tweet_create_events do + [ + { + 'created_at' => 'Wed Feb 05 08:39:31 +0000 2020', + 'id' => 1, + 'id_str' => '1', + 'text' => '@chatwootapp Testing a sample tweet with Twitter client', + 'source' => 'Twitter for iPhone', + 'truncated' => false, + 'in_reply_to_status_id' => nil, + 'in_reply_to_status_id_str' => nil, + 'in_reply_to_user_id' => 1, + 'in_reply_to_user_id_str' => '1', + 'in_reply_to_screen_name' => 'chatwootapp', + 'user' => { + 'id' => 2, + 'name' => 'SurveyJoy', + 'screen_name' => 'surveyjoyHQ', + 'location' => 'Bangalore', + 'url' => 'https://surveyjoy.co?utm_source=twitter', + 'description' => 'Delightful in-product customer satisfaction surveys', + 'followers_count' => 21, + 'friends_count' => 13, + 'profile_image_url' => 'http://pbs.twimg.com/profile_images/1114792399597985792/iHc5Gmez_normal.png', + 'profile_image_url_https' => 'https://pbs.twimg.com/profile_images/1114792399597985792/iHc5Gmez_normal.png', + 'profile_banner_url' => 'https://pbs.twimg.com/profile_banners/1109349707783041024/1554622013' + }, + 'geo' => nil, + 'coordinates' => nil, + 'place' => nil, + 'contributors' => nil, + 'is_quote_status' => false, + 'quote_count' => 0, + 'reply_count' => 0, + 'retweet_count' => 0, + 'favorite_count' => 0 + } + ] + end + initialize_with { attributes } + end +end diff --git a/spec/lib/webhooks/twitter_spec.rb b/spec/lib/webhooks/twitter_spec.rb index 7803fb156..28220406b 100644 --- a/spec/lib/webhooks/twitter_spec.rb +++ b/spec/lib/webhooks/twitter_spec.rb @@ -2,18 +2,28 @@ require 'rails_helper' require 'webhooks/twitter' describe Webhooks::Twitter do - subject(:process_twitter_event) { described_class.new(params).consume } + subject(:twitter_webhook) { described_class } let!(:account) { create(:account) } # FIX ME: recipient id is set to 1 inside event factories let!(:twitter_channel) { create(:channel_twitter_profile, account: account, profile_id: '1') } let!(:twitter_inbox) { create(:inbox, channel: twitter_channel, account: account) } - let!(:params) { build(:twitter_message_create_event).with_indifferent_access } + let!(:dm_params) { build(:twitter_message_create_event).with_indifferent_access } + let!(:tweet_params) { build(:tweet_create_event).with_indifferent_access } describe '#perform' do - context 'with correct params' do + context 'with direct_message params' do it 'creates incoming message in the twitter inbox' do - process_twitter_event + twitter_webhook.new(dm_params).consume + expect(twitter_inbox.contacts.count).to be 1 + expect(twitter_inbox.conversations.count).to be 1 + expect(twitter_inbox.messages.count).to be 1 + end + end + + context 'with tweet_params params' do + it 'creates incoming message in the twitter inbox' do + twitter_webhook.new(tweet_params).consume expect(twitter_inbox.contacts.count).to be 1 expect(twitter_inbox.conversations.count).to be 1 expect(twitter_inbox.messages.count).to be 1 diff --git a/spec/services/facebook/send_reply_service_spec.rb b/spec/services/facebook/send_reply_service_spec.rb index 4e8bc9097..44f028e8b 100644 --- a/spec/services/facebook/send_reply_service_spec.rb +++ b/spec/services/facebook/send_reply_service_spec.rb @@ -35,7 +35,7 @@ describe Facebook::SendReplyService do end it 'if message has an FB ID' do - create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, fb_id: SecureRandom.uuid) + create(:message, message_type: 'outgoing', inbox: facebook_inbox, account: account, source_id: SecureRandom.uuid) expect(bot).not_to have_received(:deliver) end end diff --git a/spec/services/twitter/send_reply_service_spec.rb b/spec/services/twitter/send_reply_service_spec.rb index 5d265f74b..ff1803710 100644 --- a/spec/services/twitter/send_reply_service_spec.rb +++ b/spec/services/twitter/send_reply_service_spec.rb @@ -3,41 +3,72 @@ require 'rails_helper' describe Twitter::SendReplyService do subject(:send_reply_service) { described_class.new(message: message) } - before do - allow($twitter).to receive(:send_direct_message).and_return(true) + let(:twitter_client) { instance_double(::Twitty::Facade) } + let(:account) { create(:account) } + let(:widget_inbox) { create(:inbox, account: account) } + let(:twitter_channel) { create(:channel_twitter_profile, account: account) } + let(:twitter_inbox) { create(:inbox, channel: twitter_channel, account: account) } + let(:contact) { create(:contact, account: account) } + let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: twitter_inbox) } + let(:dm_conversation) do + create( + :conversation, + contact: contact, + inbox: twitter_inbox, + contact_inbox: contact_inbox, + additional_attributes: { type: 'direct_message' } + ) + end + let(:tweet_conversation) do + create( + :conversation, + contact: contact, + inbox: twitter_inbox, + contact_inbox: contact_inbox, + additional_attributes: { type: 'tweet', tweet_id: '1234' } + ) end - let!(:account) { create(:account) } - let!(:widget_inbox) { create(:inbox, account: account) } - let!(:twitter_channel) { create(:channel_twitter_profile, account: account) } - let!(:twitter_inbox) { create(:inbox, channel: twitter_channel, account: account) } - let!(:contact) { create(:contact, account: account) } - let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: twitter_inbox) } - let(:conversation) { create(:conversation, contact: contact, inbox: twitter_inbox, contact_inbox: contact_inbox) } + before do + allow(::Twitty::Facade).to receive(:new).and_return(twitter_client) + allow(twitter_client).to receive(:send_direct_message).and_return(true) + allow(twitter_client).to receive(:send_tweet_reply).and_return(true) + end describe '#perform' do context 'without reply' do - it 'if message is private' do - create(:message, message_type: 'outgoing', private: true, inbox: twitter_inbox, account: account) - expect($twitter).not_to have_received(:send_direct_message) - end - it 'if inbox channel is not twitter profile' do create(:message, message_type: 'outgoing', inbox: widget_inbox, account: account) - expect($twitter).not_to have_received(:send_direct_message) + expect(twitter_client).not_to have_received(:send_direct_message) + end + + it 'if message is private' do + create(:message, message_type: 'outgoing', private: true, inbox: twitter_inbox, account: account) + expect(twitter_client).not_to have_received(:send_direct_message) + end + + it 'if message has source_id' do + create(:message, message_type: 'outgoing', source_id: '123', inbox: widget_inbox, account: account) + expect(twitter_client).not_to have_received(:send_direct_message) end it 'if message is not outgoing' do create(:message, message_type: 'incoming', inbox: twitter_inbox, account: account) - expect($twitter).not_to have_received(:send_direct_message) + expect(twitter_client).not_to have_received(:send_direct_message) end end context 'with reply' do - it 'if message is sent from chatwoot and is outgoing' do - create(:message, message_type: :incoming, inbox: twitter_inbox, account: account, conversation: conversation) - create(:message, message_type: 'outgoing', inbox: twitter_inbox, account: account, conversation: conversation) - expect($twitter).to have_received(:send_direct_message) + it 'if conversation is a direct message' do + create(:message, message_type: :incoming, inbox: twitter_inbox, account: account, conversation: dm_conversation) + create(:message, message_type: 'outgoing', inbox: twitter_inbox, account: account, conversation: dm_conversation) + expect(twitter_client).to have_received(:send_direct_message) + end + + it 'if conversation is a tweet' do + create(:message, message_type: :incoming, inbox: twitter_inbox, account: account, conversation: tweet_conversation) + create(:message, message_type: 'outgoing', inbox: twitter_inbox, account: account, conversation: tweet_conversation) + expect(twitter_client).to have_received(:send_tweet_reply) end end end