2020-02-14 17:49:17 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: webhooks
|
|
|
|
#
|
2022-04-25 12:14:42 +00:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# subscriptions :jsonb
|
|
|
|
# url :string
|
2022-10-18 00:36:56 +00:00
|
|
|
# webhook_type :integer default("account_type")
|
2022-04-25 12:14:42 +00:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer
|
|
|
|
# inbox_id :integer
|
2020-02-14 17:49:17 +00:00
|
|
|
#
|
2020-04-05 16:41:27 +00:00
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_webhooks_on_account_id_and_url (account_id,url) UNIQUE
|
|
|
|
#
|
2020-02-14 17:49:17 +00:00
|
|
|
|
|
|
|
class Webhook < ApplicationRecord
|
|
|
|
belongs_to :account
|
2020-02-26 04:14:24 +00:00
|
|
|
belongs_to :inbox, optional: true
|
2020-02-14 17:49:17 +00:00
|
|
|
|
|
|
|
validates :account_id, presence: true
|
2022-02-28 10:14:02 +00:00
|
|
|
validates :url, uniqueness: { scope: [:account_id] }, format: URI::DEFAULT_PARSER.make_regexp(%w[http https])
|
2022-04-25 12:14:42 +00:00
|
|
|
validate :validate_webhook_subscriptions
|
2022-10-18 00:36:56 +00:00
|
|
|
enum webhook_type: { account_type: 0, inbox_type: 1 }
|
2022-04-25 12:14:42 +00:00
|
|
|
|
|
|
|
ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created message_created message_updated
|
|
|
|
webwidget_triggered].freeze
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_webhook_subscriptions
|
|
|
|
invalid_subscriptions = !subscriptions.instance_of?(Array) ||
|
|
|
|
subscriptions.blank? ||
|
|
|
|
(subscriptions.uniq - ALLOWED_WEBHOOK_EVENTS).length.positive?
|
|
|
|
errors.add(:subscriptions, I18n.t('errors.webhook.invalid')) if invalid_subscriptions
|
|
|
|
end
|
2020-02-14 17:49:17 +00:00
|
|
|
end
|