FEATURE: Show votes in an "on voted" poll to the creator

This required properly plumbing the guardian into the serializer.

Notably, the default state in the client was not changed - if you haven't voted in
the poll, you need to click the button to view the results instead of the results
being immediately visible on page load.

Implements https://meta.discourse.org/t/-/138108
This commit is contained in:
Kane York
2020-03-20 11:29:00 -07:00
committed by Kane York
parent 0d3386d255
commit 330102fd20
6 changed files with 30 additions and 11 deletions

View File

@ -62,7 +62,8 @@ after_initialize do
end
# user must be allowed to post in topic
if !Guardian.new(user).can_create_post?(post.topic)
guardian = Guardian.new(user)
if !guardian.can_create_post?(post.topic)
raise StandardError.new I18n.t("poll.user_cant_post_in_topic")
end
@ -108,7 +109,7 @@ after_initialize do
poll.reload
serialized_poll = PollSerializer.new(poll, root: false).as_json
serialized_poll = PollSerializer.new(poll, root: false, scope: guardian).as_json
payload = { post_id: post_id, polls: [serialized_poll] }
post.publish_message!("/polls/#{post.topic_id}", payload)
@ -120,6 +121,7 @@ after_initialize do
def toggle_status(post_id, poll_name, status, user, raise_errors = true)
Poll.transaction do
post = Post.find_by(id: post_id)
guardian = Guardian.new(user)
# post must not be deleted
if post.nil? || post.trashed?
@ -149,7 +151,7 @@ after_initialize do
poll.status = status
poll.save!
serialized_poll = PollSerializer.new(poll, root: false).as_json
serialized_poll = PollSerializer.new(poll, root: false, scope: guardian).as_json
payload = { post_id: post_id, polls: [serialized_poll] }
post.publish_message!("/polls/#{post.topic_id}", payload)
@ -542,11 +544,12 @@ after_initialize do
end
end
on(:post_created) do |post|
on(:post_created) do |post, _opts, user|
guardian = Guardian.new(user)
DiscoursePoll::Poll.schedule_jobs(post)
unless post.is_first_post?
polls = ActiveModel::ArraySerializer.new(post.polls, each_serializer: PollSerializer, root: false).as_json
polls = ActiveModel::ArraySerializer.new(post.polls, each_serializer: PollSerializer, root: false, scope: guardian).as_json
post.publish_message!("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
end
end
@ -594,7 +597,7 @@ after_initialize do
end
add_to_serializer(:post, :polls, false) do
preloaded_polls.map { |p| PollSerializer.new(p, root: false) }
preloaded_polls.map { |p| PollSerializer.new(p, root: false, scope: self.scope) }
end
add_to_serializer(:post, :include_polls?) do