Feature: Webhooks (#489)
This commit is contained in:
36
app/controllers/api/v1/inbox/webhooks_controller.rb
Normal file
36
app/controllers/api/v1/inbox/webhooks_controller.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
class Api::V1::Inbox::WebhooksController < Api::BaseController
|
||||
before_action :check_authorization
|
||||
before_action :fetch_webhook, only: [:update, :destroy]
|
||||
|
||||
def index
|
||||
@webhooks = current_account.webhooks
|
||||
end
|
||||
|
||||
def create
|
||||
@webhook = current_account.webhooks.new(webhook_params)
|
||||
@webhook.save!
|
||||
end
|
||||
|
||||
def update
|
||||
@webhook.update!(webhook_params)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@webhook.destroy
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def webhook_params
|
||||
params.require(:webhook).permit(:account_id, :inbox_id, :urls).merge(urls: params[:urls])
|
||||
end
|
||||
|
||||
def fetch_webhook
|
||||
@webhook = current_account.webhooks.find(params[:id])
|
||||
end
|
||||
|
||||
def check_authorization
|
||||
authorize(Webhook)
|
||||
end
|
||||
end
|
||||
@@ -1,2 +0,0 @@
|
||||
module Api::V1::WebhooksHelper
|
||||
end
|
||||
7
app/jobs/webhook_job.rb
Normal file
7
app/jobs/webhook_job.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class WebhookJob < ApplicationJob
|
||||
queue_as :webhooks
|
||||
|
||||
def perform(url, payload)
|
||||
Webhooks::Trigger.execute(url, payload)
|
||||
end
|
||||
end
|
||||
15
app/listeners/webhook_listener.rb
Normal file
15
app/listeners/webhook_listener.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class WebhookListener < BaseListener
|
||||
def message_created(event)
|
||||
message = extract_message_and_account(event)[0]
|
||||
inbox = message.inbox
|
||||
|
||||
return unless message.reportable? && inbox.webhook.present?
|
||||
|
||||
webhook = message.inbox.webhook
|
||||
payload = message.push_event_data
|
||||
|
||||
webhook.urls.each do |url|
|
||||
WebhookJob.perform_later(url, payload)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -22,6 +22,7 @@ class Account < ApplicationRecord
|
||||
has_many :web_widgets, dependent: :destroy, class_name: '::Channel::WebWidget'
|
||||
has_many :telegram_bots, dependent: :destroy
|
||||
has_many :canned_responses, dependent: :destroy
|
||||
has_many :webhooks, dependent: :destroy
|
||||
has_one :subscription, dependent: :destroy
|
||||
|
||||
after_create :create_subscription
|
||||
|
||||
@@ -32,6 +32,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
|
||||
after_create :subscribe_webhook, if: :facebook?
|
||||
after_destroy :delete_round_robin_agents
|
||||
|
||||
|
||||
20
app/models/webhook.rb
Normal file
20
app/models/webhook.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# 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
|
||||
#
|
||||
|
||||
class Webhook < ApplicationRecord
|
||||
belongs_to :account
|
||||
belongs_to :inbox
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
serialize :urls, Array
|
||||
end
|
||||
17
app/policies/webhook_policy.rb
Normal file
17
app/policies/webhook_policy.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
class WebhookPolicy < ApplicationPolicy
|
||||
def index?
|
||||
@user.administrator?
|
||||
end
|
||||
|
||||
def update?
|
||||
@user.administrator?
|
||||
end
|
||||
|
||||
def destroy?
|
||||
@user.administrator?
|
||||
end
|
||||
|
||||
def create?
|
||||
@user.administrator?
|
||||
end
|
||||
end
|
||||
10
app/views/api/v1/inbox/index.json.jbuilder
Normal file
10
app/views/api/v1/inbox/index.json.jbuilder
Normal file
@@ -0,0 +1,10 @@
|
||||
json.array! @agents do |agent|
|
||||
json.account_id agent.account_id
|
||||
json.availability_status agent.availability_status
|
||||
json.confirmed agent.confirmed?
|
||||
json.email agent.email
|
||||
json.id agent.id
|
||||
json.name agent.name
|
||||
json.role agent.role
|
||||
json.thumbnail agent.avatar_url
|
||||
end
|
||||
7
app/views/api/v1/inbox/webhooks/_webhook.json.jbuilder
Normal file
7
app/views/api/v1/inbox/webhooks/_webhook.json.jbuilder
Normal file
@@ -0,0 +1,7 @@
|
||||
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
|
||||
5
app/views/api/v1/inbox/webhooks/create.json.jbuilder
Normal file
5
app/views/api/v1/inbox/webhooks/create.json.jbuilder
Normal file
@@ -0,0 +1,5 @@
|
||||
json.payload do
|
||||
json.webhook do
|
||||
json.partial! 'webhook', webhook: @webhook
|
||||
end
|
||||
end
|
||||
5
app/views/api/v1/inbox/webhooks/index.json.jbuilder
Normal file
5
app/views/api/v1/inbox/webhooks/index.json.jbuilder
Normal file
@@ -0,0 +1,5 @@
|
||||
json.payload do
|
||||
json.webhooks do
|
||||
json.array! @webhooks, partial: 'webhooks/webhook', as: :webhook
|
||||
end
|
||||
end
|
||||
5
app/views/api/v1/inbox/webhooks/update.json.jbuilder
Normal file
5
app/views/api/v1/inbox/webhooks/update.json.jbuilder
Normal file
@@ -0,0 +1,5 @@
|
||||
json.payload do
|
||||
json.webhook do
|
||||
json.partial! 'webhook', webhook: @webhook
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user