From 5059dad8f0baebc602f3b4333b03a1d807488a15 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 25 Jul 2018 23:24:43 +0530 Subject: [PATCH 1/2] FEATURE: Webhook for post approval events --- app/models/queued_post.rb | 7 +++++ app/models/web_hook_event_type.rb | 1 + config/initializers/012-web_hook_events.rb | 10 +++++++ config/locales/client.en.yml | 3 ++ db/fixtures/007_web_hook_event_types.rb | 5 ++++ spec/fabricators/web_hook_fabricator.rb | 8 ++++++ spec/models/queued_post_spec.rb | 33 ++++++++++++++++++++++ spec/models/web_hook_spec.rb | 24 ++++++++++++++++ 8 files changed, 91 insertions(+) diff --git a/app/models/queued_post.rb b/app/models/queued_post.rb index 1f2afa75fec..568307825df 100644 --- a/app/models/queued_post.rb +++ b/app/models/queued_post.rb @@ -7,6 +7,8 @@ class QueuedPost < ActiveRecord::Base belongs_to :approved_by, class_name: "User" belongs_to :rejected_by, class_name: "User" + after_commit :trigger_queued_post_event, on: :create + def create_pending_action UserAction.log_action!(action_type: UserAction::PENDING, user_id: user_id, @@ -15,6 +17,11 @@ class QueuedPost < ActiveRecord::Base queued_post_id: id) end + def trigger_queued_post_event + DiscourseEvent.trigger(:queued_post, self) + true + end + def self.states @states ||= Enum.new(:new, :approved, :rejected) end diff --git a/app/models/web_hook_event_type.rb b/app/models/web_hook_event_type.rb index 7d6d595566f..7ba6569711e 100644 --- a/app/models/web_hook_event_type.rb +++ b/app/models/web_hook_event_type.rb @@ -6,6 +6,7 @@ class WebHookEventType < ActiveRecord::Base CATEGORY = 5 TAG = 6 FLAG = 7 + APPROVAL = 8 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 d96857c7236..f9b57d9570f 100644 --- a/config/initializers/012-web_hook_events.rb +++ b/config/initializers/012-web_hook_events.rb @@ -88,3 +88,13 @@ end WebHook.enqueue_object_hooks(:flag, flag, event) 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 diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index fd8245a8fa3..75e4c616c19 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3027,6 +3027,9 @@ en: flag_event: name: "Flag Event" 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: 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 8aa08591607..4b3291eb8d0 100644 --- a/db/fixtures/007_web_hook_event_types.rb +++ b/db/fixtures/007_web_hook_event_types.rb @@ -32,3 +32,8 @@ WebHookEventType.seed do |b| b.id = WebHookEventType::FLAG b.name = "flag" end + +WebHookEventType.seed do |b| + b.id = WebHookEventType::APPROVAL + b.name = "approval" +end diff --git a/spec/fabricators/web_hook_fabricator.rb b/spec/fabricators/web_hook_fabricator.rb index c7632cd2d07..300fa35e655 100644 --- a/spec/fabricators/web_hook_fabricator.rb +++ b/spec/fabricators/web_hook_fabricator.rb @@ -76,3 +76,11 @@ Fabricator(:flag_web_hook, from: :web_hook) do web_hook.web_hook_event_types = [transients[:flag_hook]] 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 diff --git a/spec/models/queued_post_spec.rb b/spec/models/queued_post_spec.rb index 5616ef41a0b..1b22d9f6b3c 100644 --- a/spec/models/queued_post_spec.rb +++ b/spec/models/queued_post_spec.rb @@ -165,4 +165,37 @@ describe QueuedPost do 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 diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 2e456118b13..034031585e2 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -417,5 +417,29 @@ describe WebHook do payload = JSON.parse(job_args["payload"]) expect(payload["id"]).to eq(post_action.id) 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 From af5b88f8e25259e6c0f1ecbb4ce72e1a3320567e Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 26 Jul 2018 10:29:38 +0530 Subject: [PATCH 2/2] Rename approval web hook event type to queued post --- app/models/queued_post.rb | 2 +- app/models/web_hook_event_type.rb | 2 +- config/initializers/012-web_hook_events.rb | 4 ++-- config/locales/client.en.yml | 6 +++--- db/fixtures/007_web_hook_event_types.rb | 4 ++-- spec/fabricators/web_hook_fabricator.rb | 6 +++--- spec/models/queued_post_spec.rb | 2 +- spec/models/web_hook_spec.rb | 6 +++--- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/models/queued_post.rb b/app/models/queued_post.rb index 568307825df..a4067f8c57a 100644 --- a/app/models/queued_post.rb +++ b/app/models/queued_post.rb @@ -18,7 +18,7 @@ class QueuedPost < ActiveRecord::Base end def trigger_queued_post_event - DiscourseEvent.trigger(:queued_post, self) + DiscourseEvent.trigger(:queued_post_created, self) true end diff --git a/app/models/web_hook_event_type.rb b/app/models/web_hook_event_type.rb index 7ba6569711e..8841bb64e48 100644 --- a/app/models/web_hook_event_type.rb +++ b/app/models/web_hook_event_type.rb @@ -6,7 +6,7 @@ class WebHookEventType < ActiveRecord::Base CATEGORY = 5 TAG = 6 FLAG = 7 - APPROVAL = 8 + QUEUED_POST = 8 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 f9b57d9570f..0834b875f62 100644 --- a/config/initializers/012-web_hook_events.rb +++ b/config/initializers/012-web_hook_events.rb @@ -90,11 +90,11 @@ end end %i( - queued_post + queued_post_created approved_post rejected_post ).each do |event| DiscourseEvent.on(event) do |queued_post| - WebHook.enqueue_object_hooks(:approval, queued_post, event, QueuedPostSerializer) + WebHook.enqueue_object_hooks(:queued_post, queued_post, event, QueuedPostSerializer) end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 75e4c616c19..1d29338f30c 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3027,9 +3027,9 @@ en: flag_event: name: "Flag Event" 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." + queued_post_event: + name: "Post Approval Event" + details: "When a new queued post is created, approved or rejected." 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 4b3291eb8d0..7e94839fd4a 100644 --- a/db/fixtures/007_web_hook_event_types.rb +++ b/db/fixtures/007_web_hook_event_types.rb @@ -34,6 +34,6 @@ WebHookEventType.seed do |b| end WebHookEventType.seed do |b| - b.id = WebHookEventType::APPROVAL - b.name = "approval" + b.id = WebHookEventType::QUEUED_POST + b.name = "queued_post" end diff --git a/spec/fabricators/web_hook_fabricator.rb b/spec/fabricators/web_hook_fabricator.rb index 300fa35e655..0197f143874 100644 --- a/spec/fabricators/web_hook_fabricator.rb +++ b/spec/fabricators/web_hook_fabricator.rb @@ -77,10 +77,10 @@ Fabricator(:flag_web_hook, from: :web_hook) do end end -Fabricator(:approval_web_hook, from: :web_hook) do - transient approval_hook: WebHookEventType.find_by(name: 'approval') +Fabricator(:queued_post_web_hook, from: :web_hook) do + transient queued_post_hook: WebHookEventType.find_by(name: 'queued_post') after_build do |web_hook, transients| - web_hook.web_hook_event_types = [transients[:approval_hook]] + web_hook.web_hook_event_types = [transients[:queued_post_hook]] end end diff --git a/spec/models/queued_post_spec.rb b/spec/models/queued_post_spec.rb index 1b22d9f6b3c..23c7dd8bdf6 100644 --- a/spec/models/queued_post_spec.rb +++ b/spec/models/queued_post_spec.rb @@ -171,7 +171,7 @@ describe QueuedPost do it 'triggers a extensibility event' do event = DiscourseEvent.track_events { subject.save! }.first - expect(event[:event_name]).to eq(:queued_post) + expect(event[:event_name]).to eq(:queued_post_created) expect(event[:params].first).to eq(subject) end end diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 034031585e2..d184b973c3a 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -418,12 +418,12 @@ describe WebHook do expect(payload["id"]).to eq(post_action.id) end - it 'should enqueue the right hooks for post approval events' do - Fabricate(:approval_web_hook) + it 'should enqueue the right hooks for queued post events' do + Fabricate(:queued_post_web_hook) queued_post = Fabricate(:queued_post) job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first - expect(job_args["event_name"]).to eq("queued_post") + expect(job_args["event_name"]).to eq("queued_post_created") payload = JSON.parse(job_args["payload"]) expect(payload["id"]).to eq(queued_post.id)