Merge branch 'master' of github.com:discourse/discourse

This commit is contained in:
Sam Saffron
2013-02-07 16:00:55 +11:00
15 changed files with 233 additions and 34 deletions

View File

@ -38,6 +38,7 @@ class Post < ActiveRecord::Base
validates_presence_of :raw, :user_id, :topic_id
validates :raw, length: {in: SiteSetting.min_post_length..SiteSetting.max_post_length}
validate :raw_quality
validate :max_mention_validator
validate :max_images_validator
validate :max_links_validator
@ -68,6 +69,18 @@ class Post < ActiveRecord::Base
self.raw.strip! if self.raw.present?
end
def raw_quality
sentinel = TextSentinel.new(self.raw, min_entropy: SiteSetting.body_min_entropy)
if sentinel.valid?
# It's possible the sentinel has cleaned up the title a bit
self.raw = sentinel.text
else
errors.add(:raw, I18n.t(:is_invalid)) unless sentinel.valid?
end
end
# Stop us from posting the same thing too quickly
def unique_post_validator
return if SiteSetting.unique_posts_mins == 0

View File

@ -126,6 +126,11 @@ class SiteSetting < ActiveRecord::Base
setting(:basic_requires_read_posts, 100)
setting(:basic_requires_time_spent_mins, 30)
# Entropy checks
setting(:title_min_entropy, 10)
setting(:body_min_entropy, 7)
setting(:max_word_length, 30)
def self.call_mothership?
self.enforce_global_nicknames? and self.discourse_org_access_key.present?

View File

@ -2,6 +2,7 @@ require_dependency 'slug'
require_dependency 'avatar_lookup'
require_dependency 'topic_view'
require_dependency 'rate_limiter'
require_dependency 'text_sentinel'
class Topic < ActiveRecord::Base
include RateLimiter::OnCreateRecord
@ -18,13 +19,14 @@ class Topic < ActiveRecord::Base
rate_limit :limit_topics_per_day
rate_limit :limit_private_messages_per_day
validate :title_quality
validates_presence_of :title
validates :title, length: {in: SiteSetting.min_topic_title_length..SiteSetting.max_topic_title_length}
serialize :meta_data, ActiveRecord::Coders::Hstore
validate :unique_title
validate :nuclear_option
belongs_to :category
has_many :posts
@ -113,18 +115,20 @@ class Topic < ActiveRecord::Base
errors.add(:title, I18n.t(:has_already_been_used)) if finder.exists?
end
# This is bad, but people are screwing us on try.discourse.org - soon we'll replace with
# a much more sane validation of odd characters to allow for other languages and such.
def nuclear_option
# Let presence validation catch it if it's blank
return if title.blank?
title.each_char do |c|
unless (20..126).include?(c.ord)
errors.add(:title, I18n.t(:invalid_characters))
return
end
def title_quality
# We don't care about quality on private messages
return if private_message?
sentinel = TextSentinel.new(title,
min_entropy: SiteSetting.title_min_entropy,
max_word_length: SiteSetting.max_word_length,
remove_interior_spaces: true)
if sentinel.valid?
# It's possible the sentinel has cleaned up the title a bit
self.title = sentinel.text
else
errors.add(:title, I18n.t(:is_invalid)) unless sentinel.valid?
end
end