2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
class Admin::WebHooksController < Admin::AdminController
|
2024-07-09 04:43:16 +08:00
|
|
|
before_action :fetch_web_hook,
|
|
|
|
only: %i[show update destroy list_events bulk_events ping redeliver_failed_events]
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
limit = 50
|
|
|
|
offset = params[:offset].to_i
|
|
|
|
|
|
|
|
web_hooks =
|
|
|
|
WebHook
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.includes(:web_hook_event_types)
|
|
|
|
.includes(:categories)
|
|
|
|
.includes(:groups)
|
|
|
|
|
2024-04-12 05:11:00 +08:00
|
|
|
data = serialize_data(web_hooks, AdminWebHookSerializer, root: "web_hooks")
|
|
|
|
|
2024-07-02 04:08:48 +08:00
|
|
|
serialized_grouped_event_types =
|
|
|
|
WebHookEventType.active_grouped.transform_values do |array|
|
|
|
|
serialize_data(array, WebHookEventTypeSerializer)
|
|
|
|
end
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
json = {
|
2024-04-12 05:11:00 +08:00
|
|
|
web_hooks: data.delete("web_hooks"),
|
|
|
|
extras:
|
|
|
|
data.merge(
|
2024-07-02 04:08:48 +08:00
|
|
|
grouped_event_types: serialized_grouped_event_types,
|
|
|
|
default_event_types:
|
|
|
|
serialize_data(WebHook.default_event_types, WebHookEventTypeSerializer),
|
2024-04-12 05:11:00 +08:00
|
|
|
content_types: WebHook.content_types.map { |name, id| { id: id, name: name } },
|
|
|
|
delivery_statuses:
|
|
|
|
WebHook.last_delivery_statuses.map { |name, id| { id: id, name: name.to_s } },
|
|
|
|
),
|
2016-06-16 01:49:57 +08:00
|
|
|
total_rows_web_hooks: WebHook.count,
|
|
|
|
load_more_web_hooks:
|
|
|
|
admin_web_hooks_path(limit: limit, offset: offset + limit, format: :json),
|
|
|
|
}
|
|
|
|
|
|
|
|
render json: MultiJson.dump(json), status: 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2024-04-12 05:11:00 +08:00
|
|
|
data = serialize_data(@web_hook, AdminWebHookSerializer, root: "web_hook")
|
|
|
|
web_hook = data.delete("web_hook")
|
|
|
|
data = { "extras" => data, "web_hook" => web_hook }
|
|
|
|
render json: MultiJson.dump(data), status: 200
|
2016-06-16 01:49:57 +08:00
|
|
|
end
|
|
|
|
|
2022-12-13 08:53:08 +08:00
|
|
|
def edit
|
2024-04-12 05:11:00 +08:00
|
|
|
data = serialize_data(@web_hook, AdminWebHookSerializer, root: "web_hook")
|
|
|
|
data["extras"] = { "categories" => data.delete(:categories) }
|
|
|
|
render json: MultiJson.dump(data), status: 200
|
2022-12-13 08:53:08 +08:00
|
|
|
end
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
def create
|
|
|
|
web_hook = WebHook.new(web_hook_params)
|
|
|
|
|
|
|
|
if web_hook.save
|
2019-03-21 23:13:09 +08:00
|
|
|
StaffActionLogger.new(current_user).log_web_hook(
|
|
|
|
web_hook,
|
|
|
|
UserHistory.actions[:web_hook_create],
|
|
|
|
)
|
2016-06-16 01:49:57 +08:00
|
|
|
render_serialized(web_hook, AdminWebHookSerializer, root: "web_hook")
|
|
|
|
else
|
|
|
|
render_json_error web_hook.errors.full_messages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2019-04-29 15:32:25 +08:00
|
|
|
if @web_hook.update(web_hook_params)
|
2019-03-21 23:13:09 +08:00
|
|
|
StaffActionLogger.new(current_user).log_web_hook(
|
|
|
|
@web_hook,
|
|
|
|
UserHistory.actions[:web_hook_update],
|
|
|
|
changes: @web_hook.saved_changes,
|
|
|
|
)
|
2016-06-16 01:49:57 +08:00
|
|
|
render_serialized(@web_hook, AdminWebHookSerializer, root: "web_hook")
|
|
|
|
else
|
|
|
|
render_json_error @web_hook.errors.full_messages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@web_hook.destroy!
|
2019-03-21 23:13:09 +08:00
|
|
|
StaffActionLogger.new(current_user).log_web_hook(
|
|
|
|
@web_hook,
|
|
|
|
UserHistory.actions[:web_hook_destroy],
|
|
|
|
)
|
2016-06-16 01:49:57 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_events
|
|
|
|
limit = 50
|
|
|
|
offset = params[:offset].to_i
|
2024-07-09 04:43:16 +08:00
|
|
|
events = @web_hook.web_hook_events.includes(:redelivering_webhook_event)
|
2024-06-12 09:17:47 +08:00
|
|
|
status = params[:status]
|
|
|
|
if status == "successful"
|
2024-06-07 23:26:00 +08:00
|
|
|
events = events.successful
|
2024-06-12 09:17:47 +08:00
|
|
|
elsif status == "failed"
|
2024-06-07 23:26:00 +08:00
|
|
|
events = events.failed
|
|
|
|
end
|
|
|
|
|
|
|
|
total = events.count
|
|
|
|
events = events.limit(limit).offset(offset)
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
json = {
|
2024-06-07 23:26:00 +08:00
|
|
|
web_hook_events: serialize_data(events, AdminWebHookEventSerializer),
|
|
|
|
total_rows_web_hook_events: total,
|
2016-09-19 16:43:06 +08:00
|
|
|
load_more_web_hook_events:
|
2024-06-12 09:17:47 +08:00
|
|
|
web_hook_events_admin_api_index_path(
|
|
|
|
limit: limit,
|
|
|
|
offset: offset + limit,
|
|
|
|
status: status,
|
|
|
|
format: :json,
|
|
|
|
),
|
2016-06-16 01:49:57 +08:00
|
|
|
extras: {
|
|
|
|
web_hook_id: @web_hook.id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
render json: MultiJson.dump(json), status: 200
|
|
|
|
end
|
|
|
|
|
2016-09-14 07:54:53 +08:00
|
|
|
def bulk_events
|
|
|
|
params.require(:ids)
|
|
|
|
web_hook_events = @web_hook.web_hook_events.where(id: params[:ids])
|
|
|
|
render_serialized(web_hook_events, AdminWebHookEventSerializer)
|
|
|
|
end
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
def redeliver_event
|
2022-11-02 00:33:17 +08:00
|
|
|
web_hook_event = WebHookEvent.find_by(id: params[:event_id])
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
if web_hook_event
|
|
|
|
web_hook = web_hook_event.web_hook
|
2022-11-02 00:33:17 +08:00
|
|
|
emitter = WebHookEmitter.new(web_hook, web_hook_event)
|
|
|
|
emitter.emit!(headers: MultiJson.load(web_hook_event.headers), body: web_hook_event.payload)
|
2016-06-16 01:49:57 +08:00
|
|
|
render_serialized(web_hook_event, AdminWebHookEventSerializer, root: "web_hook_event")
|
|
|
|
else
|
|
|
|
render json: failed_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-09 04:43:16 +08:00
|
|
|
def redeliver_failed_events
|
|
|
|
web_hook_events =
|
|
|
|
@web_hook
|
|
|
|
.web_hook_events
|
|
|
|
.includes(:redelivering_webhook_event)
|
|
|
|
.not_ping
|
|
|
|
.where(id: params[:event_ids])
|
|
|
|
|
|
|
|
raise Discourse::InvalidParameters if web_hook_events.count.zero?
|
|
|
|
|
|
|
|
web_hook_events.each do |web_hook_event|
|
|
|
|
if !web_hook_event.redelivering_webhook_event
|
|
|
|
RedeliveringWebhookEvent.create!(web_hook_event: web_hook_event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
render json: { event_ids: web_hook_events.map(&:id) }
|
|
|
|
end
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
def ping
|
2017-04-27 05:23:37 +08:00
|
|
|
Jobs.enqueue(
|
|
|
|
:emit_web_hook_event,
|
|
|
|
web_hook_id: @web_hook.id,
|
|
|
|
event_type: "ping",
|
|
|
|
event_name: "ping",
|
|
|
|
)
|
2016-06-16 01:49:57 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def web_hook_params
|
|
|
|
params.require(:web_hook).permit(
|
|
|
|
:payload_url,
|
|
|
|
:content_type,
|
|
|
|
:secret,
|
|
|
|
:wildcard_web_hook,
|
|
|
|
:active,
|
|
|
|
:verify_certificate,
|
|
|
|
web_hook_event_type_ids: [],
|
|
|
|
group_ids: [],
|
2018-12-05 17:14:06 +08:00
|
|
|
tag_names: [],
|
2016-06-16 01:49:57 +08:00
|
|
|
category_ids: [],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_web_hook
|
|
|
|
@web_hook = WebHook.find(params[:id])
|
|
|
|
end
|
|
|
|
end
|