2013-06-13 04:18:17 -04:00
|
|
|
require_dependency 'validators/stripped_length_validator'
|
|
|
|
module Validators; end
|
|
|
|
class Validators::PostValidator < ActiveModel::Validator
|
2013-06-09 12:48:44 -04:00
|
|
|
def validate(record)
|
2013-06-13 04:18:17 -04:00
|
|
|
presence(record)
|
|
|
|
stripped_length(record)
|
2013-06-09 12:48:44 -04:00
|
|
|
raw_quality(record)
|
|
|
|
max_mention_validator(record)
|
|
|
|
max_images_validator(record)
|
2013-07-21 20:39:17 -04:00
|
|
|
max_attachments_validator(record)
|
2013-06-09 12:48:44 -04:00
|
|
|
max_links_validator(record)
|
|
|
|
unique_post_validator(record)
|
|
|
|
end
|
|
|
|
|
2013-06-13 04:18:17 -04:00
|
|
|
def presence(post)
|
2013-09-03 17:19:29 -04:00
|
|
|
[:raw,:topic_id].each do |attr_name|
|
2013-06-13 04:18:17 -04:00
|
|
|
post.errors.add(attr_name, :blank, options) if post.send(attr_name).blank?
|
|
|
|
end
|
2013-09-03 17:19:29 -04:00
|
|
|
if post.new_record? and post.user_id.nil?
|
|
|
|
post.errors.add(:user_id, :blank, options)
|
|
|
|
end
|
2013-06-13 04:18:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stripped_length(post)
|
|
|
|
range = post.topic.try(:private_message?) ? SiteSetting.private_message_post_length : SiteSetting.post_length
|
|
|
|
Validators::StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
|
|
|
end
|
|
|
|
|
2013-06-09 12:48:44 -04:00
|
|
|
def raw_quality(post)
|
2013-06-13 04:18:17 -04:00
|
|
|
sentinel = TextSentinel.body_sentinel(post.raw, private_message: post.topic.try(:private_message?))
|
2013-06-09 12:48:44 -04: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
|
|
|
|
|
|
|
|
# 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 20:39:17 -04: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 12:48:44 -04: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 11:50:05 -04:00
|
|
|
return if post.skip_unique_check
|
2013-06-09 12:48:44 -04:00
|
|
|
return if post.acting_user.admin? || post.acting_user.moderator?
|
|
|
|
|
|
|
|
# If the post is empty, default to the validates_presence_of
|
|
|
|
return if post.raw.blank?
|
|
|
|
|
2013-09-09 16:17:31 -04:00
|
|
|
if post.matches_recent_post?
|
2013-06-09 12:48:44 -04:00
|
|
|
post.errors.add(:raw, I18n.t(:just_posted_that))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def acting_user_is_trusted?(post)
|
|
|
|
post.acting_user.present? && post.acting_user.has_trust_level?(:basic)
|
|
|
|
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
|