From b7e8187cd4ef1b21d13dafd3364751c1226d7154 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 10 Mar 2024 20:30:21 +0100 Subject: [PATCH] Add max question content limit of 32,768 characters --- app/models/question.rb | 1 + lib/use_case/question/create.rb | 1 + lib/use_case/question/create_followers.rb | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/app/models/question.rb b/app/models/question.rb index 2e7549af..6426c640 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -11,6 +11,7 @@ class Question < ApplicationRecord validates :content, length: { minimum: 1 } SHORT_QUESTION_MAX_LENGTH = 512 + LONG_QUESTION_MAX_LENGTH = 32768 before_destroy do rep = Report.where(target_id: self.id, type: "Reports::Question") diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index 0a9a2db0..2f228816 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -65,6 +65,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 + raise Errors::QuestionTooLong if content.length > ::Question::LONG_QUESTION_MAX_LENGTH && target_user.profile.allow_long_questions end def create_question diff --git a/lib/use_case/question/create_followers.rb b/lib/use_case/question/create_followers.rb index 3f993dc0..1c88a5ff 100644 --- a/lib/use_case/question/create_followers.rb +++ b/lib/use_case/question/create_followers.rb @@ -9,6 +9,8 @@ module UseCase option :send_to_own_inbox, type: Types::Params::Bool, default: proc { false } def call + check_question + question = ::Question.create!( content:, author_is_anonymous: false, @@ -32,6 +34,10 @@ module UseCase private + def check_question + raise Errors::QuestionTooLong if content.length > ::Question::LONG_QUESTION_MAX_LENGTH + end + def increment_asked_count source_user.increment(:asked_count) source_user.save