chore: Add attachments key to message_created webhook payload (#5659)

- Add attachments key to `message_created` webhook payload
This commit is contained in:
Sojan Jose 2022-10-17 17:36:56 -07:00 committed by GitHub
parent 1c44e43c43
commit 2423def8e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 4 deletions

View file

@ -54,7 +54,7 @@ class WebhookListener < BaseListener
private private
def deliver_account_webhooks(payload, inbox) def deliver_account_webhooks(payload, inbox)
inbox.account.webhooks.account.each do |webhook| inbox.account.webhooks.account_type.each do |webhook|
next unless webhook.subscriptions.include?(payload[:event]) next unless webhook.subscriptions.include?(payload[:event])
WebhookJob.perform_later(webhook.url, payload) WebhookJob.perform_later(webhook.url, payload)

View file

@ -116,7 +116,7 @@ class Message < ApplicationRecord
end end
def webhook_data def webhook_data
{ data = {
account: account.webhook_data, account: account.webhook_data,
additional_attributes: additional_attributes, additional_attributes: additional_attributes,
content_attributes: content_attributes, content_attributes: content_attributes,
@ -131,6 +131,8 @@ class Message < ApplicationRecord
sender: sender.try(:webhook_data), sender: sender.try(:webhook_data),
source_id: source_id source_id: source_id
} }
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
data
end end
def content def content

View file

@ -5,7 +5,7 @@
# id :bigint not null, primary key # id :bigint not null, primary key
# subscriptions :jsonb # subscriptions :jsonb
# url :string # url :string
# webhook_type :integer default("account") # webhook_type :integer default("account_type")
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# account_id :integer # account_id :integer
@ -23,7 +23,7 @@ class Webhook < ApplicationRecord
validates :account_id, presence: true validates :account_id, presence: true
validates :url, uniqueness: { scope: [:account_id] }, format: URI::DEFAULT_PARSER.make_regexp(%w[http https]) validates :url, uniqueness: { scope: [:account_id] }, format: URI::DEFAULT_PARSER.make_regexp(%w[http https])
validate :validate_webhook_subscriptions validate :validate_webhook_subscriptions
enum webhook_type: { account: 0, inbox: 1 } enum webhook_type: { account_type: 0, inbox_type: 1 }
ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created message_created message_updated ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created message_created message_updated
webwidget_triggered].freeze webwidget_triggered].freeze

View file

@ -33,6 +33,21 @@ RSpec.describe Message, type: :model do
end end
end end
context 'with webhook_data' do
it 'contains the message attachment when attachment is present' do
message = create(:message)
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
attachment.save!
expect(message.webhook_data.key?(:attachments)).to be true
end
it 'does not contain the message attachment when attachment is not present' do
message = create(:message)
expect(message.webhook_data.key?(:attachments)).to be false
end
end
context 'when message is created' do context 'when message is created' do
let(:message) { build(:message, account: create(:account)) } let(:message) { build(:message, account: create(:account)) }