diff --git a/app/models/question.rb b/app/models/question.rb index 88b5565c..0a6adec0 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -5,7 +5,9 @@ class Question < ApplicationRecord has_many :answers, dependent: :destroy has_many :inboxes, dependent: :destroy - validates :content, length: { minimum: 1, maximum: 512 } + validates :content, length: { minimum: 1 } + + SHORT_QUESTION_MAX_LENGTH = 512 before_destroy do rep = Report.where(target_id: self.id, type: 'Reports::Question') @@ -30,4 +32,6 @@ class Question < ApplicationRecord def generated? = %w[justask retrospring_exporter].include?(author_identifier) def anonymous? = author_is_anonymous && author_identifier.present? + + def long? = content.length > SHORT_QUESTION_MAX_LENGTH end diff --git a/app/workers/question_worker.rb b/app/workers/question_worker.rb index c30c98d4..5d10904f 100644 --- a/app/workers/question_worker.rb +++ b/app/workers/question_worker.rb @@ -30,6 +30,7 @@ class QuestionWorker return true if follower.banned? return true if muted?(follower, question) return true if user.muting?(question.user) + return true if question.long? && !user.profile.allow_long_questions false end diff --git a/lib/errors.rb b/lib/errors.rb index 314ed4bc..5e2586c8 100644 --- a/lib/errors.rb +++ b/lib/errors.rb @@ -28,6 +28,9 @@ module Errors class InvalidBanDuration < BadRequest end + class QuestionTooLong < BadRequest + end + class Forbidden < Base def status 403 diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index a09a1ff7..51a4f3de 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -63,6 +63,7 @@ module UseCase def check_user raise Errors::NotAuthorized if target_user.privacy_require_user && !source_user_id + raise Errors::QuestionTooLong if content.length > ::Question::SHORT_QUESTION_MAX_LENGTH && !target_user.profile.allow_long_questions end def create_question diff --git a/spec/lib/use_case/question/create_spec.rb b/spec/lib/use_case/question/create_spec.rb index 607123b3..47b80ea4 100644 --- a/spec/lib/use_case/question/create_spec.rb +++ b/spec/lib/use_case/question/create_spec.rb @@ -61,7 +61,7 @@ describe UseCase::Question::Create do let(:content) { "a" * 513 } it "raises an error" do - expect { subject }.to raise_error(ActiveRecord::RecordInvalid) + expect { subject }.to raise_error(Errors::QuestionTooLong) end end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 2e88fc42..7da9eab0 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -16,11 +16,6 @@ RSpec.describe Question, :type => :model do expect(@question.content).to match 'Is this a question?' end - it 'does not save questions longer than 512 characters' do - @question.content = 'X' * 513 - expect{@question.save!}.to raise_error(ActiveRecord::RecordInvalid) - end - it 'has many answers' do 5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryBot.create(:user), question: @question) } expect(@question.answer_count).to match 5