From 7b4cac746a6f6b7c33d8f081b09a7ec390a199dd Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Mon, 21 Mar 2022 17:10:15 +0530 Subject: [PATCH] feat: enhancement to un-send instagram message (#4120) --- app/services/instagram/message_text.rb | 15 +++++++++++ .../instagram_message_create_event.rb | 27 +++++++++++++++++++ .../webhooks/instagram_events_job_spec.rb | 21 +++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/app/services/instagram/message_text.rb b/app/services/instagram/message_text.rb index 9fc1164a3..6905700dd 100644 --- a/app/services/instagram/message_text.rb +++ b/app/services/instagram/message_text.rb @@ -20,6 +20,8 @@ class Instagram::MessageText < Instagram::WebhooksBaseService # person can connect the channel and then delete the inbox return if @inbox.blank? + return unsend_message if message_is_deleted? + ensure_contact(contact_id) create_message @@ -46,6 +48,19 @@ class Instagram::MessageText < Instagram::WebhooksBaseService @messaging[:message][:is_echo].present? end + def message_is_deleted? + @messaging[:message][:is_deleted].present? + end + + def unsend_message + message_to_delete = @inbox.messages.find_by( + source_id: @messaging[:message][:mid] + ) + return if message_to_delete.blank? + + message_to_delete.update!(content: I18n.t('conversations.messages.deleted'), deleted: true) + end + def create_message Messages::Instagram::MessageBuilder.new(@messaging, @inbox, outgoing_echo: agent_message_via_echo?).perform end diff --git a/spec/factories/instagram/instagram_message_create_event.rb b/spec/factories/instagram/instagram_message_create_event.rb index b4ffa2dff..b62797630 100644 --- a/spec/factories/instagram/instagram_message_create_event.rb +++ b/spec/factories/instagram/instagram_message_create_event.rb @@ -56,6 +56,33 @@ FactoryBot.define do initialize_with { attributes } end + factory :instagram_message_unsend_event, class: Hash do + entry do + [ + { + 'id': 'instagram-message-id-123', + 'time': '2021-09-08T06:34:04+0000', + 'messaging': [ + { + 'sender': { + 'id': 'Sender-id-1' + }, + 'recipient': { + 'id': 'chatwoot-app-user-id-1' + }, + 'timestamp': '2021-09-08T06:34:04+0000', + 'message': { + 'mid': 'message-id-to-delete', + 'is_deleted': true + } + } + ] + } + ] + end + initialize_with { attributes } + end + factory :instagram_message_attachment_event, class: Hash do entry do [ diff --git a/spec/jobs/webhooks/instagram_events_job_spec.rb b/spec/jobs/webhooks/instagram_events_job_spec.rb index eaf77bf7b..bf3b43e14 100644 --- a/spec/jobs/webhooks/instagram_events_job_spec.rb +++ b/spec/jobs/webhooks/instagram_events_job_spec.rb @@ -28,6 +28,7 @@ describe Webhooks::InstagramEventsJob do 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!(:test_params) { build(:instagram_test_text_event).with_indifferent_access } + let!(:unsend_event) { build(:instagram_message_unsend_event).with_indifferent_access } let!(:attachment_params) { build(:instagram_message_attachment_event).with_indifferent_access } let!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access } let(:fb_object) { double } @@ -61,6 +62,26 @@ describe Webhooks::InstagramEventsJob do expect(instagram_inbox.messages.last.content).to eq('This is a test message from facebook.') end + it 'handle instagram unsend message event' do + create(:message, + source_id: 'message-id-to-delete', + inbox_id: instagram_inbox.id) + allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) + allow(fb_object).to receive(:get_object).and_return( + { + name: 'Jane', + id: 'Sender-id-1', + account_id: instagram_inbox.account_id, + profile_pic: 'https://chatwoot-assets.local/sample.png' + }.with_indifferent_access + ) + 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.reload.deleted).to eq true + end + it 'creates incoming message with attachments in the instagram inbox' do allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(fb_object).to receive(:get_object).and_return(