Fix various issues in polls (#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 #10158

* Fix polls on public pages being broken for non-logged-in users

* Do not show time remaining if poll has no expiration date
signup-info-prompt
Eugen Rochko 2019-03-05 03:51:18 +01:00 committed by GitHub
parent 0a39c81dd8
commit a198add83b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 13 deletions

View File

@ -104,9 +104,19 @@ module StreamEntriesHelper
I18n.t('statuses.content_warning', warning: status.spoiler_text) I18n.t('statuses.content_warning', warning: status.spoiler_text)
end end
def poll_summary(status)
return unless status.poll
status.poll.options.map { |o| "[ ] #{o}" }.join("\n")
end
def status_description(status) def status_description(status)
components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')] components = [[media_summary(status), status_text_summary(status)].reject(&:blank?).join(' · ')]
components << status.text if status.spoiler_text.blank?
if status.spoiler_text.blank?
components << status.text
components << poll_summary(status)
end
components.reject(&:blank?).join("\n\n") components.reject(&:blank?).join("\n\n")
end end

View File

@ -118,7 +118,7 @@ class Poll extends ImmutablePureComponent {
/> />
{!showResults && <span className={classNames('poll__input', { checkbox: poll.get('multiple'), active })} />} {!showResults && <span className={classNames('poll__input', { checkbox: poll.get('multiple'), active })} />}
{showResults && <span className='poll__number'>{Math.floor(percent)}%</span>} {showResults && <span className='poll__number'>{Math.round(percent)}%</span>}
{option.get('title')} {option.get('title')}
</label> </label>
@ -146,7 +146,8 @@ class Poll extends ImmutablePureComponent {
<div className='poll__footer'> <div className='poll__footer'>
{!showResults && <button className='button button-secondary' disabled={disabled} onClick={this.handleVote}><FormattedMessage id='poll.vote' defaultMessage='Vote' /></button>} {!showResults && <button className='button button-secondary' disabled={disabled} onClick={this.handleVote}><FormattedMessage id='poll.vote' defaultMessage='Vote' /></button>}
{showResults && !this.props.disabled && <span><button className='poll__link' onClick={this.handleRefresh}><FormattedMessage id='poll.refresh' defaultMessage='Refresh' /></button> · </span>} {showResults && !this.props.disabled && <span><button className='poll__link' onClick={this.handleRefresh}><FormattedMessage id='poll.refresh' defaultMessage='Refresh' /></button> · </span>}
<FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.get('votes_count') }} /> · {timeRemaining} <FormattedMessage id='poll.total_votes' defaultMessage='{count, plural, one {# vote} other {# votes}}' values={{ count: poll.get('votes_count') }} />
{poll.get('expires_at') && <span> · {timeRemaining}</span>}
</div> </div>
</div> </div>
); );

View File

@ -82,6 +82,7 @@
border: 0; border: 0;
color: $dark-text-color; color: $dark-text-color;
text-decoration: underline; text-decoration: underline;
font-size: inherit;
&:hover, &:hover,
&:focus, &:focus,

View File

@ -23,6 +23,10 @@ class PollVote < ApplicationRecord
delegate :local?, to: :account delegate :local?, to: :account
def object_type
:vote
end
private private
def increment_counter_cache def increment_counter_cache

View File

@ -15,8 +15,8 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local? has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local?
has_many :poll_loaded_options, key: :one_of, if: :poll_and_not_multiple? has_many :poll_options, key: :one_of, if: :poll_and_not_multiple?
has_many :poll_loaded_options, key: :any_of, if: :poll_and_multiple? has_many :poll_options, key: :any_of, if: :poll_and_multiple?
attribute :end_time, if: :poll_and_expires? attribute :end_time, if: :poll_and_expires?
attribute :closed, if: :poll_and_expired? attribute :closed, if: :poll_and_expired?
@ -121,8 +121,12 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
object.account.local? object.account.local?
end end
def poll_loaded_options def poll_options
object.poll.loaded_options if !object.expired? && object.hide_totals?
object.poll.unloaded_options
else
object.poll.loaded_options
end
end end
def poll_and_multiple? def poll_and_multiple?

View File

@ -1,5 +1,5 @@
- options = (!poll.expired? && poll.hide_totals?) ? poll.unloaded_options : poll.loaded_options - options = (!poll.expired? && poll.hide_totals?) ? poll.unloaded_options : poll.loaded_options
- voted = poll.votes.where(account: current_user.account).exists? - voted = user_signed_in? && poll.votes.where(account: current_account).exists?
- show_results = voted || poll.expired? - show_results = voted || poll.expired?
.poll .poll
@ -9,17 +9,21 @@
- if show_results - if show_results
- percent = 100 * option.votes_count / poll.votes_count - percent = 100 * option.votes_count / poll.votes_count
%span.poll__chart{ style: "width: #{percent}%" } %span.poll__chart{ style: "width: #{percent}%" }
%label.poll__text>< %label.poll__text><
%span.poll__number= percent %span.poll__number= percent.round
= option.title = option.title
- else - else
%label.poll__text>< %label.poll__text><
%span.poll__input{ class: poll.multiple ? 'checkbox' : nil}>< %span.poll__input{ class: poll.multiple? ? 'checkbox' : nil}><
= option.title = option.title
.poll__footer .poll__footer
- unless show_results - unless show_results
%button.button.button-secondary{ disabled: true } %button.button.button-secondary{ disabled: true }
= t('statuses.poll.vote') = t('statuses.poll.vote')
%span= t('statuses.poll.total_votes', count: poll.votes_count) %span= t('statuses.poll.total_votes', count: poll.votes_count)
·
%span= poll.expires_at - unless poll.expires_at.nil?
·
%span= l poll.expires_at