about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2022-06-09 21:57:36 +0200
committerGitHub <noreply@github.com>2022-06-09 21:57:36 +0200
commita2871cd74719a7a5a104daaa3dcc0e2670b7c2df (patch)
treecfd30fe202cd5be1c2984f4031d825d950784da2 /app/services
parent17ba5e1e616c853a389b9c24a347d873747f2126 (diff)
Add administrative webhooks (#18510)
* Add administrative webhooks

* Fix error when webhook is deleted before delivery worker runs
Diffstat (limited to 'app/services')
-rw-r--r--app/services/base_service.rb4
-rw-r--r--app/services/webhook_service.rb22
2 files changed, 26 insertions, 0 deletions
diff --git a/app/services/base_service.rb b/app/services/base_service.rb
index 99e8c875f..b0c0f9ec4 100644
--- a/app/services/base_service.rb
+++ b/app/services/base_service.rb
@@ -5,4 +5,8 @@ class BaseService
   include ActionView::Helpers::SanitizeHelper
 
   include RoutingHelper
+
+  def call(*)
+    raise NotImplementedError
+  end
 end
diff --git a/app/services/webhook_service.rb b/app/services/webhook_service.rb
new file mode 100644
index 000000000..aafa38318
--- /dev/null
+++ b/app/services/webhook_service.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class WebhookService < BaseService
+  def call(event, object)
+    @event  = Webhooks::EventPresenter.new(event, object)
+    @body   = serialize_event
+
+    webhooks_for_event.each do |webhook_id|
+      Webhooks::DeliveryWorker.perform_async(webhook_id, @body)
+    end
+  end
+
+  private
+
+  def webhooks_for_event
+    Webhook.enabled.where('? = ANY(events)', @event.type).pluck(:id)
+  end
+
+  def serialize_event
+    Oj.dump(ActiveModelSerializers::SerializableResource.new(@event, serializer: REST::Admin::WebhookEventSerializer, scope: nil, scope_name: :current_user).as_json)
+  end
+end