discourse/app/models/web_hook.rb

75 lines
2.5 KiB
Ruby
Raw Normal View History

2016-06-15 12:49:57 -05:00
class WebHook < ActiveRecord::Base
has_and_belongs_to_many :web_hook_event_types
has_and_belongs_to_many :groups
has_and_belongs_to_many :categories
has_many :web_hook_events, dependent: :destroy
default_scope { order('id ASC') }
validates :payload_url, presence: true, format: URI::regexp(%w(http https))
validates :secret, length: { minimum: 12 }, allow_blank: true
validates_presence_of :content_type
validates_presence_of :last_delivery_status
validates_presence_of :web_hook_event_types, unless: :wildcard_web_hook?
2017-12-11 02:15:50 -06:00
before_save :strip_url
2016-06-15 12:49:57 -05:00
def self.content_types
@content_types ||= Enum.new('application/json' => 1,
'application/x-www-form-urlencoded' => 2)
end
def self.last_delivery_statuses
@last_delivery_statuses ||= Enum.new(inactive: 1,
failed: 2,
successful: 3)
end
def self.default_event_types
[WebHookEventType.find(WebHookEventType::POST)]
end
def self.find_by_type(type)
WebHook.where(active: true)
2017-07-27 20:20:09 -05:00
.joins(:web_hook_event_types)
.where("web_hooks.wildcard_web_hook = ? OR web_hook_event_types.name = ?", true, type.to_s)
.uniq
2016-06-15 12:49:57 -05:00
end
def self.enqueue_hooks(type, opts = {})
find_by_type(type).each do |w|
Jobs.enqueue(:emit_web_hook_event, opts.merge(web_hook_id: w.id, event_type: type.to_s))
end
end
2017-07-27 20:20:09 -05:00
def self.enqueue_topic_hooks(event, topic, user = nil)
WebHook.enqueue_hooks(:topic, topic_id: topic.id, category_id: topic&.category_id, event_name: event.to_s)
2016-06-15 12:49:57 -05:00
end
2017-07-27 20:20:09 -05:00
def self.enqueue_post_hooks(event, post, user = nil)
WebHook.enqueue_hooks(:post, post_id: post.id, category_id: post&.topic&.category_id, event_name: event.to_s)
end
2017-12-11 02:15:50 -06:00
def strip_url
self.payload_url = (payload_url || "").strip.presence
end
2016-06-15 12:49:57 -05:00
end
# == Schema Information
#
# Table name: web_hooks
#
# id :integer not null, primary key
# payload_url :string not null
# content_type :integer default(1), not null
# last_delivery_status :integer default(1), not null
2016-10-31 04:32:11 -05:00
# status :integer default(1), not null
2016-06-15 12:49:57 -05:00
# secret :string default("")
# wildcard_web_hook :boolean default(FALSE), not null
# verify_certificate :boolean default(TRUE), not null
# active :boolean default(FALSE), not null
2017-12-05 09:29:14 -06:00
# created_at :datetime
# updated_at :datetime
2016-06-15 12:49:57 -05:00
#