2013-06-13 03:18:17 -05:00
|
|
|
require_dependency 'validators/stripped_length_validator'
|
|
|
|
module Validators; end
|
|
|
|
class Validators::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)
|
2014-08-01 13:53:35 -05:00
|
|
|
unless Discourse.static_doc_topic_ids.include?(record.topic_id) && record.acting_user.try(:admin?)
|
2014-07-28 15:40:06 -05:00
|
|
|
stripped_length(record)
|
|
|
|
raw_quality(record)
|
|
|
|
max_posts_validator(record)
|
|
|
|
max_mention_validator(record)
|
|
|
|
max_images_validator(record)
|
|
|
|
max_attachments_validator(record)
|
|
|
|
max_links_validator(record)
|
|
|
|
unique_post_validator(record)
|
|
|
|
end
|
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
|
|
|
|
|
|
|
post.errors.add(:raw, :blank, options) if post.raw.blank?
|
|
|
|
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
|
|
|
|
2013-09-03 16:19:29 -05:00
|
|
|
if post.new_record? and post.user_id.nil?
|
|
|
|
post.errors.add(:user_id, :blank, options)
|
|
|
|
end
|
2013-06-13 03:18:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def stripped_length(post)
|
2015-03-19 09:17:55 -05:00
|
|
|
range = if post.topic.try(:private_message?)
|
|
|
|
# private message
|
|
|
|
SiteSetting.private_message_post_length
|
|
|
|
elsif ( post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0) )
|
|
|
|
# creating/editing first post
|
|
|
|
SiteSetting.first_post_length
|
|
|
|
else
|
|
|
|
# regular post
|
|
|
|
SiteSetting.post_length
|
|
|
|
end
|
|
|
|
|
2013-06-13 03:18:17 -05:00
|
|
|
Validators::StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
|
|
|
end
|
|
|
|
|
2013-06-09 11:48:44 -05:00
|
|
|
def raw_quality(post)
|
2013-06-13 03:18:17 -05:00
|
|
|
sentinel = TextSentinel.body_sentinel(post.raw, private_message: post.topic.try(:private_message?))
|
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)
|
|
|
|
if acting_user_is_trusted?(post)
|
|
|
|
add_error_if_count_exceeded(post, :too_many_mentions, post.raw_mentions.size, SiteSetting.max_mentions_per_post)
|
|
|
|
else
|
|
|
|
add_error_if_count_exceeded(post, :too_many_mentions_newuser, post.raw_mentions.size, SiteSetting.newuser_max_mentions_per_post)
|
|
|
|
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)
|
|
|
|
add_error_if_count_exceeded(post, :too_many_images, post.image_count, SiteSetting.newuser_max_images) unless acting_user_is_trusted?(post)
|
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)
|
|
|
|
add_error_if_count_exceeded(post, :too_many_attachments, post.attachment_count, SiteSetting.newuser_max_attachments) unless acting_user_is_trusted?(post)
|
2013-06-09 11:48:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure new users can not put too many links in a post
|
|
|
|
def max_links_validator(post)
|
|
|
|
add_error_if_count_exceeded(post, :too_many_links, post.link_count, SiteSetting.newuser_max_links) unless acting_user_is_trusted?(post)
|
|
|
|
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
|
2014-08-11 03:48:00 -05:00
|
|
|
return if post.acting_user.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
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def acting_user_is_trusted?(post)
|
2014-09-05 00:20:39 -05:00
|
|
|
post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[1])
|
2013-06-09 11:48:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_error_if_count_exceeded(post, key_for_translation, current_count, max_count)
|
|
|
|
post.errors.add(:base, I18n.t(key_for_translation, count: max_count)) if current_count > max_count
|
|
|
|
end
|
|
|
|
end
|