Feature: Create conversations from Tweets (#470)

* Feature: Add tweets to conversations
This commit is contained in:
Pranav Raj S
2020-02-09 15:47:48 +05:30
committed by GitHub
parent 3465ebefd1
commit 272c481464
29 changed files with 333 additions and 68 deletions

View File

@@ -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' => '<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
'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

View File

@@ -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

View File

@@ -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

View File

@@ -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