diff --git a/app/listeners/webhook_listener.rb b/app/listeners/webhook_listener.rb index 11a86e639..81d2c6b0a 100644 --- a/app/listeners/webhook_listener.rb +++ b/app/listeners/webhook_listener.rb @@ -54,7 +54,7 @@ class WebhookListener < BaseListener private 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]) WebhookJob.perform_later(webhook.url, payload) diff --git a/app/models/message.rb b/app/models/message.rb index 2930786f9..a3f63dbe9 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -116,7 +116,7 @@ class Message < ApplicationRecord end def webhook_data - { + data = { account: account.webhook_data, additional_attributes: additional_attributes, content_attributes: content_attributes, @@ -131,6 +131,8 @@ class Message < ApplicationRecord sender: sender.try(:webhook_data), source_id: source_id } + data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present? + data end def content diff --git a/app/models/webhook.rb b/app/models/webhook.rb index fe97fe583..5b0095093 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -5,7 +5,7 @@ # id :bigint not null, primary key # subscriptions :jsonb # url :string -# webhook_type :integer default("account") +# webhook_type :integer default("account_type") # created_at :datetime not null # updated_at :datetime not null # account_id :integer @@ -23,7 +23,7 @@ class Webhook < ApplicationRecord validates :account_id, presence: true validates :url, uniqueness: { scope: [:account_id] }, format: URI::DEFAULT_PARSER.make_regexp(%w[http https]) 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 webwidget_triggered].freeze diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index 4940d704a..aeff941da 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -33,6 +33,21 @@ RSpec.describe Message, type: :model do 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 let(:message) { build(:message, account: create(:account)) }