FEATURE: Trigger topic webhook when topic status is updated.

This commit is contained in:
Guo Xiang Tan 2018-02-27 11:07:37 +08:00
parent 104df3126b
commit 66d620f7b1
3 changed files with 76 additions and 70 deletions

View File

@ -515,7 +515,7 @@ class Topic < ActiveRecord::Base
def update_status(status, enabled, user, opts = {}) def update_status(status, enabled, user, opts = {})
TopicStatusUpdater.new(self, user).update!(status, enabled, opts) TopicStatusUpdater.new(self, user).update!(status, enabled, opts)
DiscourseEvent.trigger(:topic_status_updated, self.id, status, enabled) DiscourseEvent.trigger(:topic_status_updated, self, status, enabled)
end end
# Atomically creates the next post number # Atomically creates the next post number

View File

@ -4,6 +4,10 @@
end end
end end
DiscourseEvent.on(:topic_status_updated) do |topic, status|
WebHook.enqueue_topic_hooks("topic_#{status}_status_updated", topic)
end
DiscourseEvent.on(:topic_created) do |topic, _, user| DiscourseEvent.on(:topic_created) do |topic, _, user|
WebHook.enqueue_topic_hooks(:topic_created, topic, user) WebHook.enqueue_topic_hooks(:topic_created, topic, user)
end end

View File

@ -123,24 +123,30 @@ describe WebHook do
it 'should enqueue the right hooks for topic events' do it 'should enqueue the right hooks for topic events' do
Fabricate(:topic_web_hook) Fabricate(:topic_web_hook)
Sidekiq::Testing.fake! do post = PostCreator.create(user, raw: 'post', title: 'topic', skip_validations: true)
post = PostCreator.create(user, raw: 'post', title: 'topic', skip_validations: true) topic_id = post.topic_id
topic_id = post.topic_id job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("topic_created")
expect(job_args["topic_id"]).to eq(topic_id)
PostDestroyer.new(user, post).destroy
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("topic_destroyed")
expect(job_args["topic_id"]).to eq(topic_id)
PostDestroyer.new(user, post).recover
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("topic_recovered")
expect(job_args["topic_id"]).to eq(topic_id)
%w{archived closed visible}.each do |status|
post.topic.update_status(status, true, topic.user)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("topic_created") expect(job_args["event_name"]).to eq("topic_#{status}_status_updated")
expect(job_args["topic_id"]).to eq(topic_id)
PostDestroyer.new(user, post).destroy
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("topic_destroyed")
expect(job_args["topic_id"]).to eq(topic_id)
PostDestroyer.new(user, post).recover
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("topic_recovered")
expect(job_args["topic_id"]).to eq(topic_id) expect(job_args["topic_id"]).to eq(topic_id)
end end
end end
@ -166,85 +172,81 @@ describe WebHook do
it 'should enqueue the right hooks for post events' do it 'should enqueue the right hooks for post events' do
Fabricate(:web_hook) Fabricate(:web_hook)
Sidekiq::Testing.fake! do user
user topic
topic
post = PostCreator.create(user, post = PostCreator.create(user,
raw: 'post', raw: 'post',
topic_id: topic.id, topic_id: topic.id,
reply_to_post_number: 1, reply_to_post_number: 1,
skip_validations: true skip_validations: true
) )
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
Sidekiq::Worker.clear_all Sidekiq::Worker.clear_all
expect(job_args["event_name"]).to eq("post_created") expect(job_args["event_name"]).to eq("post_created")
expect(job_args["post_id"]).to eq(post.id) expect(job_args["post_id"]).to eq(post.id)
# post destroy or recover triggers a moderator post # post destroy or recover triggers a moderator post
expect { PostDestroyer.new(user, post).destroy } expect { PostDestroyer.new(user, post).destroy }
.to change { Jobs::EmitWebHookEvent.jobs.count }.by(2) .to change { Jobs::EmitWebHookEvent.jobs.count }.by(2)
job_args = Jobs::EmitWebHookEvent.jobs.first["args"].first job_args = Jobs::EmitWebHookEvent.jobs.first["args"].first
expect(job_args["event_name"]).to eq("post_edited") expect(job_args["event_name"]).to eq("post_edited")
expect(job_args["post_id"]).to eq(post.id) expect(job_args["post_id"]).to eq(post.id)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("post_destroyed") expect(job_args["event_name"]).to eq("post_destroyed")
expect(job_args["post_id"]).to eq(post.id) expect(job_args["post_id"]).to eq(post.id)
PostDestroyer.new(user, post).recover PostDestroyer.new(user, post).recover
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("post_recovered") expect(job_args["event_name"]).to eq("post_recovered")
expect(job_args["post_id"]).to eq(post.id) expect(job_args["post_id"]).to eq(post.id)
end
end end
it 'should enqueue the right hooks for user events' do it 'should enqueue the right hooks for user events' do
Fabricate(:user_web_hook, active: true) Fabricate(:user_web_hook, active: true)
Sidekiq::Testing.fake! do user
user job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("user_created") expect(job_args["event_name"]).to eq("user_created")
expect(job_args["user_id"]).to eq(user.id) expect(job_args["user_id"]).to eq(user.id)
admin admin
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("user_created") expect(job_args["event_name"]).to eq("user_created")
expect(job_args["user_id"]).to eq(admin.id) expect(job_args["user_id"]).to eq(admin.id)
user.approve(admin) user.approve(admin)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("user_approved") expect(job_args["event_name"]).to eq("user_approved")
expect(job_args["user_id"]).to eq(user.id) expect(job_args["user_id"]).to eq(user.id)
UserUpdater.new(admin, user).update(username: 'testing123') UserUpdater.new(admin, user).update(username: 'testing123')
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("user_updated") expect(job_args["event_name"]).to eq("user_updated")
expect(job_args["user_id"]).to eq(user.id) expect(job_args["user_id"]).to eq(user.id)
user.logged_out user.logged_out
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("user_logged_out") expect(job_args["event_name"]).to eq("user_logged_out")
expect(job_args["user_id"]).to eq(user.id) expect(job_args["user_id"]).to eq(user.id)
user.logged_in user.logged_in
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("user_logged_in") expect(job_args["event_name"]).to eq("user_logged_in")
expect(job_args["user_id"]).to eq(user.id) expect(job_args["user_id"]).to eq(user.id)
end
end end
end end
end end