Respect allow long questions setting
This commit is contained in:
parent
e2f6284982
commit
3a6814b908
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -28,6 +28,9 @@ module Errors
|
|||
class InvalidBanDuration < BadRequest
|
||||
end
|
||||
|
||||
class QuestionTooLong < BadRequest
|
||||
end
|
||||
|
||||
class Forbidden < Base
|
||||
def status
|
||||
403
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue