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.
This commit is contained in:
Robin Ward 2019-08-15 14:45:30 -04:00
parent 69498a58e9
commit b4878cde6f
7 changed files with 35 additions and 0 deletions

View File

@ -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

View File

@ -10,6 +10,7 @@ class WebHookEventType < ActiveRecord::Base
FLAG = 7
QUEUED_POST = 8
REVIEWABLE = 9
NOTIFICATION = 10
has_and_belongs_to_many :web_hooks

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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)