From 6571baf2116b9cd45bef82aa240c720f25440719 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Wed, 21 Aug 2024 20:10:31 +0530 Subject: [PATCH] fix: Delete attachments when removing them from Instagram in real-time (#9996) --- app/services/instagram/message_text.rb | 1 + spec/jobs/webhooks/instagram_events_job_spec.rb | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/services/instagram/message_text.rb b/app/services/instagram/message_text.rb index e42355990..396f0cdac 100644 --- a/app/services/instagram/message_text.rb +++ b/app/services/instagram/message_text.rb @@ -83,6 +83,7 @@ class Instagram::MessageText < Instagram::WebhooksBaseService ) return if message_to_delete.blank? + message_to_delete.attachments.destroy_all message_to_delete.update!(content: I18n.t('conversations.messages.deleted'), deleted: true) end diff --git a/spec/jobs/webhooks/instagram_events_job_spec.rb b/spec/jobs/webhooks/instagram_events_job_spec.rb index 84bff722b..1ffa8691c 100644 --- a/spec/jobs/webhooks/instagram_events_job_spec.rb +++ b/spec/jobs/webhooks/instagram_events_job_spec.rb @@ -81,9 +81,7 @@ describe Webhooks::InstagramEventsJob do end it 'handle instagram unsend message event' do - create(:message, - source_id: 'message-id-to-delete', - inbox_id: instagram_inbox.id) + message = create(:message, inbox_id: instagram_inbox.id, source_id: 'message-id-to-delete') allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(fb_object).to receive(:get_object).and_return( { @@ -93,10 +91,15 @@ describe Webhooks::InstagramEventsJob do profile_pic: 'https://chatwoot-assets.local/sample.png' }.with_indifferent_access ) + message.attachments.new(file_type: :image, external_url: 'https://www.example.com/test.jpeg') + expect(instagram_inbox.messages.count).to be 1 + instagram_webhook.perform_now(unsend_event[:entry]) expect(instagram_inbox.messages.last.content).to eq 'This message was deleted' + expect(instagram_inbox.messages.last.deleted).to be true + expect(instagram_inbox.messages.last.attachments.count).to be 0 expect(instagram_inbox.messages.last.reload.deleted).to be true end