workaround badly denormalized data in polls

This commit is contained in:
Sam 2018-08-17 11:15:29 +10:00
parent f9f490e957
commit eacb2593ee
2 changed files with 19 additions and 1 deletions

View File

@ -280,7 +280,8 @@ after_initialize do
User.where(id: user_ids).each do |user| User.where(id: user_ids).each do |user|
user_hash = UserNameSerializer.new(user).serializable_hash 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]) if (params[:option_id])
next unless option_id == params[:option_id].to_s next unless option_id == params[:option_id].to_s
end end

View File

@ -284,6 +284,23 @@ describe ::DiscoursePoll::PollsController do
# no user3 cause voter_limit is 2 # 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"][first].map { |h| h["id"] }.sort).to eq([user1.id, user2.id])
expect(json["poll"][second].map { |h| h["id"] }).to eq([user3.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
end end