2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-15 13:49:57 -04: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
|
2018-12-05 04:14:06 -05:00
|
|
|
has_and_belongs_to_many :tags
|
2016-06-15 13:49:57 -04:00
|
|
|
|
|
|
|
has_many :web_hook_events, dependent: :destroy
|
2024-07-08 16:43:16 -04:00
|
|
|
has_many :redelivering_webhook_events
|
2024-06-25 12:56:47 -04:00
|
|
|
has_many :web_hook_events_daily_aggregates, dependent: :destroy
|
2016-06-15 13:49:57 -04:00
|
|
|
|
|
|
|
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?
|
2022-11-01 12:33:17 -04:00
|
|
|
validate :ensure_payload_url_allowed, if: :payload_url_changed?
|
2016-06-15 13:49:57 -04:00
|
|
|
|
2017-12-11 03:15:50 -05:00
|
|
|
before_save :strip_url
|
|
|
|
|
2018-12-05 04:14:06 -05:00
|
|
|
def tag_names=(tag_names_arg)
|
|
|
|
DiscourseTagging.add_or_create_tags_by_name(self, tag_names_arg, unlimited: true)
|
|
|
|
end
|
|
|
|
|
2016-06-15 13:49:57 -04: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, disabled: 4)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_event_types
|
2023-10-08 23:35:31 -04:00
|
|
|
WebHookEventType.where(
|
|
|
|
id: [
|
|
|
|
WebHookEventType::TYPES[:post_created],
|
|
|
|
WebHookEventType::TYPES[:post_edited],
|
|
|
|
WebHookEventType::TYPES[:post_destroyed],
|
|
|
|
WebHookEventType::TYPES[:post_recovered],
|
|
|
|
],
|
|
|
|
)
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
|
2018-05-21 04:23:09 -04:00
|
|
|
def strip_url
|
|
|
|
self.payload_url = (payload_url || "").strip.presence
|
|
|
|
end
|
|
|
|
|
2023-10-08 23:35:31 -04:00
|
|
|
EVENT_NAME_TO_EVENT_TYPE_MAP = {
|
|
|
|
/\Atopic_\w+_status_updated\z/ => "topic_edited",
|
|
|
|
"reviewable_score_updated" => "reviewable_updated",
|
|
|
|
"reviewable_transitioned_to" => "reviewable_updated",
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.translate_event_name_to_type(event_name)
|
|
|
|
EVENT_NAME_TO_EVENT_TYPE_MAP.each do |key, value|
|
|
|
|
if key.is_a?(Regexp)
|
|
|
|
return value if event_name.to_s =~ key
|
|
|
|
else
|
|
|
|
return value if event_name.to_s == key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
event_name.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.active_web_hooks(event)
|
|
|
|
event_type = translate_event_name_to_type(event)
|
|
|
|
|
2016-06-15 13:49:57 -04:00
|
|
|
WebHook
|
|
|
|
.where(active: true)
|
|
|
|
.joins(:web_hook_event_types)
|
2023-10-08 23:35:31 -04:00
|
|
|
.where("web_hooks.wildcard_web_hook = ? OR web_hook_event_types.name = ?", true, event_type)
|
2018-05-24 02:56:40 -04:00
|
|
|
.distinct
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
|
2018-10-05 04:53:59 -04:00
|
|
|
def self.enqueue_hooks(type, event, opts = {})
|
2023-10-08 23:35:31 -04:00
|
|
|
active_web_hooks(event).each do |web_hook|
|
2018-05-21 04:23:09 -04:00
|
|
|
Jobs.enqueue(
|
|
|
|
:emit_web_hook_event,
|
2018-10-05 04:53:59 -04:00
|
|
|
opts.merge(web_hook_id: web_hook.id, event_name: event.to_s, event_type: type.to_s),
|
2018-05-21 04:23:09 -04:00
|
|
|
)
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-30 20:08:38 -04:00
|
|
|
def self.enqueue_object_hooks(type, object, event, serializer = nil, opts = {})
|
2023-10-08 23:35:31 -04:00
|
|
|
if active_web_hooks(event).exists?
|
2018-10-05 04:53:59 -04:00
|
|
|
payload = WebHook.generate_payload(type, object, serializer)
|
|
|
|
|
2021-04-30 20:08:38 -04:00
|
|
|
WebHook.enqueue_hooks(type, event, opts.merge(id: object.id, payload: payload))
|
2018-05-21 04:23:09 -04:00
|
|
|
end
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
|
2019-03-06 12:22:54 -05:00
|
|
|
def self.enqueue_topic_hooks(event, topic, payload = nil)
|
2023-10-08 23:35:31 -04:00
|
|
|
if active_web_hooks(event).exists? && topic.present?
|
2019-03-06 12:22:54 -05:00
|
|
|
payload ||=
|
|
|
|
begin
|
2023-05-04 05:15:31 -04:00
|
|
|
topic_view = TopicView.new(topic.id, Discourse.system_user, skip_staff_action: true)
|
2019-03-06 12:22:54 -05:00
|
|
|
WebHook.generate_payload(:topic, topic_view, WebHookTopicViewSerializer)
|
|
|
|
end
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2018-10-05 04:53:59 -04:00
|
|
|
WebHook.enqueue_hooks(
|
|
|
|
:topic,
|
|
|
|
event,
|
|
|
|
id: topic.id,
|
2018-12-05 09:37:18 -05:00
|
|
|
category_id: topic.category_id,
|
|
|
|
tag_ids: topic.tags.pluck(:id),
|
2018-10-05 04:53:59 -04:00
|
|
|
payload: payload,
|
2018-05-24 03:01:46 -04:00
|
|
|
)
|
2018-05-21 04:23:09 -04:00
|
|
|
end
|
2016-11-04 16:16:30 -04:00
|
|
|
end
|
2017-12-11 03:15:50 -05:00
|
|
|
|
2019-03-06 12:22:54 -05:00
|
|
|
def self.enqueue_post_hooks(event, post, payload = nil)
|
2023-10-08 23:35:31 -04:00
|
|
|
if active_web_hooks(event).exists? && post.present?
|
2019-03-06 12:22:54 -05:00
|
|
|
payload ||= WebHook.generate_payload(:post, post)
|
2018-10-05 04:53:59 -04:00
|
|
|
|
|
|
|
WebHook.enqueue_hooks(
|
|
|
|
:post,
|
|
|
|
event,
|
|
|
|
id: post.id,
|
2018-12-05 09:37:18 -05:00
|
|
|
category_id: post.topic&.category_id,
|
|
|
|
tag_ids: post.topic&.tags&.pluck(:id),
|
2018-10-05 04:53:59 -04:00
|
|
|
payload: payload,
|
2018-05-24 03:01:46 -04:00
|
|
|
)
|
2018-05-21 04:23:09 -04:00
|
|
|
end
|
2017-12-11 03:15:50 -05:00
|
|
|
end
|
2018-05-21 04:23:09 -04:00
|
|
|
|
2018-10-05 04:53:59 -04:00
|
|
|
def self.generate_payload(type, object, serializer = nil)
|
|
|
|
serializer ||= TagSerializer if type == :tag
|
|
|
|
serializer ||= "WebHook#{type.capitalize}Serializer".constantize
|
|
|
|
|
|
|
|
serializer.new(object, scope: self.guardian, root: false).to_json
|
|
|
|
end
|
|
|
|
|
2018-05-21 04:23:09 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def self.guardian
|
2021-03-04 16:41:57 -05:00
|
|
|
Guardian.new(Discourse.system_user)
|
2018-05-21 04:23:09 -04:00
|
|
|
end
|
2022-11-01 12:33:17 -04:00
|
|
|
|
|
|
|
# This check is to improve UX
|
|
|
|
# IPs are re-checked at request time
|
|
|
|
def ensure_payload_url_allowed
|
|
|
|
return if payload_url.blank?
|
|
|
|
uri = URI(payload_url.strip)
|
|
|
|
|
|
|
|
allowed =
|
|
|
|
begin
|
|
|
|
FinalDestination::SSRFDetector.lookup_and_filter_ips(uri.hostname).present?
|
|
|
|
rescue FinalDestination::SSRFDetector::DisallowedIpError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
self.errors.add(:base, I18n.t("webhooks.payload_url.blocked_or_internal")) if !allowed
|
|
|
|
end
|
2016-06-15 13:49:57 -04: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 05:32:11 -04:00
|
|
|
# status :integer default(1), not null
|
2016-06-15 13:49:57 -04: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
|
2019-01-11 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2016-06-15 13:49:57 -04:00
|
|
|
#
|