FEATURE: add reviewable score updated webhook (#12846)

Adds a webhook to notify when a reviewable score is updated.

This is different from created or status changed as additional flags can
roll in and update the score without updating status. Useful for applications
looking to integrate in with Discourse's scores
This commit is contained in:
Jeff Wong 2021-04-26 14:40:32 -10:00 committed by GitHub
parent 22d23c4af1
commit fa393b2956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -213,6 +213,8 @@ class Reviewable < ActiveRecord::Base
update(score: self.score + rs.score, latest_score: rs.created_at, force_review: force_review) update(score: self.score + rs.score, latest_score: rs.created_at, force_review: force_review)
topic.update(reviewable_score: topic.reviewable_score + rs.score) if topic topic.update(reviewable_score: topic.reviewable_score + rs.score) if topic
DiscourseEvent.trigger(:reviewable_score_updated, self)
rs rs
end end
@ -625,6 +627,10 @@ protected
) )
self.score = result[0].score self.score = result[0].score
DiscourseEvent.trigger(:reviewable_score_updated, self)
self.score
end end
def increment_version!(version = nil) def increment_version!(version = nil)

View File

@ -85,8 +85,13 @@ end
end end
end end
DiscourseEvent.on(:reviewable_created) do |reviewable| %i(
WebHook.enqueue_object_hooks(:reviewable, reviewable, :reviewable_created, reviewable.serializer) reviewable_created
reviewable_score_updated
).each do |event|
DiscourseEvent.on(event) do |reviewable|
WebHook.enqueue_object_hooks(:reviewable, reviewable, event, reviewable.serializer)
end
end end
DiscourseEvent.on(:reviewable_transitioned_to) do |status, reviewable| DiscourseEvent.on(:reviewable_transitioned_to) do |status, reviewable|

View File

@ -494,6 +494,17 @@ describe WebHook do
payload = JSON.parse(job_args["payload"]) payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(reviewable.id) expect(payload["id"]).to eq(reviewable.id)
reviewable.add_score(
Discourse.system_user,
ReviewableScore.types[:off_topic],
reason: "test"
)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first
expect(job_args["event_name"]).to eq("reviewable_score_updated")
payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(reviewable.id)
reviewable.perform(Discourse.system_user, :reject_user_delete) reviewable.perform(Discourse.system_user, :reject_user_delete)
job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first