Skip to content

Commit

Permalink
Immediately display poll results to poll author (mastodon#10187)
Browse files Browse the repository at this point in the history
* Immediately display poll results to poll author

* Refactor Poll#loaded_options and add Poll#voted? to improve DRYness
  • Loading branch information
Gargron authored Mar 7, 2019
1 parent c4821a6 commit af136d6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
14 changes: 9 additions & 5 deletions app/models/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ class Poll < ApplicationRecord
after_commit :reset_parent_cache, on: :update

def loaded_options
options.map.with_index { |title, key| Option.new(self, key.to_s, title, cached_tallies[key]) }
end

def unloaded_options
options.map.with_index { |title, key| Option.new(self, key.to_s, title, nil) }
options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? cached_tallies[key] : nil) }
end

def possibly_stale?
remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
end

def voted?(account)
account.id == account_id || votes.where(account: account).exists?
end

delegate :local?, to: :account

def remote?
Expand Down Expand Up @@ -95,4 +95,8 @@ def last_fetched_before_expiration?
def time_passed_since_last_fetch?
last_fetched_at.nil? || last_fetched_at < 1.minute.ago
end

def show_totals_now?
expired? || !hide_totals?
end
end
6 changes: 1 addition & 5 deletions app/serializers/activitypub/note_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ def local?
end

def poll_options
if !object.poll.expired? && object.poll.hide_totals?
object.poll.unloaded_options
else
object.poll.loaded_options
end
object.poll.loaded_options
end

def poll_and_multiple?
Expand Down
12 changes: 2 additions & 10 deletions app/serializers/rest/poll_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@ class REST::PollSerializer < ActiveModel::Serializer
attributes :id, :expires_at, :expired,
:multiple, :votes_count

has_many :dynamic_options, key: :options
has_many :loaded_options, key: :options

attribute :voted, if: :current_user?

def id
object.id.to_s
end

def dynamic_options
if !object.expired? && object.hide_totals?
object.unloaded_options
else
object.loaded_options
end
end

def expired
object.expired?
end

def voted
object.votes.where(account: current_user.account).exists?
object.voted?(current_user.account)
end

def current_user?
Expand Down
6 changes: 2 additions & 4 deletions app/views/stream_entries/_poll.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
- options = (!poll.expired? && poll.hide_totals?) ? poll.unloaded_options : poll.loaded_options
- voted = user_signed_in? && poll.votes.where(account: current_account).exists?
- show_results = voted || poll.expired?
- show_results = (user_signed_in? && poll.voted?(current_account)) || poll.expired?

.poll
%ul
- options.each do |option|
- poll.loaded_options.each do |option|
%li
- if show_results
- percent = poll.votes_count > 0 ? 100 * option.votes_count / poll.votes_count : 0
Expand Down

0 comments on commit af136d6

Please sign in to comment.