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:
parent
69498a58e9
commit
b4878cde6f
|
@ -26,6 +26,10 @@ class Notification < ActiveRecord::Base
|
||||||
after_commit :send_email, on: :create
|
after_commit :send_email, on: :create
|
||||||
after_commit :refresh_notification_count, on: [:create, :update, :destroy]
|
after_commit :refresh_notification_count, on: [:create, :update, :destroy]
|
||||||
|
|
||||||
|
after_commit(on: :create) do
|
||||||
|
DiscourseEvent.trigger(:notification_created, self)
|
||||||
|
end
|
||||||
|
|
||||||
def self.ensure_consistency!
|
def self.ensure_consistency!
|
||||||
DB.exec(<<~SQL, Notification.types[:private_message])
|
DB.exec(<<~SQL, Notification.types[:private_message])
|
||||||
DELETE
|
DELETE
|
||||||
|
|
|
@ -10,6 +10,7 @@ class WebHookEventType < ActiveRecord::Base
|
||||||
FLAG = 7
|
FLAG = 7
|
||||||
QUEUED_POST = 8
|
QUEUED_POST = 8
|
||||||
REVIEWABLE = 9
|
REVIEWABLE = 9
|
||||||
|
NOTIFICATION = 10
|
||||||
|
|
||||||
has_and_belongs_to_many :web_hooks
|
has_and_belongs_to_many :web_hooks
|
||||||
|
|
||||||
|
|
|
@ -106,3 +106,7 @@ DiscourseEvent.on(:reviewable_transitioned_to) do |status, reviewable|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
DiscourseEvent.on(:notification_created) do |notification|
|
||||||
|
WebHook.enqueue_object_hooks(:notification, notification, :notification_created, NotificationSerializer)
|
||||||
|
end
|
||||||
|
|
|
@ -3322,6 +3322,9 @@ en:
|
||||||
reviewable_event:
|
reviewable_event:
|
||||||
name: "Reviewable Event"
|
name: "Reviewable Event"
|
||||||
details: "When a new item is ready for review and when its status is updated."
|
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:
|
delivery_status:
|
||||||
title: "Delivery Status"
|
title: "Delivery Status"
|
||||||
inactive: "Inactive"
|
inactive: "Inactive"
|
||||||
|
|
|
@ -44,3 +44,8 @@ WebHookEventType.seed do |b|
|
||||||
b.id = WebHookEventType::REVIEWABLE
|
b.id = WebHookEventType::REVIEWABLE
|
||||||
b.name = "reviewable"
|
b.name = "reviewable"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
WebHookEventType.seed do |b|
|
||||||
|
b.id = WebHookEventType::NOTIFICATION
|
||||||
|
b.name = "notification"
|
||||||
|
end
|
||||||
|
|
|
@ -94,3 +94,11 @@ Fabricator(:reviewable_web_hook, from: :web_hook) do
|
||||||
web_hook.web_hook_event_types = [transients[:reviewable_hook]]
|
web_hook.web_hook_event_types = [transients[:reviewable_hook]]
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
|
@ -508,6 +508,16 @@ describe WebHook do
|
||||||
expect(payload["id"]).to eq(reviewable.id)
|
expect(payload["id"]).to eq(reviewable.id)
|
||||||
end
|
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
|
it 'should enqueue the right hooks for reviewables' do
|
||||||
Fabricate(:reviewable_web_hook)
|
Fabricate(:reviewable_web_hook)
|
||||||
reviewable = Fabricate(:reviewable)
|
reviewable = Fabricate(:reviewable)
|
||||||
|
|
Loading…
Reference in New Issue