FEATURE: Webhook for post approval events

This commit is contained in:
Vinoth Kannan 2018-07-25 23:24:43 +05:30
parent 796639a797
commit 5059dad8f0
8 changed files with 91 additions and 0 deletions

View File

@ -7,6 +7,8 @@ class QueuedPost < ActiveRecord::Base
belongs_to :approved_by, class_name: "User" belongs_to :approved_by, class_name: "User"
belongs_to :rejected_by, class_name: "User" belongs_to :rejected_by, class_name: "User"
after_commit :trigger_queued_post_event, on: :create
def create_pending_action def create_pending_action
UserAction.log_action!(action_type: UserAction::PENDING, UserAction.log_action!(action_type: UserAction::PENDING,
user_id: user_id, user_id: user_id,
@ -15,6 +17,11 @@ class QueuedPost < ActiveRecord::Base
queued_post_id: id) queued_post_id: id)
end end
def trigger_queued_post_event
DiscourseEvent.trigger(:queued_post, self)
true
end
def self.states def self.states
@states ||= Enum.new(:new, :approved, :rejected) @states ||= Enum.new(:new, :approved, :rejected)
end end

View File

@ -6,6 +6,7 @@ class WebHookEventType < ActiveRecord::Base
CATEGORY = 5 CATEGORY = 5
TAG = 6 TAG = 6
FLAG = 7 FLAG = 7
APPROVAL = 8
has_and_belongs_to_many :web_hooks has_and_belongs_to_many :web_hooks

View File

@ -88,3 +88,13 @@ end
WebHook.enqueue_object_hooks(:flag, flag, event) WebHook.enqueue_object_hooks(:flag, flag, event)
end end
end end
%i(
queued_post
approved_post
rejected_post
).each do |event|
DiscourseEvent.on(event) do |queued_post|
WebHook.enqueue_object_hooks(:approval, queued_post, event, QueuedPostSerializer)
end
end

View File

@ -3027,6 +3027,9 @@ en:
flag_event: flag_event:
name: "Flag Event" name: "Flag Event"
details: "When a flag is created, agreed, disagreed or ignored." details: "When a flag is created, agreed, disagreed or ignored."
approval_event:
name: "Approval Event"
details: "When a new post is queued, approved or rejected."
delivery_status: delivery_status:
title: "Delivery Status" title: "Delivery Status"
inactive: "Inactive" inactive: "Inactive"

View File

@ -32,3 +32,8 @@ WebHookEventType.seed do |b|
b.id = WebHookEventType::FLAG b.id = WebHookEventType::FLAG
b.name = "flag" b.name = "flag"
end end
WebHookEventType.seed do |b|
b.id = WebHookEventType::APPROVAL
b.name = "approval"
end

View File

@ -76,3 +76,11 @@ Fabricator(:flag_web_hook, from: :web_hook) do
web_hook.web_hook_event_types = [transients[:flag_hook]] web_hook.web_hook_event_types = [transients[:flag_hook]]
end end
end end
Fabricator(:approval_web_hook, from: :web_hook) do
transient approval_hook: WebHookEventType.find_by(name: 'approval')
after_build do |web_hook, transients|
web_hook.web_hook_event_types = [transients[:approval_hook]]
end
end

View File

@ -165,4 +165,37 @@ describe QueuedPost do
end end
end end
describe 'create' do
subject { Fabricate.build(:queued_post) }
it 'triggers a extensibility event' do
event = DiscourseEvent.track_events { subject.save! }.first
expect(event[:event_name]).to eq(:queued_post)
expect(event[:params].first).to eq(subject)
end
end
describe 'approve' do
subject { Fabricate(:queued_post) }
it 'triggers a extensibility event' do
event = DiscourseEvent.track_events { subject.approve!(Discourse.system_user) }.last
expect(event[:event_name]).to eq(:approved_post)
expect(event[:params].first).to eq(subject)
end
end
describe 'reject' do
subject { Fabricate(:queued_post) }
it 'triggers a extensibility event' do
event = DiscourseEvent.track_events { subject.reject!(Discourse.system_user) }.last
expect(event[:event_name]).to eq(:rejected_post)
expect(event[:params].first).to eq(subject)
end
end
end end

View File

@ -417,5 +417,29 @@ describe WebHook do
payload = JSON.parse(job_args["payload"]) payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(post_action.id) expect(payload["id"]).to eq(post_action.id)
end end
it 'should enqueue the right hooks for post approval events' do
Fabricate(:approval_web_hook)
queued_post = Fabricate(:queued_post)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("queued_post")
payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(queued_post.id)
queued_post.approve!(Discourse.system_user)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("approved_post")
payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(queued_post.id)
queued_post.reject!(Discourse.system_user)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("rejected_post")
payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(queued_post.id)
end
end end
end end