fix: Handling facebook client error for deleted story (#4984)

This commit is contained in:
Tejaswini Chile
2022-07-07 18:02:18 +05:30
committed by GitHub
parent 91d9584fcb
commit 2f3bdbdea2
5 changed files with 40 additions and 6 deletions

View File

@@ -71,6 +71,11 @@ class Messages::Messenger::MessageBuilder
rescue Koala::Facebook::AuthenticationError rescue Koala::Facebook::AuthenticationError
@inbox.channel.authorization_error! @inbox.channel.authorization_error!
raise raise
rescue Koala::Facebook::ClientError => e
# The exception occurs when we are trying fetch the deleted story or blocked story.
@message.update(content: I18n.t('conversations.messages.instagram_deleted_story_content'))
Rails.logger.error e
{}
rescue StandardError => e rescue StandardError => e
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception
{} {}

View File

@@ -100,6 +100,7 @@ en:
conversations: conversations:
messages: messages:
instagram_story_content: "%{story_sender} mentioned you in the story: " instagram_story_content: "%{story_sender} mentioned you in the story: "
instagram_deleted_story_content: This story is no longer available.
deleted: This message was deleted deleted: This message was deleted
activity: activity:
status: status:

View File

@@ -5,12 +5,14 @@ describe ::Messages::Instagram::MessageBuilder do
before do before do
stub_request(:post, /graph.facebook.com/) stub_request(:post, /graph.facebook.com/)
stub_request(:get, 'https://www.example.com/test.jpeg')
end end
let!(:account) { create(:account) } let!(:account) { create(:account) }
let!(:instagram_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') } let!(:instagram_channel) { create(:channel_instagram_fb_page, account: account, instagram_id: 'chatwoot-app-user-id-1') }
let!(:instagram_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) } let!(:instagram_inbox) { create(:inbox, channel: instagram_channel, account: account, greeting_enabled: false) }
let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access } let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access }
let!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access }
let(:fb_object) { double } let(:fb_object) { double }
let(:contact) { create(:contact, id: 'Sender-id-1', name: 'Jane Dae') } let(:contact) { create(:contact, id: 'Sender-id-1', name: 'Jane Dae') }
let(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: instagram_inbox.id, source_id: 'Sender-id-1') } let(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: instagram_inbox.id, source_id: 'Sender-id-1') }
@@ -28,7 +30,7 @@ describe ::Messages::Instagram::MessageBuilder do
) )
messaging = dm_params[:entry][0]['messaging'][0] messaging = dm_params[:entry][0]['messaging'][0]
contact_inbox contact_inbox
instagram_message_builder.new(messaging, instagram_inbox).perform described_class.new(messaging, instagram_inbox).perform
instagram_inbox.reload instagram_inbox.reload
@@ -41,5 +43,32 @@ describe ::Messages::Instagram::MessageBuilder do
expect(contact.name).to eq('Jane Dae') expect(contact.name).to eq('Jane Dae')
expect(message.content).to eq('This is the first message from the customer') expect(message.content).to eq('This is the first message from the customer')
end end
it 'raises exception on deleted story' do
allow(Koala::Facebook::API).to receive(:new).and_return(fb_object)
allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::ClientError.new(
190,
'This Message has been deleted by the user or the business.'
))
messaging = story_mention_params[:entry][0][:messaging][0]
contact_inbox
described_class.new(messaging, instagram_inbox, outgoing_echo: false).perform
instagram_inbox.reload
# we would have contact created, message created but the empty message because the story mention has been deleted later
# As they show it in instagram that this story is no longer available
# and external attachments link would be reachable
expect(instagram_inbox.conversations.count).to be 1
expect(instagram_inbox.messages.count).to be 1
contact = instagram_channel.inbox.contacts.first
message = instagram_channel.inbox.messages.first
expect(contact.name).to eq('Jane Dae')
expect(message.content).to eq('This story is no longer available.')
expect(message.attachments.count).to eq(1)
end
end end
end end

View File

@@ -104,7 +104,7 @@ FactoryBot.define do
{ {
'type': 'share', 'type': 'share',
'payload': { 'payload': {
'url': 'https://imagekit.io/blog/content/images/2020/05/media_library.jpeg' 'url': 'https://www.example.com/test.jpeg'
} }
} }
] ]
@@ -138,7 +138,7 @@ FactoryBot.define do
{ {
'type': 'story_mention', 'type': 'story_mention',
'payload': { 'payload': {
'url': 'https://imagekit.io/blog/content/images/2020/05/media_library.jpeg' 'url': 'https://www.example.com/test.jpeg'
} }
} }
] ]

View File

@@ -6,7 +6,7 @@ describe Webhooks::InstagramEventsJob do
before do before do
stub_request(:post, /graph.facebook.com/) stub_request(:post, /graph.facebook.com/)
stub_request(:get, 'https://imagekit.io/blog/content/images/2020/05/media_library.jpeg') stub_request(:get, 'https://www.example.com/test.jpeg')
.with( .with(
headers: { headers: {
'Accept' => '*/*', 'Accept' => '*/*',
@@ -102,8 +102,7 @@ describe Webhooks::InstagramEventsJob do
{ story: { story:
{ {
mention: { mention: {
link: link: 'https://www.example.com/test.jpeg',
'https://lookaside.fbsbx.com/ig_messaging_cdn/?asset_id=17920786367196703&signature=Aby8EXbvNu4on9efDQecXDasiJX2s0FgWhFGz3mNFB__CsHR22O_1bJiYHkbp3mC1NQeW4jHxls9WyqVgRPcyonUbSJmD44UwLfFhbCK2obesWnFi7VOnisqLu48Xd6KYuNex7uSCQKWM-nw55zQ23bBgfCYw6h5hiJjFHwJDZYm65zXpQ',
id: '17920786367196703' id: '17920786367196703'
} }
}, },