A platform for community discussion. Free, open, simple.
DEV: An endpoint to check if the current user voted in a poll. (#13648)
Roman Rizzi7925a76d93c0
diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rbindex 816b06ff648..fd428ab99fe 100644--- a/plugins/poll/plugin.rb+++ b/plugins/poll/plugin.rb@@ -398,6 +398,17 @@ after_initialize doendend+ def current_user_voted+ poll = Poll.includes(:post).find_by(id: params[:id])+ raise Discourse::NotFound.new(:id) if poll.nil?++ can_see_poll = Guardian.new(current_user).can_see_post?(poll.post)+ raise Discourse::NotFound.new(:id) if !can_see_poll++ presence = PollVote.where(poll: poll, user: current_user).exists?+ render json: { voted: presence }+ end+def toggle_statuspost_id = params.require(:post_id)poll_name = params.require(:poll_name)@@ -453,6 +464,7 @@ after_initialize doget "/voters" => 'polls#voters'get "/grouped_poll_results" => 'polls#grouped_poll_results'get "/groupable_user_fields" => 'polls#groupable_user_fields'+ get "/:id/votes/current_user_voted" => "polls#current_user_voted"endDiscourse::Application.routes.append dodiff --git a/plugins/poll/spec/controllers/polls_controller_spec.rb b/plugins/poll/spec/controllers/polls_controller_spec.rbindex 9534a5ca426..81628c7937b 100644--- a/plugins/poll/spec/controllers/polls_controller_spec.rb+++ b/plugins/poll/spec/controllers/polls_controller_spec.rb@@ -385,4 +385,48 @@ describe ::DiscoursePoll::PollsController doend+ describe '#current_user_voted' do+ let(:logged_user) { Fabricate(:user) }+ let(:post_with_poll) { Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]") }++ before { log_in_user(logged_user) }++ it 'returns true if the logged user already voted' do+ poll = post_with_poll.polls.last+ PollVote.create!(poll: poll, user: logged_user)++ get :current_user_voted, params: { id: poll.id }, format: :json+ parsed_body = JSON.parse(response.body)++ expect(response.status).to eq(200)+ expect(parsed_body['voted']).to eq(true)+ end++ it 'returns a 404 if there is no poll' do+ unknown_poll_id = 999999++ get :current_user_voted, params: { id: unknown_poll_id }, format: :json++ expect(response.status).to eq(404)+ end++ it "returns a 404 if the user doesn't have access to the poll" do+ pm_with_poll = Fabricate(:private_message_post, raw: "[poll]\n- A\n- B\n[/poll]")+ poll = pm_with_poll.polls.last++ get :current_user_voted, params: { id: poll.id }, format: :json++ expect(response.status).to eq(404)+ end++ it "returns false if the user didn't vote yet" do+ poll = post_with_poll.polls.last++ get :current_user_voted, params: { id: poll.id }, format: :json+ parsed_body = JSON.parse(response.body)++ expect(response.status).to eq(200)+ expect(parsed_body['voted']).to eq(false)+ end+ endend