Skip to content

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

workaround badly denormalized data in polls

Sameacb2593ee63
diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb
index c4a5adcf32a..85c9f5632cf 100644
--- a/plugins/poll/plugin.rb
+++ b/plugins/poll/plugin.rb
@@ -280,7 +280,8 @@ after_initialize do
User.where(id: user_ids).each do |user|
user_hash = UserNameSerializer.new(user).serializable_hash
- poll_votes[user.id.to_s][poll_name].each do |option_id|
+ # protect against poorly denormalized data
+ poll_votes.dig(user.id.to_s, poll_name)&.each do |option_id|
if (params[:option_id])
next unless option_id == params[:option_id].to_s
end
diff --git a/plugins/poll/spec/controllers/polls_controller_spec.rb b/plugins/poll/spec/controllers/polls_controller_spec.rb
index 7b97fcc6b43..5f377896bbe 100644
--- a/plugins/poll/spec/controllers/polls_controller_spec.rb
+++ b/plugins/poll/spec/controllers/polls_controller_spec.rb
@@ -284,6 +284,23 @@ describe ::DiscoursePoll::PollsController do
# no user3 cause voter_limit is 2
expect(json["poll"][first].map { |h| h["id"] }.sort).to eq([user1.id, user2.id])
expect(json["poll"][second].map { |h| h["id"] }).to eq([user3.id])
+
+ reloaded = Post.find(multi_poll.id)
+
+ # break the custom poll and make sure we still return something sane here
+ # TODO: normalize this data so we don't store the information twice and there is a chance
+ # that somehow a bg job can cause both fields to be out-of-sync
+ poll_votes = reloaded.custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD]
+ poll_votes.delete user2.id.to_s
+
+ reloaded.save_custom_fields(true)
+
+ get :voters, params: {
+ poll_name: 'poll', post_id: multi_poll.id, voter_limit: 2
+ }, format: :json
+
+ expect(response.status).to eq(200)
+
end
end