Feature: Webhooks (#489)

This commit is contained in:
Subin T P
2020-02-14 23:19:17 +05:30
committed by GitHub
parent 79a847aeab
commit 919261d843
29 changed files with 566 additions and 214 deletions

View 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

View File

@@ -1,2 +0,0 @@
module Api::V1::WebhooksHelper
end

7
app/jobs/webhook_job.rb Normal file
View File

@@ -0,0 +1,7 @@
class WebhookJob < ApplicationJob
queue_as :webhooks
def perform(url, payload)
Webhooks::Trigger.execute(url, payload)
end
end

View 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

View File

@@ -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

View File

@@ -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
View 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

View 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

View 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

View 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

View File

@@ -0,0 +1,5 @@
json.payload do
json.webhook do
json.partial! 'webhook', webhook: @webhook
end
end

View File

@@ -0,0 +1,5 @@
json.payload do
json.webhooks do
json.array! @webhooks, partial: 'webhooks/webhook', as: :webhook
end
end

View File

@@ -0,0 +1,5 @@
json.payload do
json.webhook do
json.partial! 'webhook', webhook: @webhook
end
end