Feature: Support account/inbox specific webhooks (#562)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
class Api::V1::Inbox::WebhooksController < Api::BaseController
|
||||
class Api::V1::Account::WebhooksController < Api::BaseController
|
||||
before_action :check_authorization
|
||||
before_action :fetch_webhook, only: [:update, :destroy]
|
||||
|
||||
@@ -23,7 +23,7 @@ class Api::V1::Inbox::WebhooksController < Api::BaseController
|
||||
private
|
||||
|
||||
def webhook_params
|
||||
params.require(:webhook).permit(:account_id, :inbox_id, :urls).merge(urls: params[:urls])
|
||||
params.require(:webhook).permit(:account_id, :inbox_id, :url)
|
||||
end
|
||||
|
||||
def fetch_webhook
|
||||
@@ -3,13 +3,17 @@ class WebhookListener < BaseListener
|
||||
message = extract_message_and_account(event)[0]
|
||||
inbox = message.inbox
|
||||
|
||||
return unless message.reportable? && inbox.webhook.present?
|
||||
return unless message.reportable?
|
||||
|
||||
webhook = message.inbox.webhook
|
||||
payload = message.push_event_data.merge(event: __method__.to_s)
|
||||
payload = message.webhook_data.merge(event: __method__.to_s)
|
||||
# Account webhooks
|
||||
inbox.account.webhooks.account.each do |webhook|
|
||||
WebhookJob.perform_later(webhook.url, payload)
|
||||
end
|
||||
|
||||
webhook.urls.each do |url|
|
||||
WebhookJob.perform_later(url, payload)
|
||||
# Inbox webhooks
|
||||
inbox.webhooks.inbox.each do |webhook|
|
||||
WebhookJob.perform_later(webhook.url, payload)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,6 +60,13 @@ class Account < ApplicationRecord
|
||||
}
|
||||
end
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
id: id,
|
||||
name: name
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_subscription
|
||||
|
||||
@@ -41,4 +41,11 @@ class Contact < ApplicationRecord
|
||||
pubsub_token: pubsub_token
|
||||
}
|
||||
end
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
id: id,
|
||||
name: name
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -93,6 +93,13 @@ class Conversation < ApplicationRecord
|
||||
Conversations::EventDataPresenter.new(self).lock_data
|
||||
end
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
display_id: display_id,
|
||||
additional_attributes: additional_attributes
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def dispatch_events
|
||||
|
||||
@@ -33,7 +33,7 @@ class Inbox < ApplicationRecord
|
||||
has_many :members, through: :inbox_members, source: :user
|
||||
has_many :conversations, dependent: :destroy
|
||||
has_many :messages, through: :conversations
|
||||
has_one :webhook, dependent: :destroy
|
||||
has_many :webhooks, dependent: :destroy
|
||||
after_create :subscribe_webhook, if: :facebook?
|
||||
after_destroy :delete_round_robin_agents
|
||||
|
||||
@@ -60,6 +60,13 @@ class Inbox < ApplicationRecord
|
||||
account.users.find_by(id: user_id)
|
||||
end
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
id: id,
|
||||
name: name
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def delete_round_robin_agents
|
||||
|
||||
@@ -50,6 +50,7 @@ class Message < ApplicationRecord
|
||||
belongs_to :inbox
|
||||
belongs_to :conversation
|
||||
belongs_to :user, required: false
|
||||
belongs_to :contact, required: false
|
||||
|
||||
has_one :attachment, dependent: :destroy, autosave: true
|
||||
|
||||
@@ -78,6 +79,21 @@ class Message < ApplicationRecord
|
||||
incoming? || outgoing?
|
||||
end
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
id: id,
|
||||
content: content,
|
||||
created_at: created_at,
|
||||
message_type: message_type,
|
||||
source_id: source_id,
|
||||
sender: user.try(:webhook_data),
|
||||
contact: contact.try(:webhook_data),
|
||||
inbox: inbox.webhook_data,
|
||||
conversation: conversation.webhook_data,
|
||||
account: account.webhook_data
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def dispatch_event
|
||||
|
||||
@@ -110,4 +110,12 @@ class User < ApplicationRecord
|
||||
avatar_url: avatar_url
|
||||
}
|
||||
end
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
id: id,
|
||||
name: name,
|
||||
email: email
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,19 +2,20 @@
|
||||
#
|
||||
# Table name: webhooks
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# urls :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :integer
|
||||
# inbox_id :integer
|
||||
# id :bigint not null, primary key
|
||||
# url :string
|
||||
# webhook_type :integer default("account")
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :integer
|
||||
# inbox_id :integer
|
||||
#
|
||||
|
||||
class Webhook < ApplicationRecord
|
||||
belongs_to :account
|
||||
belongs_to :inbox
|
||||
belongs_to :inbox, optional: true
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
serialize :urls, Array
|
||||
|
||||
enum webhook_type: { account: 0, inbox: 1 }
|
||||
end
|
||||
|
||||
9
app/views/api/v1/account/webhooks/_webhook.json.jbuilder
Normal file
9
app/views/api/v1/account/webhooks/_webhook.json.jbuilder
Normal file
@@ -0,0 +1,9 @@
|
||||
json.id webhook.id
|
||||
json.url webhook.url
|
||||
json.account_id webhook.account_id
|
||||
if webhook.inbox
|
||||
json.inbox do
|
||||
json.id webhook.inbox.id
|
||||
json.name webhook.inbox.name
|
||||
end
|
||||
end
|
||||
@@ -1,7 +0,0 @@
|
||||
json.id webhook.id
|
||||
json.urls webhook.urls
|
||||
json.account_id webhook.account_id
|
||||
json.inbox do
|
||||
json.id webhook.inbox.id
|
||||
json.name webhook.inbox.name
|
||||
end
|
||||
Reference in New Issue
Block a user