diff --git a/app/models/queued_post.rb b/app/models/queued_post.rb index 1f2afa75fec..a4067f8c57a 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_created, 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..8841bb64e48 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 + 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 d96857c7236..0834b875f62 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_created + approved_post + rejected_post +).each do |event| + DiscourseEvent.on(event) do |queued_post| + 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 17e2fc9f73a..b42fbfd1705 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3028,6 +3028,9 @@ en: flag_event: name: "Flag Event" details: "When a flag is created, agreed, disagreed or ignored." + 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 8aa08591607..7e94839fd4a 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::QUEUED_POST + b.name = "queued_post" +end diff --git a/spec/fabricators/web_hook_fabricator.rb b/spec/fabricators/web_hook_fabricator.rb index c7632cd2d07..0197f143874 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(: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[:queued_post_hook]] + end +end diff --git a/spec/models/queued_post_spec.rb b/spec/models/queued_post_spec.rb index 5616ef41a0b..23c7dd8bdf6 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_created) + 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..d184b973c3a 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 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_created") + 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