Skip to content

Commit

Permalink
FEATURE: add topic_unvote event to trigger topic_voting webhook (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
KThompson-Lane-Unity committed Oct 3, 2023
1 parent 83ab47f commit ed1ded1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/controllers/discourse_topic_voting/votes_controller.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion config/locales/client.en.yml
Expand Up @@ -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"
13 changes: 13 additions & 0 deletions spec/requests/votes_controller_spec.rb
Expand Up @@ -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

0 comments on commit ed1ded1

Please sign in to comment.