FIX: preload poll voters
This commit is contained in:
parent
b907924c93
commit
0a708ba064
|
@ -9,7 +9,8 @@ class PollSerializer < ApplicationSerializer
|
||||||
:step,
|
:step,
|
||||||
:options,
|
:options,
|
||||||
:voters,
|
:voters,
|
||||||
:close
|
:close,
|
||||||
|
:preloaded_voters
|
||||||
|
|
||||||
def public
|
def public
|
||||||
true
|
true
|
||||||
|
@ -47,4 +48,12 @@ class PollSerializer < ApplicationSerializer
|
||||||
object.close_at.present?
|
object.close_at.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def preloaded_voters
|
||||||
|
DiscoursePoll::Poll.serialized_voters(object)
|
||||||
|
end
|
||||||
|
|
||||||
|
def include_preloaded_voters?
|
||||||
|
object.can_see_voters?(scope)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -120,6 +120,7 @@ createWidget("discourse-poll-voters", {
|
||||||
: result.voters[attrs.optionId];
|
: result.voters[attrs.optionId];
|
||||||
|
|
||||||
const existingVoters = new Set(state.voters.map(voter => voter.username));
|
const existingVoters = new Set(state.voters.map(voter => voter.username));
|
||||||
|
|
||||||
newVoters.forEach(voter => {
|
newVoters.forEach(voter => {
|
||||||
if (!existingVoters.has(voter.username)) {
|
if (!existingVoters.has(voter.username)) {
|
||||||
existingVoters.add(voter.username);
|
existingVoters.add(voter.username);
|
||||||
|
@ -202,8 +203,8 @@ createWidget("discourse-poll-standard-results", {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (isPublic && !state.loaded) {
|
if (isPublic && !state.loaded) {
|
||||||
|
state.voters = poll.get("preloaded_voters");
|
||||||
state.loaded = true;
|
state.loaded = true;
|
||||||
this.fetchVoters();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const percentages =
|
const percentages =
|
||||||
|
@ -290,8 +291,8 @@ createWidget("discourse-poll-number-results", {
|
||||||
|
|
||||||
if (poll.get("public")) {
|
if (poll.get("public")) {
|
||||||
if (!state.loaded) {
|
if (!state.loaded) {
|
||||||
|
state.voters = poll.get("preloaded_voters");
|
||||||
state.loaded = true;
|
state.loaded = true;
|
||||||
this.fetchVoters();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
contents.push(
|
contents.push(
|
||||||
|
|
|
@ -149,13 +149,7 @@ after_initialize do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def voters(post_id, poll_name, user, opts = {})
|
def serialized_voters(poll, opts = {})
|
||||||
post = Post.find_by(id: post_id)
|
|
||||||
raise Discourse::InvalidParameters.new("post_id is invalid") unless post
|
|
||||||
|
|
||||||
poll = Poll.find_by(post_id: post_id, name: poll_name)
|
|
||||||
raise Discourse::InvalidParameters.new("poll_name is invalid") unless poll&.can_see_voters?(user)
|
|
||||||
|
|
||||||
limit = (opts["limit"] || 25).to_i
|
limit = (opts["limit"] || 25).to_i
|
||||||
limit = 0 if limit < 0
|
limit = 0 if limit < 0
|
||||||
limit = 50 if limit > 50
|
limit = 50 if limit > 50
|
||||||
|
@ -208,7 +202,7 @@ after_initialize do
|
||||||
WHERE row BETWEEN #{offset} AND #{offset + limit}
|
WHERE row BETWEEN #{offset} AND #{offset + limit}
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
user_ids = votes.map { |v| v.user_id }.to_set
|
user_ids = votes.map(&:user_id).uniq
|
||||||
|
|
||||||
user_hashes = User
|
user_hashes = User
|
||||||
.where(id: user_ids)
|
.where(id: user_ids)
|
||||||
|
@ -225,6 +219,16 @@ after_initialize do
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def voters(post_id, poll_name, user, opts = {})
|
||||||
|
post = Post.find_by(id: post_id)
|
||||||
|
raise Discourse::InvalidParameters.new("post_id is invalid") unless post
|
||||||
|
|
||||||
|
poll = Poll.find_by(post_id: post_id, name: poll_name)
|
||||||
|
raise Discourse::InvalidParameters.new("poll_name is invalid") unless poll&.can_see_voters?(user)
|
||||||
|
|
||||||
|
serialized_voters(poll, opts)
|
||||||
|
end
|
||||||
|
|
||||||
def schedule_jobs(post)
|
def schedule_jobs(post)
|
||||||
Poll.where(post: post).find_each do |poll|
|
Poll.where(post: post).find_each do |poll|
|
||||||
job_args = {
|
job_args = {
|
||||||
|
|
Loading…
Reference in New Issue