2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-01 23:01:53 -05:00
|
|
|
class PostValidator < ActiveModel::Validator
|
2015-03-26 15:57:50 -05:00
|
|
|
|
2013-06-09 11:48:44 -05:00
|
|
|
def validate(record)
|
2013-06-13 03:18:17 -05:00
|
|
|
presence(record)
|
2015-12-01 03:40:23 -06:00
|
|
|
|
|
|
|
return if record.acting_user.try(:staged?)
|
2016-04-18 15:08:42 -05:00
|
|
|
return if record.acting_user.try(:admin?) && Discourse.static_doc_topic_ids.include?(record.topic_id)
|
2015-12-01 03:40:23 -06:00
|
|
|
|
2016-12-05 06:31:43 -06:00
|
|
|
post_body_validator(record)
|
2015-12-01 03:40:23 -06:00
|
|
|
max_posts_validator(record)
|
|
|
|
max_mention_validator(record)
|
|
|
|
max_images_validator(record)
|
|
|
|
max_attachments_validator(record)
|
2018-02-06 17:07:24 -06:00
|
|
|
can_post_links_validator(record)
|
2015-12-01 03:40:23 -06:00
|
|
|
unique_post_validator(record)
|
2018-11-14 08:48:16 -06:00
|
|
|
force_edit_last_validator(record)
|
2013-06-09 11:48:44 -05:00
|
|
|
end
|
|
|
|
|
2013-06-13 03:18:17 -05:00
|
|
|
def presence(post)
|
2015-03-26 15:57:50 -05:00
|
|
|
unless options[:skip_topic]
|
|
|
|
post.errors.add(:topic_id, :blank, options) if post.topic_id.blank?
|
2013-06-13 03:18:17 -05:00
|
|
|
end
|
2015-03-26 15:57:50 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
if post.new_record? && post.user_id.nil?
|
2013-09-03 16:19:29 -05:00
|
|
|
post.errors.add(:user_id, :blank, options)
|
|
|
|
end
|
2013-06-13 03:18:17 -05:00
|
|
|
end
|
|
|
|
|
2016-12-05 06:31:43 -06:00
|
|
|
def post_body_validator(post)
|
2017-04-26 22:53:53 -05:00
|
|
|
return if options[:skip_post_body] || post.topic&.pm_with_non_human_user?
|
2016-12-05 06:31:43 -06:00
|
|
|
stripped_length(post)
|
|
|
|
raw_quality(post)
|
2019-10-01 19:38:34 -05:00
|
|
|
WatchedWordsValidator.new(attributes: [:raw]).validate(post) if !post.acting_user&.staged
|
2016-12-05 06:31:43 -06:00
|
|
|
end
|
|
|
|
|
2013-06-13 03:18:17 -05:00
|
|
|
def stripped_length(post)
|
2016-04-18 15:08:42 -05:00
|
|
|
range = if private_message?(post)
|
2015-03-19 09:17:55 -05:00
|
|
|
# private message
|
|
|
|
SiteSetting.private_message_post_length
|
2015-11-30 12:08:35 -06:00
|
|
|
elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0)
|
2015-03-19 09:17:55 -05:00
|
|
|
# creating/editing first post
|
2016-12-09 14:46:26 -06:00
|
|
|
post.topic&.featured_link&.present? ? (0..SiteSetting.max_post_length) : SiteSetting.first_post_length
|
2015-03-19 09:17:55 -05:00
|
|
|
else
|
|
|
|
# regular post
|
|
|
|
SiteSetting.post_length
|
|
|
|
end
|
|
|
|
|
2019-10-01 23:01:53 -05:00
|
|
|
StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
2013-06-13 03:18:17 -05:00
|
|
|
end
|
|
|
|
|
2013-06-09 11:48:44 -05:00
|
|
|
def raw_quality(post)
|
2016-04-18 15:08:42 -05:00
|
|
|
sentinel = TextSentinel.body_sentinel(post.raw, private_message: private_message?(post))
|
2013-06-09 11:48:44 -05:00
|
|
|
post.errors.add(:raw, I18n.t(:is_invalid)) unless sentinel.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure maximum amount of mentions in a post
|
|
|
|
def max_mention_validator(post)
|
2016-02-01 09:37:49 -06:00
|
|
|
return if post.acting_user.try(:staff?)
|
|
|
|
|
2016-04-18 15:08:42 -05:00
|
|
|
if acting_user_is_trusted?(post) || private_message?(post)
|
2015-11-13 14:19:46 -06: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 11:48:44 -05:00
|
|
|
else
|
2015-11-13 14:19:46 -06: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 11:48:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 12:45:55 -06:00
|
|
|
def max_posts_validator(post)
|
2013-12-20 10:29:44 -06:00
|
|
|
if post.new_record? && post.acting_user.present? && post.acting_user.posted_too_much_in_topic?(post.topic_id)
|
2014-01-06 10:10:18 -06:00
|
|
|
post.errors.add(:base, I18n.t(:too_many_replies, count: SiteSetting.newuser_max_replies_per_topic))
|
2013-12-19 12:45:55 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-09 11:48:44 -05:00
|
|
|
# Ensure new users can not put too many images in a post
|
|
|
|
def max_images_validator(post)
|
2018-04-04 23:21:03 -05:00
|
|
|
return if post.acting_user.blank? || post.acting_user&.staff?
|
2018-02-20 19:00:06 -06:00
|
|
|
|
|
|
|
if post.acting_user.trust_level < TrustLevel[SiteSetting.min_trust_to_post_images]
|
|
|
|
add_error_if_count_exceeded(
|
|
|
|
post,
|
|
|
|
:no_images_allowed_trust,
|
|
|
|
:no_images_allowed_trust,
|
|
|
|
post.image_count,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
elsif post.acting_user.trust_level == TrustLevel[0]
|
|
|
|
add_error_if_count_exceeded(
|
|
|
|
post,
|
|
|
|
:no_images_allowed,
|
|
|
|
:too_many_images,
|
|
|
|
post.image_count,
|
|
|
|
SiteSetting.newuser_max_images
|
|
|
|
)
|
|
|
|
end
|
2013-07-21 19:39:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure new users can not put too many attachments in a post
|
|
|
|
def max_attachments_validator(post)
|
2016-04-18 15:08:42 -05:00
|
|
|
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)
|
2013-06-09 11:48:44 -05:00
|
|
|
end
|
|
|
|
|
2018-02-06 17:07:24 -06:00
|
|
|
def can_post_links_validator(post)
|
2018-06-13 13:57:32 -05:00
|
|
|
if (post.link_count == 0 && !post.has_oneboxes?) || private_message?(post)
|
|
|
|
return newuser_links_validator(post)
|
|
|
|
end
|
2018-02-06 17:07:24 -06:00
|
|
|
|
2018-06-13 13:57:32 -05:00
|
|
|
guardian = Guardian.new(post.acting_user)
|
|
|
|
if post.linked_hosts.keys.all? { |h| guardian.can_post_link?(host: h) }
|
2018-04-05 11:54:19 -05:00
|
|
|
return newuser_links_validator(post)
|
|
|
|
end
|
|
|
|
|
2018-02-06 17:07:24 -06:00
|
|
|
post.errors.add(:base, I18n.t(:links_require_trust))
|
|
|
|
end
|
|
|
|
|
2013-06-09 11:48:44 -05:00
|
|
|
# Ensure new users can not put too many links in a post
|
2018-02-06 17:07:24 -06:00
|
|
|
def newuser_links_validator(post)
|
2016-04-18 15:08:42 -05:00
|
|
|
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)
|
2013-06-09 11:48:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Stop us from posting the same thing too quickly
|
|
|
|
def unique_post_validator(post)
|
|
|
|
return if SiteSetting.unique_posts_mins == 0
|
2013-09-06 10:50:05 -05:00
|
|
|
return if post.skip_unique_check
|
2016-09-16 12:12:05 -05:00
|
|
|
return if post.acting_user.try(:staff?)
|
2013-06-09 11:48:44 -05:00
|
|
|
|
|
|
|
# If the post is empty, default to the validates_presence_of
|
|
|
|
return if post.raw.blank?
|
|
|
|
|
2013-09-09 15:17:31 -05:00
|
|
|
if post.matches_recent_post?
|
2013-06-09 11:48:44 -05:00
|
|
|
post.errors.add(:raw, I18n.t(:just_posted_that))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-14 08:48:16 -06:00
|
|
|
def force_edit_last_validator(post)
|
|
|
|
return if SiteSetting.max_consecutive_replies == 0 || post.id || post.acting_user&.staff? || private_message?(post)
|
|
|
|
|
|
|
|
topic = post.topic
|
2019-01-17 20:18:20 -06:00
|
|
|
return if topic&.ordered_posts&.first&.user == post.user
|
2018-11-14 08:48:16 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
return if last_posts_count < SiteSetting.max_consecutive_replies
|
|
|
|
|
|
|
|
guardian = Guardian.new(post.acting_user)
|
2019-01-17 20:18:20 -06:00
|
|
|
if guardian.can_edit?(topic.ordered_posts.last)
|
2018-11-14 08:48:16 -06:00
|
|
|
post.errors.add(:base, I18n.t(:max_consecutive_replies, count: SiteSetting.max_consecutive_replies))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-09 11:48:44 -05:00
|
|
|
private
|
|
|
|
|
2018-02-06 17:07:24 -06:00
|
|
|
def acting_user_is_trusted?(post, level = 1)
|
2018-06-18 19:05:04 -05:00
|
|
|
post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[level])
|
2013-06-09 11:48:44 -05:00
|
|
|
end
|
|
|
|
|
2016-04-18 15:08:42 -05:00
|
|
|
def private_message?(post)
|
|
|
|
post.topic.try(:private_message?)
|
|
|
|
end
|
|
|
|
|
2015-11-13 14:19:46 -06: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 11:48:44 -05:00
|
|
|
end
|
|
|
|
end
|