2013-06-13 16:18:17 +08:00
|
|
|
require_dependency 'validators/stripped_length_validator'
|
2015-12-01 17:40:23 +08:00
|
|
|
|
2013-06-13 16:18:17 +08:00
|
|
|
module Validators; end
|
2015-12-01 17:40:23 +08:00
|
|
|
|
2013-06-13 16:18:17 +08:00
|
|
|
class Validators::PostValidator < ActiveModel::Validator
|
2015-03-27 04:57:50 +08:00
|
|
|
|
2013-06-10 00:48:44 +08:00
|
|
|
def validate(record)
|
2013-06-13 16:18:17 +08:00
|
|
|
presence(record)
|
2015-12-01 17:40:23 +08:00
|
|
|
|
|
|
|
return if record.acting_user.try(:staged?)
|
2016-04-19 04:08:42 +08:00
|
|
|
return if record.acting_user.try(:admin?) && Discourse.static_doc_topic_ids.include?(record.topic_id)
|
2015-12-01 17:40:23 +08:00
|
|
|
|
2016-12-05 20:31:43 +08:00
|
|
|
post_body_validator(record)
|
2015-12-01 17:40:23 +08:00
|
|
|
max_posts_validator(record)
|
|
|
|
max_mention_validator(record)
|
|
|
|
max_images_validator(record)
|
|
|
|
max_attachments_validator(record)
|
|
|
|
max_links_validator(record)
|
|
|
|
unique_post_validator(record)
|
2013-06-10 00:48:44 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 16:18:17 +08:00
|
|
|
def presence(post)
|
2015-03-27 04:57:50 +08:00
|
|
|
unless options[:skip_topic]
|
|
|
|
post.errors.add(:topic_id, :blank, options) if post.topic_id.blank?
|
2013-06-13 16:18:17 +08:00
|
|
|
end
|
2015-03-27 04:57:50 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
if post.new_record? && post.user_id.nil?
|
2013-09-04 05:19:29 +08:00
|
|
|
post.errors.add(:user_id, :blank, options)
|
|
|
|
end
|
2013-06-13 16:18:17 +08:00
|
|
|
end
|
|
|
|
|
2016-12-05 20:31:43 +08:00
|
|
|
def post_body_validator(post)
|
2017-04-27 11:53:53 +08:00
|
|
|
return if options[:skip_post_body] || post.topic&.pm_with_non_human_user?
|
2016-12-05 20:31:43 +08:00
|
|
|
stripped_length(post)
|
|
|
|
raw_quality(post)
|
2017-07-28 00:26:55 +08:00
|
|
|
watched_words(post)
|
2016-12-05 20:31:43 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 16:18:17 +08:00
|
|
|
def stripped_length(post)
|
2016-04-19 04:08:42 +08:00
|
|
|
range = if private_message?(post)
|
2015-03-19 22:17:55 +08:00
|
|
|
# private message
|
|
|
|
SiteSetting.private_message_post_length
|
2015-12-01 02:08:35 +08:00
|
|
|
elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0)
|
2015-03-19 22:17:55 +08:00
|
|
|
# creating/editing first post
|
2016-12-10 04:46:26 +08:00
|
|
|
post.topic&.featured_link&.present? ? (0..SiteSetting.max_post_length) : SiteSetting.first_post_length
|
2015-03-19 22:17:55 +08:00
|
|
|
else
|
|
|
|
# regular post
|
|
|
|
SiteSetting.post_length
|
|
|
|
end
|
|
|
|
|
2013-06-13 16:18:17 +08:00
|
|
|
Validators::StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
|
|
|
end
|
|
|
|
|
2013-06-10 00:48:44 +08:00
|
|
|
def raw_quality(post)
|
2016-04-19 04:08:42 +08:00
|
|
|
sentinel = TextSentinel.body_sentinel(post.raw, private_message: private_message?(post))
|
2013-06-10 00:48:44 +08:00
|
|
|
post.errors.add(:raw, I18n.t(:is_invalid)) unless sentinel.valid?
|
|
|
|
end
|
|
|
|
|
2017-07-28 00:26:55 +08:00
|
|
|
def watched_words(post)
|
|
|
|
if !post.acting_user&.staff? && !post.acting_user&.staged && WordWatcher.new(post.raw).should_block?
|
|
|
|
post.errors[:base] << I18n.t('contains_blocked_words')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-10 00:48:44 +08:00
|
|
|
# Ensure maximum amount of mentions in a post
|
|
|
|
def max_mention_validator(post)
|
2016-02-01 23:37:49 +08:00
|
|
|
return if post.acting_user.try(:staff?)
|
|
|
|
|
2016-04-19 04:08:42 +08:00
|
|
|
if acting_user_is_trusted?(post) || private_message?(post)
|
2015-11-14 04:19:46 +08:00
|
|
|
add_error_if_count_exceeded(post, :no_mentions_allowed, :too_many_mentions, post.raw_mentions.size, SiteSetting.max_mentions_per_post)
|
2013-06-10 00:48:44 +08:00
|
|
|
else
|
2015-11-14 04:19:46 +08: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-10 00:48:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-20 02:45:55 +08:00
|
|
|
def max_posts_validator(post)
|
2013-12-21 00:29:44 +08:00
|
|
|
if post.new_record? && post.acting_user.present? && post.acting_user.posted_too_much_in_topic?(post.topic_id)
|
2014-01-07 00:10:18 +08:00
|
|
|
post.errors.add(:base, I18n.t(:too_many_replies, count: SiteSetting.newuser_max_replies_per_topic))
|
2013-12-20 02:45:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-10 00:48:44 +08:00
|
|
|
# Ensure new users can not put too many images in a post
|
|
|
|
def max_images_validator(post)
|
2016-04-19 04:08:42 +08:00
|
|
|
return if acting_user_is_trusted?(post) || private_message?(post)
|
|
|
|
add_error_if_count_exceeded(post, :no_images_allowed, :too_many_images, post.image_count, SiteSetting.newuser_max_images)
|
2013-07-22 08:39:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure new users can not put too many attachments in a post
|
|
|
|
def max_attachments_validator(post)
|
2016-04-19 04:08:42 +08: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-10 00:48:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure new users can not put too many links in a post
|
|
|
|
def max_links_validator(post)
|
2016-04-19 04:08:42 +08: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-10 00:48:44 +08: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 23:50:05 +08:00
|
|
|
return if post.skip_unique_check
|
2016-09-17 01:12:05 +08:00
|
|
|
return if post.acting_user.try(:staff?)
|
2013-06-10 00:48:44 +08:00
|
|
|
|
|
|
|
# If the post is empty, default to the validates_presence_of
|
|
|
|
return if post.raw.blank?
|
|
|
|
|
2013-09-10 04:17:31 +08:00
|
|
|
if post.matches_recent_post?
|
2013-06-10 00:48:44 +08:00
|
|
|
post.errors.add(:raw, I18n.t(:just_posted_that))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def acting_user_is_trusted?(post)
|
2015-12-01 17:40:23 +08:00
|
|
|
post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[1])
|
2013-06-10 00:48:44 +08:00
|
|
|
end
|
|
|
|
|
2016-04-19 04:08:42 +08:00
|
|
|
def private_message?(post)
|
|
|
|
post.topic.try(:private_message?)
|
|
|
|
end
|
|
|
|
|
2015-11-14 04:19:46 +08: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-10 00:48:44 +08:00
|
|
|
end
|
|
|
|
end
|