Skip to content

Commit dc2fe4d

Browse files
committed
feat: add inbox matching logic to webhook event delivery
1 parent 4f70b0f commit dc2fe4d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

app/listeners/webhook_listener.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ def conversation_status_changed(event)
33
conversation = extract_conversation_and_account(event)[0]
44
changed_attributes = extract_changed_attributes(event)
55
inbox = conversation.inbox
6+
67
payload = conversation.webhook_data.merge(event: __method__.to_s, changed_attributes: changed_attributes)
78
deliver_webhook_payloads(payload, inbox)
89
end
@@ -88,6 +89,7 @@ def inbox_updated(event)
8889
def deliver_account_webhooks(payload, account)
8990
account.webhooks.account_type.each do |webhook|
9091
next unless webhook.subscriptions.include?(payload[:event])
92+
next if payload[:inbox].present? && (webhook.inbox_id.present? && webhook.inbox_id != payload[:inbox][:id])
9193

9294
WebhookJob.perform_later(webhook.url, payload)
9395
end

spec/listeners/webhook_listener_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@
7575
listener.message_created(api_event)
7676
end
7777
end
78+
79+
context 'when webhook has an inbox and it matches the event inbox' do
80+
it 'receives the event' do
81+
webhook = create(:webhook, account: account, inbox: inbox)
82+
expect(WebhookJob).to receive(:perform_later)
83+
.with(webhook.url, message.webhook_data.merge(event: 'message_created')).once
84+
85+
listener.message_created(message_created_event)
86+
end
87+
end
88+
89+
context 'when webhook has an inbox and it does not match the event inbox' do
90+
it 'does not receive the event' do
91+
another_inbox = create(:inbox, account: account)
92+
create(:webhook, account: account, inbox: another_inbox)
93+
94+
expect(WebhookJob).not_to receive(:perform_later)
95+
listener.message_created(message_created_event)
96+
end
97+
end
7898
end
7999

80100
describe '#conversation_created' do

0 commit comments

Comments
 (0)