Skip to content

Commit af136d6

Browse files
authored
Immediately display poll results to poll author (mastodon#10187)
* Immediately display poll results to poll author * Refactor Poll#loaded_options and add Poll#voted? to improve DRYness
1 parent c4821a6 commit af136d6

4 files changed

Lines changed: 14 additions & 24 deletions

File tree

app/models/poll.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ class Poll < ApplicationRecord
4141
after_commit :reset_parent_cache, on: :update
4242

4343
def loaded_options
44-
options.map.with_index { |title, key| Option.new(self, key.to_s, title, cached_tallies[key]) }
45-
end
46-
47-
def unloaded_options
48-
options.map.with_index { |title, key| Option.new(self, key.to_s, title, nil) }
44+
options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? cached_tallies[key] : nil) }
4945
end
5046

5147
def possibly_stale?
5248
remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
5349
end
5450

51+
def voted?(account)
52+
account.id == account_id || votes.where(account: account).exists?
53+
end
54+
5555
delegate :local?, to: :account
5656

5757
def remote?
@@ -95,4 +95,8 @@ def last_fetched_before_expiration?
9595
def time_passed_since_last_fetch?
9696
last_fetched_at.nil? || last_fetched_at < 1.minute.ago
9797
end
98+
99+
def show_totals_now?
100+
expired? || !hide_totals?
101+
end
98102
end

app/serializers/activitypub/note_serializer.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,7 @@ def local?
122122
end
123123

124124
def poll_options
125-
if !object.poll.expired? && object.poll.hide_totals?
126-
object.poll.unloaded_options
127-
else
128-
object.poll.loaded_options
129-
end
125+
object.poll.loaded_options
130126
end
131127

132128
def poll_and_multiple?

app/serializers/rest/poll_serializer.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@ class REST::PollSerializer < ActiveModel::Serializer
44
attributes :id, :expires_at, :expired,
55
:multiple, :votes_count
66

7-
has_many :dynamic_options, key: :options
7+
has_many :loaded_options, key: :options
88

99
attribute :voted, if: :current_user?
1010

1111
def id
1212
object.id.to_s
1313
end
1414

15-
def dynamic_options
16-
if !object.expired? && object.hide_totals?
17-
object.unloaded_options
18-
else
19-
object.loaded_options
20-
end
21-
end
22-
2315
def expired
2416
object.expired?
2517
end
2618

2719
def voted
28-
object.votes.where(account: current_user.account).exists?
20+
object.voted?(current_user.account)
2921
end
3022

3123
def current_user?

app/views/stream_entries/_poll.html.haml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
- options = (!poll.expired? && poll.hide_totals?) ? poll.unloaded_options : poll.loaded_options
2-
- voted = user_signed_in? && poll.votes.where(account: current_account).exists?
3-
- show_results = voted || poll.expired?
1+
- show_results = (user_signed_in? && poll.voted?(current_account)) || poll.expired?
42

53
.poll
64
%ul
7-
- options.each do |option|
5+
- poll.loaded_options.each do |option|
86
%li
97
- if show_results
108
- percent = poll.votes_count > 0 ? 100 * option.votes_count / poll.votes_count : 0

0 commit comments

Comments
 (0)