feat: enhancement to un-send instagram message (#4120)

This commit is contained in:
Tejaswini Chile 2022-03-21 17:10:15 +05:30 committed by GitHub
parent 6169d172d8
commit 7b4cac746a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 0 deletions

View file

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

View file

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

View file

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