diff --git a/app/controllers/discourse_topic_voting/votes_controller.rb b/app/controllers/discourse_topic_voting/votes_controller.rb index 22da37c..68906a1 100644 --- a/app/controllers/discourse_topic_voting/votes_controller.rb +++ b/app/controllers/discourse_topic_voting/votes_controller.rb @@ -70,6 +70,16 @@ def unvote votes_left: [(current_user.vote_limit - current_user.vote_count), 0].max, } + if WebHook.active_web_hooks(:topic_voting).exists? + payload = { + topic_id: topic_id, + topic_slug: topic.slug, + voter_id: current_user.id, + vote_count: obj[:vote_count], + } + WebHook.enqueue_topic_voting_hooks(:topic_unvote, topic, payload.to_json) + end + render json: obj end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 895f175..6a4a6bf 100755 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -47,4 +47,4 @@ en: web_hooks: topic_voting_event: name: "Topic Voting Event" - details: "When a topic receives an upvote" + details: "When a user votes or unvotes on a topic" diff --git a/spec/requests/votes_controller_spec.rb b/spec/requests/votes_controller_spec.rb index ce989de..baaaf19 100644 --- a/spec/requests/votes_controller_spec.rb +++ b/spec/requests/votes_controller_spec.rb @@ -59,4 +59,17 @@ expect(payload["voter_id"]).to eq(user.id) expect(payload["vote_count"]).to eq(1) end + + it "triggers a topic_unvote webhook when unvoting" do + Fabricate(:topic_voting_web_hook) + post "/voting/unvote.json", params: { topic_id: topic.id } + expect(response.status).to eq(200) + job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first + expect(job_args["event_name"]).to eq("topic_unvote") + payload = JSON.parse(job_args["payload"]) + expect(payload["topic_id"]).to eq(topic.id) + expect(payload["topic_slug"]).to eq(topic.slug) + expect(payload["voter_id"]).to eq(user.id) + expect(payload["vote_count"]).to eq(0) + end end