FEATURE: Add a webhook for user notifications

FEATURE: Add a webhook for user notifications

If enabled, this will fire a webhook whenever a user’s notification has been created. This could potentially be a lot of data depending on your forum, and should be used carefully since it includes everything all users will see in their feeds.

diff --git a/app/models/notification.rb b/app/models/notification.rb
index fdfd006..b31812c 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -26,6 +26,10 @@ class Notification < ActiveRecord::Base
   after_commit :send_email, on: :create
   after_commit :refresh_notification_count, on: [:create, :update, :destroy]
 
+  after_commit(on: :create) do
+    DiscourseEvent.trigger(:notification_created, self)
+  end
+
   def self.ensure_consistency!
     DB.exec(<<~SQL, Notification.types[:private_message])
       DELETE
diff --git a/app/models/web_hook_event_type.rb b/app/models/web_hook_event_type.rb
index 297c117..719ceaa 100644
--- a/app/models/web_hook_event_type.rb
+++ b/app/models/web_hook_event_type.rb
@@ -10,6 +10,7 @@ class WebHookEventType < ActiveRecord::Base
   FLAG = 7
   QUEUED_POST = 8
   REVIEWABLE = 9
+  NOTIFICATION = 10
 
   has_and_belongs_to_many :web_hooks
 
diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb
index 1451a5d..d93faa0 100644
--- a/config/initializers/012-web_hook_events.rb
+++ b/config/initializers/012-web_hook_events.rb
@@ -106,3 +106,7 @@ DiscourseEvent.on(:reviewable_transitioned_to) do |status, reviewable|
     end
   end
 end
+
+DiscourseEvent.on(:notification_created) do |notification|
+  WebHook.enqueue_object_hooks(:notification, notification, :notification_created, NotificationSerializer)
+end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 583e886..940879d 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3322,6 +3322,9 @@ en:
         reviewable_event:
           name: "Reviewable Event"
           details: "When a new item is ready for review and when its status is updated."
+        notification_event:
+          name: "Notification Event"
+          details: "When a user receives a notification in their feed."
         delivery_status:
           title: "Delivery Status"
           inactive: "Inactive"
diff --git a/db/fixtures/007_web_hook_event_types.rb b/db/fixtures/007_web_hook_event_types.rb
index 4f3d2d1..0645cfa 100644
--- a/db/fixtures/007_web_hook_event_types.rb
+++ b/db/fixtures/007_web_hook_event_types.rb
@@ -44,3 +44,8 @@ WebHookEventType.seed do |b|
   b.id = WebHookEventType::REVIEWABLE
   b.name = "reviewable"
 end
+
+WebHookEventType.seed do |b|
+  b.id = WebHookEventType::NOTIFICATION
+  b.name = "notification"
+end
diff --git a/spec/fabricators/web_hook_fabricator.rb b/spec/fabricators/web_hook_fabricator.rb
index eff69df..6d03c7b 100644
--- a/spec/fabricators/web_hook_fabricator.rb
+++ b/spec/fabricators/web_hook_fabricator.rb
@@ -94,3 +94,11 @@ Fabricator(:reviewable_web_hook, from: :web_hook) do
     web_hook.web_hook_event_types = [transients[:reviewable_hook]]
   end
 end
+
+Fabricator(:notification_web_hook, from: :web_hook) do
+  transient notification_hook: WebHookEventType.find_by(name: 'notification')
+
+  after_build do |web_hook, transients|
+    web_hook.web_hook_event_types = [transients[:notification_hook]]
+  end
+end
diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb
index 92283e4..bc4b0f4 100644
--- a/spec/models/web_hook_spec.rb
+++ b/spec/models/web_hook_spec.rb
@@ -508,6 +508,16 @@ describe WebHook do
       expect(payload["id"]).to eq(reviewable.id)
     end
 
+    it 'should enqueue the right hooks for notifications' do
+      Fabricate(:notification_web_hook)
+      notification = Fabricate(:notification)
+      job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
+
+      expect(job_args["event_name"]).to eq("notification_created")
+      payload = JSON.parse(job_args["payload"])
+      expect(payload["id"]).to eq(notification.id)
+    end
+
     it 'should enqueue the right hooks for reviewables' do
       Fabricate(:reviewable_web_hook)
       reviewable = Fabricate(:reviewable)

GitHub sha: b4878cde

1 Like