2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
class PostValidator < ActiveModel::Validator
|
2013-06-09 12:48:44 -04:00
|
|
|
def validate(record)
|
2013-06-13 04:18:17 -04:00
|
|
|
presence(record)
|
2015-12-01 04:40:23 -05:00
|
|
|
|
2024-05-07 12:06:16 -04:00
|
|
|
return if record.acting_user&.staged?
|
|
|
|
return if record.acting_user&.admin? && Discourse.static_doc_topic_ids.include?(record.topic_id)
|
2015-12-01 04:40:23 -05:00
|
|
|
|
2016-12-05 07:31:43 -05:00
|
|
|
post_body_validator(record)
|
2015-12-01 04:40:23 -05:00
|
|
|
max_posts_validator(record)
|
|
|
|
unique_post_validator(record)
|
2013-06-09 12:48:44 -04:00
|
|
|
|
2024-05-07 12:06:16 -04:00
|
|
|
# These validators might cook the post or do SQL queries.
|
|
|
|
# So we only run them if the post is otherwise valid.
|
|
|
|
if record.errors.empty?
|
|
|
|
max_mention_validator(record)
|
|
|
|
max_embedded_media_validator(record)
|
|
|
|
max_attachments_validator(record)
|
|
|
|
max_links_validator(record)
|
|
|
|
force_edit_last_validator(record)
|
2013-06-13 04:18:17 -04:00
|
|
|
end
|
2024-05-07 12:06:16 -04:00
|
|
|
end
|
2015-03-26 16:57:50 -04:00
|
|
|
|
2024-05-07 12:06:16 -04:00
|
|
|
def presence(post)
|
|
|
|
post.errors.add(:topic_id, :blank, **options) if !options[:skip_topic] && post.topic_id.blank?
|
|
|
|
post.errors.add(:user_id, :blank, **options) if post.new_record? && post.user_id.blank?
|
2013-06-13 04:18:17 -04:00
|
|
|
end
|
|
|
|
|
2016-12-05 07:31:43 -05:00
|
|
|
def post_body_validator(post)
|
2017-04-26 23:53:53 -04:00
|
|
|
return if options[:skip_post_body] || post.topic&.pm_with_non_human_user?
|
2016-12-05 07:31:43 -05:00
|
|
|
stripped_length(post)
|
|
|
|
raw_quality(post)
|
2019-10-01 20:38:34 -04:00
|
|
|
WatchedWordsValidator.new(attributes: [:raw]).validate(post) if !post.acting_user&.staged
|
2016-12-05 07:31:43 -05:00
|
|
|
end
|
|
|
|
|
2013-06-13 04:18:17 -04:00
|
|
|
def stripped_length(post)
|
2016-04-18 16:08:42 -04:00
|
|
|
range =
|
|
|
|
if private_message?(post)
|
2015-03-19 10:17:55 -04:00
|
|
|
SiteSetting.private_message_post_length
|
2015-11-30 13:08:35 -05:00
|
|
|
elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0)
|
2024-05-27 06:27:13 -04:00
|
|
|
if post.topic&.featured_link.present?
|
2016-12-09 15:46:26 -05:00
|
|
|
(0..SiteSetting.max_post_length)
|
2023-01-09 07:10:19 -05:00
|
|
|
else
|
2016-12-09 15:46:26 -05:00
|
|
|
SiteSetting.first_post_length
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2015-03-19 10:17:55 -04:00
|
|
|
else
|
|
|
|
SiteSetting.post_length
|
|
|
|
end
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
2013-06-13 04:18:17 -04:00
|
|
|
end
|
|
|
|
|
2024-05-07 12:06:16 -04:00
|
|
|
def max_posts_validator(post)
|
|
|
|
if post.new_record? && 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
|
|
|
|
|
|
|
|
def unique_post_validator(post)
|
|
|
|
return if SiteSetting.unique_posts_mins == 0
|
|
|
|
return if post.skip_unique_check
|
|
|
|
return if post.acting_user&.staff?
|
|
|
|
return if post.raw.blank?
|
|
|
|
|
|
|
|
post.errors.add(:raw, I18n.t(:just_posted_that)) if post.matches_recent_post?
|
2013-06-09 12:48:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def max_mention_validator(post)
|
2024-05-07 12:06:16 -04:00
|
|
|
return if post.acting_user&.staff?
|
2016-02-01 10:37:49 -05:00
|
|
|
|
2016-04-18 16:08:42 -04:00
|
|
|
if acting_user_is_trusted?(post) || private_message?(post)
|
2015-11-13 15:19:46 -05:00
|
|
|
add_error_if_count_exceeded(
|
|
|
|
post,
|
|
|
|
:no_mentions_allowed,
|
|
|
|
:too_many_mentions,
|
|
|
|
post.raw_mentions.size,
|
|
|
|
SiteSetting.max_mentions_per_post,
|
|
|
|
)
|
2013-06-09 12:48:44 -04:00
|
|
|
else
|
2015-11-13 15:19:46 -05:00
|
|
|
add_error_if_count_exceeded(
|
|
|
|
post,
|
|
|
|
:no_mentions_allowed_newuser,
|
|
|
|
:too_many_mentions_newuser,
|
|
|
|
post.raw_mentions.size,
|
|
|
|
SiteSetting.newuser_max_mentions_per_post,
|
|
|
|
)
|
2013-06-09 12:48:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-07 12:08:59 -04:00
|
|
|
def max_embedded_media_validator(post)
|
2024-05-07 12:06:16 -04:00
|
|
|
return if post.acting_user.nil?
|
|
|
|
return if post.acting_user.staff?
|
2018-02-20 20:00:06 -05:00
|
|
|
|
2024-01-24 18:50:59 -05:00
|
|
|
if !post.acting_user.in_any_groups?(SiteSetting.embedded_media_post_allowed_groups_map)
|
2018-02-20 20:00:06 -05:00
|
|
|
add_error_if_count_exceeded(
|
|
|
|
post,
|
2024-01-24 18:50:59 -05:00
|
|
|
:no_embedded_media_allowed_group,
|
|
|
|
:no_embedded_media_allowed_group,
|
2020-08-07 12:08:59 -04:00
|
|
|
post.embedded_media_count,
|
2018-02-20 20:00:06 -05:00
|
|
|
0,
|
|
|
|
)
|
|
|
|
elsif post.acting_user.trust_level == TrustLevel[0]
|
|
|
|
add_error_if_count_exceeded(
|
|
|
|
post,
|
2020-08-07 12:08:59 -04:00
|
|
|
:no_embedded_media_allowed,
|
|
|
|
:too_many_embedded_media,
|
|
|
|
post.embedded_media_count,
|
|
|
|
SiteSetting.newuser_max_embedded_media,
|
2018-02-20 20:00:06 -05:00
|
|
|
)
|
|
|
|
end
|
2013-07-21 20:39:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def max_attachments_validator(post)
|
2016-04-18 16:08:42 -04:00
|
|
|
return if acting_user_is_trusted?(post) || private_message?(post)
|
2024-05-07 12:06:16 -04:00
|
|
|
|
2016-04-18 16:08:42 -04:00
|
|
|
add_error_if_count_exceeded(
|
|
|
|
post,
|
|
|
|
:no_attachments_allowed,
|
|
|
|
:too_many_attachments,
|
|
|
|
post.attachment_count,
|
|
|
|
SiteSetting.newuser_max_attachments,
|
|
|
|
)
|
2013-06-09 12:48:44 -04:00
|
|
|
end
|
|
|
|
|
2024-05-07 12:06:16 -04:00
|
|
|
def max_links_validator(post)
|
2018-06-13 14:57:32 -04:00
|
|
|
if (post.link_count == 0 && !post.has_oneboxes?) || private_message?(post)
|
|
|
|
return newuser_links_validator(post)
|
|
|
|
end
|
2018-02-06 18:07:24 -05:00
|
|
|
|
2018-06-13 14:57:32 -04:00
|
|
|
guardian = Guardian.new(post.acting_user)
|
|
|
|
if post.linked_hosts.keys.all? { |h| guardian.can_post_link?(host: h) }
|
2018-04-05 12:54:19 -04:00
|
|
|
return newuser_links_validator(post)
|
|
|
|
end
|
|
|
|
|
2018-02-06 18:07:24 -05:00
|
|
|
post.errors.add(:base, I18n.t(:links_require_trust))
|
|
|
|
end
|
|
|
|
|
2018-11-14 09:48:16 -05:00
|
|
|
def force_edit_last_validator(post)
|
2024-05-07 12:06:16 -04:00
|
|
|
return if post.id
|
|
|
|
return if private_message?(post)
|
|
|
|
return if post.acting_user&.staff?
|
|
|
|
return if SiteSetting.max_consecutive_replies == 0
|
2018-11-14 09:48:16 -05:00
|
|
|
|
|
|
|
topic = post.topic
|
2019-01-17 21:18:20 -05:00
|
|
|
return if topic&.ordered_posts&.first&.user == post.user
|
2018-11-14 09:48:16 -05:00
|
|
|
|
2022-05-10 15:18:17 -04:00
|
|
|
guardian = Guardian.new(post.acting_user)
|
|
|
|
return if guardian.is_category_group_moderator?(post.topic&.category)
|
|
|
|
|
2018-11-14 09:48:16 -05:00
|
|
|
last_posts_count =
|
|
|
|
DB.query_single(
|
|
|
|
<<~SQL,
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM (
|
|
|
|
SELECT user_id
|
|
|
|
FROM posts
|
|
|
|
WHERE deleted_at IS NULL
|
|
|
|
AND NOT hidden
|
|
|
|
AND topic_id = :topic_id
|
|
|
|
ORDER BY post_number DESC
|
|
|
|
LIMIT :max_replies
|
|
|
|
) 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,
|
2023-01-09 07:10:19 -05:00
|
|
|
).first
|
2018-11-14 09:48:16 -05:00
|
|
|
return if last_posts_count < SiteSetting.max_consecutive_replies
|
|
|
|
|
2019-01-17 21:18:20 -05:00
|
|
|
if guardian.can_edit?(topic.ordered_posts.last)
|
2018-11-14 09:48:16 -05:00
|
|
|
post.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t(:max_consecutive_replies, count: SiteSetting.max_consecutive_replies),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-09 12:48:44 -04:00
|
|
|
private
|
|
|
|
|
2024-05-07 12:06:16 -04:00
|
|
|
def raw_quality(post)
|
|
|
|
return if TextSentinel.body_sentinel(post.raw, private_message: private_message?(post)).valid?
|
|
|
|
post.errors.add(:raw, I18n.t(:is_invalid))
|
|
|
|
end
|
|
|
|
|
2018-02-06 18:07:24 -05:00
|
|
|
def acting_user_is_trusted?(post, level = 1)
|
2024-05-07 12:06:16 -04:00
|
|
|
post.acting_user&.has_trust_level?(TrustLevel[level])
|
2013-06-09 12:48:44 -04:00
|
|
|
end
|
|
|
|
|
2016-04-18 16:08:42 -04:00
|
|
|
def private_message?(post)
|
2024-05-07 12:06:16 -04:00
|
|
|
post.topic&.private_message? || options[:private_message]
|
|
|
|
end
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2016-04-18 16:08:42 -04:00
|
|
|
end
|
|
|
|
|
2015-11-13 15:19:46 -05:00
|
|
|
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))
|
|
|
|
else
|
|
|
|
post.errors.add(:base, I18n.t(limit_translation_key, count: max_count))
|
|
|
|
end
|
|
|
|
end
|
2013-06-09 12:48:44 -04:00
|
|
|
end
|
|
|
|
end
|