If min entropy setting is greater than min post/body length setting, then use a sensible min entropy value instead

This commit is contained in:
Neil Lalonde 2013-08-28 11:04:28 -04:00
parent 61281a3c81
commit f611a5d898
2 changed files with 38 additions and 4 deletions

View File

@ -5,6 +5,8 @@ class TextSentinel
attr_accessor :text
ENTROPY_SCALE = 0.7
def initialize(text, opts=nil)
@opts = opts || {}
@text = text.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '')
@ -15,14 +17,20 @@ class TextSentinel
if opts[:private_message]
scale_entropy = SiteSetting.min_private_message_post_length.to_f / SiteSetting.min_post_length.to_f
entropy = (entropy * scale_entropy).to_i
entropy = (SiteSetting.min_private_message_post_length.to_f * ENTROPY_SCALE).to_i if entropy > SiteSetting.min_private_message_post_length
else
entropy = (SiteSetting.min_post_length.to_f * ENTROPY_SCALE).to_i if entropy > SiteSetting.min_post_length
end
TextSentinel.new(text, min_entropy: entropy)
end
def self.title_sentinel(text)
TextSentinel.new(text,
min_entropy: SiteSetting.title_min_entropy,
max_word_length: SiteSetting.max_word_length)
entropy = if SiteSetting.min_topic_title_length > SiteSetting.title_min_entropy
SiteSetting.title_min_entropy
else
(SiteSetting.min_topic_title_length.to_f * ENTROPY_SCALE).to_i
end
TextSentinel.new(text, min_entropy: entropy, max_word_length: SiteSetting.max_word_length)
end
# Entropy is a number of how many unique characters the string needs.

View File

@ -90,7 +90,7 @@ describe TextSentinel do
TextSentinel.new("jfewjfoejwfojeojfoejofjeo3" * 5, max_word_length: 30).should_not be_valid
end
it "doesn't except junk symbols as a string" do
it "doesn't accept junk symbols as a string" do
TextSentinel.new("[[[").should_not be_valid
TextSentinel.new("<<<").should_not be_valid
TextSentinel.new("{{$!").should_not be_valid
@ -98,4 +98,30 @@ describe TextSentinel do
end
context 'title_sentinel' do
it "uses a sensible min entropy value when min title length is less than title_min_entropy" do
SiteSetting.stubs(:min_topic_title_length).returns(3)
SiteSetting.stubs(:title_min_entropy).returns(10)
TextSentinel.title_sentinel('Hey').should be_valid
end
end
context 'body_sentinel' do
it "uses a sensible min entropy value when min body length is less than min entropy" do
SiteSetting.stubs(:min_post_length).returns(3)
SiteSetting.stubs(:body_min_entropy).returns(7)
TextSentinel.body_sentinel('Yup').should be_valid
end
it "uses a sensible min entropy value when min pm body length is less than min entropy" do
SiteSetting.stubs(:min_post_length).returns(5)
SiteSetting.stubs(:min_private_message_post_length).returns(3)
SiteSetting.stubs(:body_min_entropy).returns(7)
TextSentinel.body_sentinel('Lol', private_message: true).should be_valid
end
end
end