feat: Facebook delivery reports (#8136)
This commit is contained in:
@@ -10,4 +10,24 @@ FactoryBot.define do
|
||||
|
||||
initialize_with { attributes }
|
||||
end
|
||||
|
||||
factory :message_deliveries, class: Hash do
|
||||
messaging do
|
||||
{ sender: { id: '3383290475046708' },
|
||||
recipient: { id: '117172741761305' },
|
||||
delivery: { watermark: '1648581633369' } }
|
||||
end
|
||||
|
||||
initialize_with { attributes }
|
||||
end
|
||||
|
||||
factory :message_reads, class: Hash do
|
||||
messaging do
|
||||
{ sender: { id: '3383290475046708' },
|
||||
recipient: { id: '117172741761305' },
|
||||
read: { watermark: '1648581633369' } }
|
||||
end
|
||||
|
||||
initialize_with { attributes }
|
||||
end
|
||||
end
|
||||
|
||||
44
spec/jobs/webhooks/facebook_delivery_job_spec.rb
Normal file
44
spec/jobs/webhooks/facebook_delivery_job_spec.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Webhooks::FacebookDeliveryJob do
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
let(:message) { 'test_message' }
|
||||
let(:parsed_message) { instance_double(Integrations::Facebook::MessageParser) }
|
||||
let(:delivery_status) { instance_double(Integrations::Facebook::DeliveryStatus) }
|
||||
|
||||
before do
|
||||
allow(Integrations::Facebook::MessageParser).to receive(:new).with(message).and_return(parsed_message)
|
||||
allow(Integrations::Facebook::DeliveryStatus).to receive(:new).with(params: parsed_message).and_return(delivery_status)
|
||||
allow(delivery_status).to receive(:perform)
|
||||
end
|
||||
|
||||
after do
|
||||
clear_enqueued_jobs
|
||||
end
|
||||
|
||||
describe '#perform_later' do
|
||||
it 'enqueues the job' do
|
||||
expect do
|
||||
described_class.perform_later(message)
|
||||
end.to have_enqueued_job(described_class).with(message).on_queue('low')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
it 'calls the MessageParser with the correct argument' do
|
||||
expect(Integrations::Facebook::MessageParser).to receive(:new).with(message)
|
||||
described_class.perform_now(message)
|
||||
end
|
||||
|
||||
it 'calls the DeliveryStatus with the correct argument' do
|
||||
expect(Integrations::Facebook::DeliveryStatus).to receive(:new).with(params: parsed_message)
|
||||
described_class.perform_now(message)
|
||||
end
|
||||
|
||||
it 'executes perform on the DeliveryStatus instance' do
|
||||
expect(delivery_status).to receive(:perform)
|
||||
described_class.perform_now(message)
|
||||
end
|
||||
end
|
||||
end
|
||||
83
spec/lib/integrations/facebook/delivery_status_spec.rb
Normal file
83
spec/lib/integrations/facebook/delivery_status_spec.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Integrations::Facebook::DeliveryStatus do
|
||||
subject(:message_builder) { described_class.new(message_deliveries, facebook_channel.inbox).perform }
|
||||
|
||||
before do
|
||||
stub_request(:post, /graph\.facebook\.com/)
|
||||
end
|
||||
|
||||
let!(:account) { create(:account) }
|
||||
let!(:facebook_channel) { create(:channel_facebook_page, page_id: '117172741761305') }
|
||||
let!(:message_delivery_object) { build(:message_deliveries).to_json }
|
||||
let!(:message_deliveries) { Integrations::Facebook::MessageParser.new(message_delivery_object) }
|
||||
|
||||
let!(:contact) { create(:contact, account: account) }
|
||||
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: facebook_channel.inbox, source_id: '3383290475046708') }
|
||||
let!(:conversation) { create(:conversation, inbox: facebook_channel.inbox, contact: contact, contact_inbox: contact_inbox) }
|
||||
|
||||
let!(:message_read_object) { build(:message_reads).to_json }
|
||||
let!(:message_reads) { Integrations::Facebook::MessageParser.new(message_read_object) }
|
||||
let!(:message1) do
|
||||
create(:message, content: 'facebook message', message_type: 'outgoing', inbox: facebook_channel.inbox, conversation: conversation)
|
||||
end
|
||||
let!(:message2) do
|
||||
create(:message, content: 'facebook message', message_type: 'incoming', inbox: facebook_channel.inbox, conversation: conversation)
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
context 'when message_deliveries callback fires' do
|
||||
before do
|
||||
allow(Conversations::UpdateMessageStatusJob).to receive(:perform_later)
|
||||
end
|
||||
|
||||
it 'updates all messages if the status is delivered' do
|
||||
described_class.new(params: message_deliveries).perform
|
||||
expect(Conversations::UpdateMessageStatusJob).to have_received(:perform_later).with(
|
||||
message1.conversation.id,
|
||||
Time.zone.at(message_deliveries.delivery['watermark'].to_i).to_datetime,
|
||||
:delivered
|
||||
)
|
||||
end
|
||||
|
||||
it 'does not update the message status if the message is incoming' do
|
||||
described_class.new(params: message_deliveries).perform
|
||||
expect(message2.reload.status).to eq('sent')
|
||||
end
|
||||
|
||||
it 'does not update the message status if the message was created after the watermark' do
|
||||
message1.update(created_at: 1.day.from_now)
|
||||
message_deliveries.delivery['watermark'] = 1.day.ago.to_i
|
||||
described_class.new(params: message_deliveries).perform
|
||||
expect(message1.reload.status).to eq('sent')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when message_reads callback fires' do
|
||||
before do
|
||||
allow(Conversations::UpdateMessageStatusJob).to receive(:perform_later)
|
||||
end
|
||||
|
||||
it 'updates all messages if the status is read' do
|
||||
described_class.new(params: message_reads).perform
|
||||
expect(Conversations::UpdateMessageStatusJob).to have_received(:perform_later).with(
|
||||
message1.conversation.id,
|
||||
Time.zone.at(message_reads.read['watermark'].to_i).to_datetime,
|
||||
:read
|
||||
)
|
||||
end
|
||||
|
||||
it 'does not update the message status if the message is incoming' do
|
||||
described_class.new(params: message_reads).perform
|
||||
expect(message2.reload.status).to eq('sent')
|
||||
end
|
||||
|
||||
it 'does not update the message status if the message was created after the watermark' do
|
||||
message1.update(created_at: 1.day.from_now)
|
||||
message_reads.read['watermark'] = 1.day.ago.to_i
|
||||
described_class.new(params: message_reads).perform
|
||||
expect(message1.reload.status).to eq('sent')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user