2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-15 13:49:57 -04:00
|
|
|
class Admin::WebHooksController < Admin::AdminController
|
2023-01-09 07:20:10 -05:00
|
|
|
before_action :fetch_web_hook, only: %i[show update destroy list_events bulk_events ping]
|
2016-06-15 13:49:57 -04:00
|
|
|
|
|
|
|
def index
|
|
|
|
limit = 50
|
|
|
|
offset = params[:offset].to_i
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
web_hooks =
|
|
|
|
WebHook
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.includes(:web_hook_event_types)
|
|
|
|
.includes(:categories)
|
|
|
|
.includes(:groups)
|
2016-06-15 13:49:57 -04:00
|
|
|
|
|
|
|
json = {
|
|
|
|
web_hooks: serialize_data(web_hooks, AdminWebHookSerializer),
|
|
|
|
extras: {
|
2020-03-17 12:39:24 -04:00
|
|
|
event_types: WebHookEventType.active,
|
2016-06-15 13:49:57 -04:00
|
|
|
default_event_types: WebHook.default_event_types,
|
|
|
|
content_types: WebHook.content_types.map { |name, id| { id: id, name: name } },
|
2023-01-09 07:20:10 -05:00
|
|
|
delivery_statuses:
|
|
|
|
WebHook.last_delivery_statuses.map { |name, id| { id: id, name: name.to_s } },
|
2016-06-15 13:49:57 -04:00
|
|
|
},
|
|
|
|
total_rows_web_hooks: WebHook.count,
|
2023-01-09 07:20:10 -05:00
|
|
|
load_more_web_hooks:
|
|
|
|
admin_web_hooks_path(limit: limit, offset: offset + limit, format: :json),
|
2016-06-15 13:49:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
render json: MultiJson.dump(json), status: 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2023-01-09 07:20:10 -05:00
|
|
|
render_serialized(@web_hook, AdminWebHookSerializer, root: "web_hook")
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
|
2022-12-12 19:53:08 -05:00
|
|
|
def edit
|
2023-01-09 07:20:10 -05:00
|
|
|
render_serialized(@web_hook, AdminWebHookSerializer, root: "web_hook")
|
2022-12-12 19:53:08 -05:00
|
|
|
end
|
|
|
|
|
2016-06-15 13:49:57 -04:00
|
|
|
def create
|
|
|
|
web_hook = WebHook.new(web_hook_params)
|
|
|
|
|
|
|
|
if web_hook.save
|
2023-01-09 07:20:10 -05:00
|
|
|
StaffActionLogger.new(current_user).log_web_hook(
|
|
|
|
web_hook,
|
|
|
|
UserHistory.actions[:web_hook_create],
|
|
|
|
)
|
|
|
|
render_serialized(web_hook, AdminWebHookSerializer, root: "web_hook")
|
2016-06-15 13:49:57 -04:00
|
|
|
else
|
|
|
|
render_json_error web_hook.errors.full_messages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2019-04-29 03:32:25 -04:00
|
|
|
if @web_hook.update(web_hook_params)
|
2023-01-09 07:20:10 -05:00
|
|
|
StaffActionLogger.new(current_user).log_web_hook(
|
|
|
|
@web_hook,
|
|
|
|
UserHistory.actions[:web_hook_update],
|
|
|
|
changes: @web_hook.saved_changes,
|
|
|
|
)
|
|
|
|
render_serialized(@web_hook, AdminWebHookSerializer, root: "web_hook")
|
2016-06-15 13:49:57 -04:00
|
|
|
else
|
|
|
|
render_json_error @web_hook.errors.full_messages
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@web_hook.destroy!
|
2023-01-09 07:20:10 -05:00
|
|
|
StaffActionLogger.new(current_user).log_web_hook(
|
|
|
|
@web_hook,
|
|
|
|
UserHistory.actions[:web_hook_destroy],
|
|
|
|
)
|
2016-06-15 13:49:57 -04:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_events
|
|
|
|
limit = 50
|
|
|
|
offset = params[:offset].to_i
|
|
|
|
|
|
|
|
json = {
|
2023-01-09 07:20:10 -05:00
|
|
|
web_hook_events:
|
|
|
|
serialize_data(
|
|
|
|
@web_hook.web_hook_events.limit(limit).offset(offset),
|
|
|
|
AdminWebHookEventSerializer,
|
|
|
|
),
|
2016-06-15 13:49:57 -04:00
|
|
|
total_rows_web_hook_events: @web_hook.web_hook_events.count,
|
2023-01-09 07:20:10 -05:00
|
|
|
load_more_web_hook_events:
|
|
|
|
web_hook_events_admin_api_index_path(limit: limit, offset: offset + limit, format: :json),
|
2016-06-15 13:49:57 -04:00
|
|
|
extras: {
|
2023-01-09 07:20:10 -05:00
|
|
|
web_hook_id: @web_hook.id,
|
|
|
|
},
|
2016-06-15 13:49:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
render json: MultiJson.dump(json), status: 200
|
|
|
|
end
|
|
|
|
|
2016-09-13 19:54:53 -04: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-15 13:49:57 -04:00
|
|
|
def redeliver_event
|
2022-11-01 12:33:17 -04:00
|
|
|
web_hook_event = WebHookEvent.find_by(id: params[:event_id])
|
2016-06-15 13:49:57 -04:00
|
|
|
|
|
|
|
if web_hook_event
|
|
|
|
web_hook = web_hook_event.web_hook
|
2022-11-01 12:33:17 -04:00
|
|
|
emitter = WebHookEmitter.new(web_hook, web_hook_event)
|
|
|
|
emitter.emit!(headers: MultiJson.load(web_hook_event.headers), body: web_hook_event.payload)
|
2023-01-09 07:20:10 -05:00
|
|
|
render_serialized(web_hook_event, AdminWebHookEventSerializer, root: "web_hook_event")
|
2016-06-15 13:49:57 -04:00
|
|
|
else
|
|
|
|
render json: failed_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ping
|
2023-01-09 07:20:10 -05:00
|
|
|
Jobs.enqueue(
|
|
|
|
:emit_web_hook_event,
|
|
|
|
web_hook_id: @web_hook.id,
|
|
|
|
event_type: "ping",
|
|
|
|
event_name: "ping",
|
|
|
|
)
|
2016-06-15 13:49:57 -04:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def web_hook_params
|
2023-01-09 07:20:10 -05:00
|
|
|
params.require(:web_hook).permit(
|
|
|
|
:payload_url,
|
|
|
|
:content_type,
|
|
|
|
:secret,
|
|
|
|
:wildcard_web_hook,
|
|
|
|
:active,
|
|
|
|
:verify_certificate,
|
|
|
|
web_hook_event_type_ids: [],
|
|
|
|
group_ids: [],
|
|
|
|
tag_names: [],
|
|
|
|
category_ids: [],
|
|
|
|
)
|
2016-06-15 13:49:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_web_hook
|
|
|
|
@web_hook = WebHook.find(params[:id])
|
|
|
|
end
|
|
|
|
end
|