Skip to content

Commit 52beeb0

Browse files
Gargronhiyuki2578
authored andcommitted
Fix various issues in polls (mastodon#10165)
* Fix ActivityPub poll results being serialized even with hide_totals * Fix poll refresh button having a different font size * Display poll in OpenGraph description * Fix NoMethodError when serializing votes Regression from mastodon#10158 * Fix polls on public pages being broken for non-logged-in users * Do not show time remaining if poll has no expiration date
1 parent 33c8f24 commit 52beeb0

6 files changed

Lines changed: 37 additions & 13 deletions

File tree

app/helpers/stream_entries_helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,19 @@ def status_text_summary(status)
104104
I18n.t('statuses.content_warning', warning: status.spoiler_text)
105105
end
106106

107+
def poll_summary(status)
108+
return unless status.poll
109+
status.poll.options.map { |o| "[ ] #{o}" }.join("\n")
110+
end
111+
107112
def status_description(status)
108113
components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')]
109-
components << status.text if status.spoiler_text.blank?
114+
115+
if status.spoiler_text.blank?
116+
components << status.text
117+
components << poll_summary(status)
118+
end
119+
110120
components.reject(&:blank?).join("\n\n")
111121
end
112122

app/javascript/mastodon/components/poll.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Poll extends ImmutablePureComponent {
118118
/>
119119

120120
{!showResults && <span className={classNames('poll__input', { checkbox: poll.get('multiple'), active })} />}
121-
{showResults && <span className='poll__number'>{Math.floor(percent)}%</span>}
121+
{showResults && <span className='poll__number'>{Math.round(percent)}%</span>}
122122

123123
{option.get('title')}
124124
</label>
@@ -146,7 +146,8 @@ class Poll extends ImmutablePureComponent {
146146
<div className='poll__footer'>
147147
{!showResults && <button className='button button-secondary' disabled={disabled} onClick={this.handleVote}><FormattedMessage id='poll.vote' defaultMessage='Vote' /></button>}
148148
{showResults && !this.props.disabled && <span><button className='poll__link' onClick={this.handleRefresh}><FormattedMessage id='poll.refresh' defaultMessage='Refresh' /></button> · </span>}
149-
<FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.get('votes_count') }} /> · {timeRemaining}
149+
<FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.get('votes_count') }} />
150+
{poll.get('expires_at') && <span> · {timeRemaining}</span>}
150151
</div>
151152
</div>
152153
);

app/javascript/styles/mastodon/polls.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
border: 0;
8383
color: $dark-text-color;
8484
text-decoration: underline;
85+
font-size: inherit;
8586

8687
&:hover,
8788
&:focus,

app/models/poll_vote.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class PollVote < ApplicationRecord
2323

2424
delegate :local?, to: :account
2525

26+
def object_type
27+
:vote
28+
end
29+
2630
private
2731

2832
def increment_counter_cache

app/serializers/activitypub/note_serializer.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
1515

1616
has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local?
1717

18-
has_many :poll_loaded_options, key: :one_of, if: :poll_and_not_multiple?
19-
has_many :poll_loaded_options, key: :any_of, if: :poll_and_multiple?
18+
has_many :poll_options, key: :one_of, if: :poll_and_not_multiple?
19+
has_many :poll_options, key: :any_of, if: :poll_and_multiple?
2020

2121
attribute :end_time, if: :poll_and_expires?
2222
attribute :closed, if: :poll_and_expired?
@@ -121,8 +121,12 @@ def local?
121121
object.account.local?
122122
end
123123

124-
def poll_loaded_options
125-
object.poll.loaded_options
124+
def poll_options
125+
if !object.expired? && object.hide_totals?
126+
object.poll.unloaded_options
127+
else
128+
object.poll.loaded_options
129+
end
126130
end
127131

128132
def poll_and_multiple?
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- options = (!poll.expired? && poll.hide_totals?) ? poll.unloaded_options : poll.loaded_options
2-
- voted = poll.votes.where(account: current_user.account).exists?
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?
33
- show_results = voted || poll.expired?
44

55
.poll
@@ -9,17 +9,21 @@
99
- if show_results
1010
- percent = 100 * option.votes_count / poll.votes_count
1111
%span.poll__chart{ style: "width: #{percent}%" }
12+
1213
%label.poll__text><
13-
%span.poll__number= percent
14+
%span.poll__number= percent.round
1415
= option.title
1516
- else
1617
%label.poll__text><
17-
%span.poll__input{ class: poll.multiple ? 'checkbox' : nil}><
18+
%span.poll__input{ class: poll.multiple? ? 'checkbox' : nil}><
1819
= option.title
1920
.poll__footer
2021
- unless show_results
2122
%button.button.button-secondary{ disabled: true }
2223
= t('statuses.poll.vote')
24+
2325
%span= t('statuses.poll.total_votes', count: poll.votes_count)
24-
·
25-
%span= poll.expires_at
26+
27+
- unless poll.expires_at.nil?
28+
·
29+
%span= l poll.expires_at

0 commit comments

Comments
 (0)