2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
class WebHookEvent < ActiveRecord::Base
|
2024-06-07 23:26:00 +08:00
|
|
|
scope :successful, -> { where("status >= 200 AND status <= 299") }
|
|
|
|
scope :failed, -> { where("status < 200 OR status > 299") }
|
2024-07-09 04:43:16 +08:00
|
|
|
scope :not_ping, -> { where("status <> 0") }
|
2016-06-16 01:49:57 +08:00
|
|
|
belongs_to :web_hook
|
|
|
|
|
2024-07-09 04:43:16 +08:00
|
|
|
has_one :redelivering_webhook_event, class_name: "RedeliveringWebhookEvent"
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
after_save :update_web_hook_delivery_status
|
|
|
|
|
|
|
|
default_scope { order("created_at DESC") }
|
|
|
|
|
2017-11-08 14:08:30 +08:00
|
|
|
def self.purge_old
|
|
|
|
where("created_at < ?", SiteSetting.retain_web_hook_events_period_days.days.ago).delete_all
|
|
|
|
end
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
def update_web_hook_delivery_status
|
|
|
|
web_hook.last_delivery_status =
|
|
|
|
case status
|
|
|
|
when 200..299
|
|
|
|
WebHook.last_delivery_statuses[:successful]
|
|
|
|
else
|
|
|
|
WebHook.last_delivery_statuses[:failed]
|
|
|
|
end
|
|
|
|
web_hook.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: web_hook_events
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# web_hook_id :integer not null
|
|
|
|
# headers :string
|
|
|
|
# payload :text
|
2016-10-31 17:32:11 +08:00
|
|
|
# status :integer default(0)
|
2016-06-16 01:49:57 +08:00
|
|
|
# response_headers :string
|
|
|
|
# response_body :text
|
|
|
|
# duration :integer default(0)
|
2019-01-12 03:29:56 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2016-06-16 01:49:57 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_web_hook_events_on_web_hook_id (web_hook_id)
|
|
|
|
#
|