From 230a012f0090c496fc5cdb011bcc8ed732fd0f5c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 3 Mar 2019 22:18:23 +0100 Subject: Add polls (#10111) * Add polls Fix #1629 * Add tests * Fixes * Change API for creating polls * Use name instead of content for votes * Remove poll validation for remote polls * Add polls to public pages * When updating the poll, update options just in case they were changed * Fix public pages showing both poll and other media --- app/validators/poll_validator.rb | 19 +++++++++++++++++++ app/validators/vote_validator.rb | 13 +++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 app/validators/poll_validator.rb create mode 100644 app/validators/vote_validator.rb (limited to 'app/validators') diff --git a/app/validators/poll_validator.rb b/app/validators/poll_validator.rb new file mode 100644 index 000000000..a1c4f6851 --- /dev/null +++ b/app/validators/poll_validator.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class PollValidator < ActiveModel::Validator + MAX_OPTIONS = 4 + MAX_OPTION_CHARS = 25 + MAX_EXPIRATION = 7.days.freeze + MIN_EXPIRATION = 1.day.freeze + + def validate(poll) + current_time = Time.now.utc + + poll.errors.add(:options, I18n.t('polls.errors.too_few_options')) unless poll.options.size > 1 + poll.errors.add(:options, I18n.t('polls.errors.too_many_options', max: MAX_OPTIONS)) if poll.options.size > MAX_OPTIONS + poll.errors.add(:options, I18n.t('polls.errors.over_character_limit', max: MAX_OPTION_CHARS)) if poll.options.any? { |option| option.mb_chars.grapheme_length > MAX_OPTION_CHARS } + poll.errors.add(:options, I18n.t('polls.errors.duplicate_options')) unless poll.options.uniq.size == poll.options.size + poll.errors.add(:expires_at, I18n.t('polls.errors.duration_too_long')) if poll.expires_at.nil? || poll.expires_at - current_time >= MAX_EXPIRATION + poll.errors.add(:expires_at, I18n.t('polls.errors.duration_too_short')) if poll.expires_at.present? && poll.expires_at - current_time <= MIN_EXPIRATION + end +end diff --git a/app/validators/vote_validator.rb b/app/validators/vote_validator.rb new file mode 100644 index 000000000..e2a68d1f3 --- /dev/null +++ b/app/validators/vote_validator.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class VoteValidator < ActiveModel::Validator + def validate(vote) + vote.errors.add(:base, I18n.t('polls.errors.expired')) if vote.poll.expired? + + if vote.poll.multiple? && vote.poll.votes.where(account: vote.account, choice: vote.choice).exists? + vote.errors.add(:base, I18n.t('polls.errors.already_voted')) + elsif vote.poll.votes.where(account: vote.account).exists? + vote.errors.add(:base, I18n.t('polls.errors.already_voted')) + end + end +end -- cgit From 8fe93b0701ea754fe8727b1d4ef11f7a33903f81 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 3 Mar 2019 23:41:30 +0100 Subject: Fix vote validation for polls with multiple choices (#10138) --- app/validators/vote_validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/validators') diff --git a/app/validators/vote_validator.rb b/app/validators/vote_validator.rb index e2a68d1f3..2e1818bdb 100644 --- a/app/validators/vote_validator.rb +++ b/app/validators/vote_validator.rb @@ -6,7 +6,7 @@ class VoteValidator < ActiveModel::Validator if vote.poll.multiple? && vote.poll.votes.where(account: vote.account, choice: vote.choice).exists? vote.errors.add(:base, I18n.t('polls.errors.already_voted')) - elsif vote.poll.votes.where(account: vote.account).exists? + elsif !vote.poll.multiple? && vote.poll.votes.where(account: vote.account).exists? vote.errors.add(:base, I18n.t('polls.errors.already_voted')) end end -- cgit From f2a1b8b96b031db3a479de029a29e04e28c55352 Mon Sep 17 00:00:00 2001 From: ThibG Date: Mon, 4 Mar 2019 22:46:38 +0100 Subject: Widen allowed time windows for polls (#10162) --- app/validators/poll_validator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/validators') diff --git a/app/validators/poll_validator.rb b/app/validators/poll_validator.rb index a1c4f6851..d4ae4c16a 100644 --- a/app/validators/poll_validator.rb +++ b/app/validators/poll_validator.rb @@ -3,8 +3,8 @@ class PollValidator < ActiveModel::Validator MAX_OPTIONS = 4 MAX_OPTION_CHARS = 25 - MAX_EXPIRATION = 7.days.freeze - MIN_EXPIRATION = 1.day.freeze + MAX_EXPIRATION = 1.month.freeze + MIN_EXPIRATION = 5.minutes.freeze def validate(poll) current_time = Time.now.utc -- cgit