mirror of
https://github.com/discourse/discourse.git
synced 2026-08-11 21:45:25 -05:00
DEV: Apply syntax_tree formatting to lib/*
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class PostValidator < ActiveModel::Validator
|
||||
|
||||
def validate(record)
|
||||
presence(record)
|
||||
|
||||
return if record.acting_user.try(:staged?)
|
||||
return if record.acting_user.try(:admin?) && Discourse.static_doc_topic_ids.include?(record.topic_id)
|
||||
if record.acting_user.try(:admin?) && Discourse.static_doc_topic_ids.include?(record.topic_id)
|
||||
return
|
||||
end
|
||||
|
||||
post_body_validator(record)
|
||||
max_posts_validator(record)
|
||||
@@ -23,9 +24,7 @@ class PostValidator < ActiveModel::Validator
|
||||
post.errors.add(:topic_id, :blank, **options) if post.topic_id.blank?
|
||||
end
|
||||
|
||||
if post.new_record? && post.user_id.nil?
|
||||
post.errors.add(:user_id, :blank, **options)
|
||||
end
|
||||
post.errors.add(:user_id, :blank, **options) if post.new_record? && post.user_id.nil?
|
||||
end
|
||||
|
||||
def post_body_validator(post)
|
||||
@@ -36,16 +35,21 @@ class PostValidator < ActiveModel::Validator
|
||||
end
|
||||
|
||||
def stripped_length(post)
|
||||
range = if private_message?(post)
|
||||
# private message
|
||||
SiteSetting.private_message_post_length
|
||||
elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0)
|
||||
# creating/editing first post
|
||||
post.topic&.featured_link&.present? ? (0..SiteSetting.max_post_length) : SiteSetting.first_post_length
|
||||
else
|
||||
# regular post
|
||||
SiteSetting.post_length
|
||||
end
|
||||
range =
|
||||
if private_message?(post)
|
||||
# private message
|
||||
SiteSetting.private_message_post_length
|
||||
elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0)
|
||||
# creating/editing first post
|
||||
if post.topic&.featured_link&.present?
|
||||
(0..SiteSetting.max_post_length)
|
||||
else
|
||||
SiteSetting.first_post_length
|
||||
end
|
||||
else
|
||||
# regular post
|
||||
SiteSetting.post_length
|
||||
end
|
||||
|
||||
StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
||||
end
|
||||
@@ -60,15 +64,31 @@ class PostValidator < ActiveModel::Validator
|
||||
return if post.acting_user.try(:staff?)
|
||||
|
||||
if acting_user_is_trusted?(post) || private_message?(post)
|
||||
add_error_if_count_exceeded(post, :no_mentions_allowed, :too_many_mentions, post.raw_mentions.size, SiteSetting.max_mentions_per_post)
|
||||
add_error_if_count_exceeded(
|
||||
post,
|
||||
:no_mentions_allowed,
|
||||
:too_many_mentions,
|
||||
post.raw_mentions.size,
|
||||
SiteSetting.max_mentions_per_post,
|
||||
)
|
||||
else
|
||||
add_error_if_count_exceeded(post, :no_mentions_allowed_newuser, :too_many_mentions_newuser, post.raw_mentions.size, SiteSetting.newuser_max_mentions_per_post)
|
||||
add_error_if_count_exceeded(
|
||||
post,
|
||||
:no_mentions_allowed_newuser,
|
||||
:too_many_mentions_newuser,
|
||||
post.raw_mentions.size,
|
||||
SiteSetting.newuser_max_mentions_per_post,
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def max_posts_validator(post)
|
||||
if post.new_record? && post.acting_user.present? && post.acting_user.posted_too_much_in_topic?(post.topic_id)
|
||||
post.errors.add(:base, I18n.t(:too_many_replies, count: SiteSetting.newuser_max_replies_per_topic))
|
||||
if post.new_record? && post.acting_user.present? &&
|
||||
post.acting_user.posted_too_much_in_topic?(post.topic_id)
|
||||
post.errors.add(
|
||||
:base,
|
||||
I18n.t(:too_many_replies, count: SiteSetting.newuser_max_replies_per_topic),
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -82,7 +102,7 @@ class PostValidator < ActiveModel::Validator
|
||||
:no_embedded_media_allowed_trust,
|
||||
:no_embedded_media_allowed_trust,
|
||||
post.embedded_media_count,
|
||||
0
|
||||
0,
|
||||
)
|
||||
elsif post.acting_user.trust_level == TrustLevel[0]
|
||||
add_error_if_count_exceeded(
|
||||
@@ -90,7 +110,7 @@ class PostValidator < ActiveModel::Validator
|
||||
:no_embedded_media_allowed,
|
||||
:too_many_embedded_media,
|
||||
post.embedded_media_count,
|
||||
SiteSetting.newuser_max_embedded_media
|
||||
SiteSetting.newuser_max_embedded_media,
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -98,7 +118,13 @@ class PostValidator < ActiveModel::Validator
|
||||
# Ensure new users can not put too many attachments in a post
|
||||
def max_attachments_validator(post)
|
||||
return if acting_user_is_trusted?(post) || private_message?(post)
|
||||
add_error_if_count_exceeded(post, :no_attachments_allowed, :too_many_attachments, post.attachment_count, SiteSetting.newuser_max_attachments)
|
||||
add_error_if_count_exceeded(
|
||||
post,
|
||||
:no_attachments_allowed,
|
||||
:too_many_attachments,
|
||||
post.attachment_count,
|
||||
SiteSetting.newuser_max_attachments,
|
||||
)
|
||||
end
|
||||
|
||||
def can_post_links_validator(post)
|
||||
@@ -117,7 +143,13 @@ class PostValidator < ActiveModel::Validator
|
||||
# Ensure new users can not put too many links in a post
|
||||
def newuser_links_validator(post)
|
||||
return if acting_user_is_trusted?(post) || private_message?(post)
|
||||
add_error_if_count_exceeded(post, :no_links_allowed, :too_many_links, post.link_count, SiteSetting.newuser_max_links)
|
||||
add_error_if_count_exceeded(
|
||||
post,
|
||||
:no_links_allowed,
|
||||
:too_many_links,
|
||||
post.link_count,
|
||||
SiteSetting.newuser_max_links,
|
||||
)
|
||||
end
|
||||
|
||||
# Stop us from posting the same thing too quickly
|
||||
@@ -129,13 +161,14 @@ class PostValidator < ActiveModel::Validator
|
||||
# If the post is empty, default to the validates_presence_of
|
||||
return if post.raw.blank?
|
||||
|
||||
if post.matches_recent_post?
|
||||
post.errors.add(:raw, I18n.t(:just_posted_that))
|
||||
end
|
||||
post.errors.add(:raw, I18n.t(:just_posted_that)) if post.matches_recent_post?
|
||||
end
|
||||
|
||||
def force_edit_last_validator(post)
|
||||
return if SiteSetting.max_consecutive_replies == 0 || post.id || post.acting_user&.staff? || private_message?(post)
|
||||
if SiteSetting.max_consecutive_replies == 0 || post.id || post.acting_user&.staff? ||
|
||||
private_message?(post)
|
||||
return
|
||||
end
|
||||
|
||||
topic = post.topic
|
||||
return if topic&.ordered_posts&.first&.user == post.user
|
||||
@@ -143,7 +176,9 @@ class PostValidator < ActiveModel::Validator
|
||||
guardian = Guardian.new(post.acting_user)
|
||||
return if guardian.is_category_group_moderator?(post.topic&.category)
|
||||
|
||||
last_posts_count = DB.query_single(<<~SQL, topic_id: post.topic_id, user_id: post.acting_user.id, max_replies: SiteSetting.max_consecutive_replies).first
|
||||
last_posts_count =
|
||||
DB.query_single(
|
||||
<<~SQL,
|
||||
SELECT COUNT(*)
|
||||
FROM (
|
||||
SELECT user_id
|
||||
@@ -156,10 +191,17 @@ class PostValidator < ActiveModel::Validator
|
||||
) c
|
||||
WHERE c.user_id = :user_id
|
||||
SQL
|
||||
topic_id: post.topic_id,
|
||||
user_id: post.acting_user.id,
|
||||
max_replies: SiteSetting.max_consecutive_replies,
|
||||
).first
|
||||
return if last_posts_count < SiteSetting.max_consecutive_replies
|
||||
|
||||
if guardian.can_edit?(topic.ordered_posts.last)
|
||||
post.errors.add(:base, I18n.t(:max_consecutive_replies, count: SiteSetting.max_consecutive_replies))
|
||||
post.errors.add(
|
||||
:base,
|
||||
I18n.t(:max_consecutive_replies, count: SiteSetting.max_consecutive_replies),
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -173,7 +215,13 @@ class PostValidator < ActiveModel::Validator
|
||||
post.topic.try(:private_message?)
|
||||
end
|
||||
|
||||
def add_error_if_count_exceeded(post, not_allowed_translation_key, limit_translation_key, current_count, max_count)
|
||||
def add_error_if_count_exceeded(
|
||||
post,
|
||||
not_allowed_translation_key,
|
||||
limit_translation_key,
|
||||
current_count,
|
||||
max_count
|
||||
)
|
||||
if current_count > max_count
|
||||
if max_count == 0
|
||||
post.errors.add(:base, I18n.t(not_allowed_translation_key))
|
||||
|
||||
Reference in New Issue
Block a user