Skip to content

A platform for community discussion. Free, open, simple.

FIX: Avoid returning duplicate voters from `/polls/voters.json` endpoint (#34433)

Charles Lechasseur14ee6bd67d66
diff --git a/plugins/poll/lib/poll.rb b/plugins/poll/lib/poll.rb
index 724a4ece1b5..8d53910adf0 100644
--- a/plugins/poll/lib/poll.rb
+++ b/plugins/poll/lib/poll.rb
@@ -223,7 +223,7 @@ class DiscoursePoll::Poll
offset = (page - 1) * limit
params = {
- offset: offset,
+ offset: offset + 1,
offset_plus_limit: offset + limit,
option_digest: opts[:option_id].presence,
}
@@ -243,11 +243,12 @@ class DiscoursePoll::Poll
where_clause = params[:option_digest] ? "AND po.digest = :option_digest" : ""
query = <<~SQL.gsub("/* where */", where_clause)
- SELECT poll_id, digest, rank, user_id
+ SELECT poll_id, digest, user_id,
+ CASE int_rank WHEN 0 THEN 'Abstain' ELSE CAST(int_rank AS text) END AS rank
FROM (
SELECT pv.poll_id
, po.digest
- , CASE pv.rank WHEN 0 THEN 'Abstain' ELSE CAST(pv.rank AS text) END AS rank
+ , pv.rank AS int_rank
, pv.user_id
, u.username
, ROW_NUMBER() OVER (PARTITION BY pv.poll_option_id ORDER BY pv.created_at) AS row
@@ -258,7 +259,7 @@ class DiscoursePoll::Poll
/* where */
) v
WHERE row BETWEEN :offset AND :offset_plus_limit
- ORDER BY digest, CASE WHEN rank = 'Abstain' THEN 1 ELSE CAST(rank AS integer) END, username
+ ORDER BY digest, int_rank, username
SQL
votes = DB.query(query, params.merge(poll_ids: uncached_poll_ids))
diff --git a/plugins/poll/spec/lib/poll_spec.rb b/plugins/poll/spec/lib/poll_spec.rb
index ab1bbae250e..5a6c907768f 100644
--- a/plugins/poll/spec/lib/poll_spec.rb
+++ b/plugins/poll/spec/lib/poll_spec.rb
@@ -3,6 +3,7 @@
RSpec.describe DiscoursePoll::Poll do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:user_2, :user)
+ fab!(:user_3, :user)
fab!(:post_with_regular_poll) { Fabricate(:post, raw: <<~RAW) }
[poll]
@@ -337,4 +338,246 @@ RSpec.describe DiscoursePoll::Poll do
)
end
end
+
+ describe ".serialized_voters" do
+ context "with a regular poll" do
+ let(:post) { post_with_regular_poll }
+ let(:poll) { post.polls.first }
+ let(:poll_options) { poll.poll_options }
+ let(:votes) do
+ {
+ user => [poll_options.first.digest],
+ user_2 => [poll_options.second.digest],
+ user_3 => [poll_options.first.digest],
+ }
+ end
+
+ before do
+ votes.each_pair { |user, options| DiscoursePoll::Poll.vote(user, post.id, "poll", options) }
+ end
+
+ it "returns all serialized voters" do
+ voters = DiscoursePoll::Poll.serialized_voters(poll)
+ expect(voters).to eq(
+ {
+ poll_options.first.digest => [
+ UserNameSerializer.new(user).serializable_hash,
+ UserNameSerializer.new(user_3).serializable_hash,
+ ],
+ poll_options.second.digest => [UserNameSerializer.new(user_2).serializable_hash],
+ },
+ )
+ end
+
+ it "correctly paginates voters" do
+ opts = { page: 1, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ expect(voters).to eq(
+ {
+ poll_options.first.digest => [
+ UserNameSerializer.new(user).serializable_hash,
+ UserNameSerializer.new(user_3).serializable_hash,
+ ],
+ poll_options.second.digest => [UserNameSerializer.new(user_2).serializable_hash],
+ },
+ )
+
+ opts = { page: 2, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ expect(voters).to be_nil
+ end
+ end
+
+ context "with a multi-choice poll" do
+ let(:post) { post_with_multiple_poll }
+ let(:poll) { post.polls.first }
+ let(:poll_options) { poll.poll_options }
+ let(:votes) do
+ {
+ user => [poll_options.first.digest, poll_options.second.digest],
+ user_2 => [poll_options.second.digest, poll_options.third.digest],
+ user_3 => [
+ poll_options.second.digest,
+ poll_options.third.digest,
+ poll_options.fourth.digest,
+ ],
+ }
+ end
+
+ before do
+ votes.each_pair { |user, options| DiscoursePoll::Poll.vote(user, post.id, "poll", options) }
+ end
+
+ it "returns all serialized voters" do
+ voters = DiscoursePoll::Poll.serialized_voters(poll)
+ expect(voters).to eq(
+ {
+ poll_options.first.digest => [UserNameSerializer.new(user).serializable_hash],
+ poll_options.second.digest => [
+ UserNameSerializer.new(user).serializable_hash,
+ UserNameSerializer.new(user_2).serializable_hash,
+ UserNameSerializer.new(user_3).serializable_hash,
+ ],
+ poll_options.third.digest => [
+ UserNameSerializer.new(user_2).serializable_hash,
+ UserNameSerializer.new(user_3).serializable_hash,
+ ],
+ poll_options.fourth.digest => [UserNameSerializer.new(user_3).serializable_hash],
+ },
+ )
+ end
+
+ it "correctly paginates voters" do
+ opts = { page: 1, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ expect(voters).to eq(
+ {
+ poll_options.first.digest => [UserNameSerializer.new(user).serializable_hash],
+ poll_options.second.digest => [
+ UserNameSerializer.new(user).serializable_hash,
+ UserNameSerializer.new(user_2).serializable_hash,
+ ],
+ poll_options.third.digest => [
+ UserNameSerializer.new(user_2).serializable_hash,
+ UserNameSerializer.new(user_3).serializable_hash,
+ ],
+ poll_options.fourth.digest => [UserNameSerializer.new(user_3).serializable_hash],
+ },
+ )
+
+ opts = { page: 2, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ expect(voters).to eq(
+ { poll_options.second.digest => [UserNameSerializer.new(user_3).serializable_hash] },
+ )
+
+ opts = { page: 3, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ expect(voters).to be_nil
+ end
+ end
+
+ context "with a ranked choice poll" do
+ let(:post) { post_with_ranked_choice_poll }
+ let(:poll) { post.polls.first }
+ let(:poll_options) { poll.poll_options }
+ let(:votes) do
+ {
+ user => {
+ "0": {
+ digest: poll_options.first.digest,
+ rank: "0",
+ },
+ "1": {
+ digest: poll_options.second.digest,
+ rank: "1",
+ },
+ "2": {
+ digest: poll_options.third.digest,
+ rank: "2",
+ },
+ },
+ user_2 => {
+ "0": {
+ digest: poll_options.second.digest,
+ rank: "0",
+ },
+ "1": {
+ digest: poll_options.third.digest,
+ rank: "1",
+ },
+ "2": {
+ digest: poll_options.first.digest,
+ rank: "2",
+ },
+ },
+ user_3 => {
+ "0": {
+ digest: poll_options.third.digest,
+ rank: "0",
+ },
+ "1": {
+ digest: poll_options.first.digest,
+ rank: "1",
+ },
+ "2": {
+ digest: poll_options.second.digest,
+ rank: "2",
+ },
+ },
+ }
+ end
+
+ before do
+ votes.each_pair { |user, options| DiscoursePoll::Poll.vote(user, post.id, "poll", options) }
+ end
+
+ it "returns all serialized voters" do
+ voters = DiscoursePoll::Poll.serialized_voters(poll)
+ voters.transform_values! { |users| users.sort_by { |ranked_u| ranked_u[:user][:username] } }
+ expect(voters).to eq(
+ {
+ poll_options.first.digest => [
+ { user: UserNameSerializer.new(user).serializable_hash, rank: "Abstain" },
+ { user: UserNameSerializer.new(user_2).serializable_hash, rank: "2" },
+ { user: UserNameSerializer.new(user_3).serializable_hash, rank: "1" },
+ ],
+ poll_options.second.digest => [
+ { user: UserNameSerializer.new(user).serializable_hash, rank: "1" },
+ { user: UserNameSerializer.new(user_2).serializable_hash, rank: "Abstain" },
+ { user: UserNameSerializer.new(user_3).serializable_hash, rank: "2" },
+ ],
+ poll_options.third.digest => [
+ { user: UserNameSerializer.new(user).serializable_hash, rank: "2" },
+ { user: UserNameSerializer.new(user_2).serializable_hash, rank: "1" },
+ { user: UserNameSerializer.new(user_3).serializable_hash, rank: "Abstain" },
+ ],
+ },
+ )
+ end
+
+ it "correctly paginates voters" do
+ opts = { page: 1, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ voters.transform_values! { |users| users.sort_by { |ranked_u| ranked_u[:user][:username] } }
+ expect(voters).to eq(
+ {
+ poll_options.first.digest => [
+ { user: UserNameSerializer.new(user).serializable_hash, rank: "Abstain" },
+ { user: UserNameSerializer.new(user_2).serializable_hash, rank: "2" },
+ ],
+ poll_options.second.digest => [
+ { user: UserNameSerializer.new(user).serializable_hash, rank: "1" },
+ { user: UserNameSerializer.new(user_2).serializable_hash, rank: "Abstain" },
+ ],
+ poll_options.third.digest => [
+ { user: UserNameSerializer.new(user).serializable_hash, rank: "2" },
+ { user: UserNameSerializer.new(user_2).serializable_hash, rank: "1" },
+ ],
+ },
+ )
+
+ opts = { page: 2, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ voters.transform_values! { |users| users.sort_by { |ranked_u| ranked_u[:user][:username] } }
+ expect(voters).to eq(
+ {
+ poll_options.first.digest => [
+ { user: UserNameSerializer.new(user_3).serializable_hash, rank: "1" },
+ ],
+ poll_options.second.digest => [
+ { user: UserNameSerializer.new(user_3).serializable_hash, rank: "2" },
+ ],
+ poll_options.third.digest => [
+ { user: UserNameSerializer.new(user_3).serializable_hash, rank: "Abstain" },
+ ],
+ },
+ )
+
+ opts = { page: 3, limit: 2 }.with_indifferent_access
+ voters = DiscoursePoll::Poll.serialized_voters(poll, opts)
+ expect(voters).to be_nil
+ end
+ end
+ end
end