diff --git a/lib/text_sentinel.rb b/lib/text_sentinel.rb index 5d7d8ac7913..b16e761329d 100644 --- a/lib/text_sentinel.rb +++ b/lib/text_sentinel.rb @@ -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. diff --git a/spec/components/text_sentinel_spec.rb b/spec/components/text_sentinel_spec.rb index 15275e25a59..8c3ca4bc45a 100644 --- a/spec/components/text_sentinel_spec.rb +++ b/spec/components/text_sentinel_spec.rb @@ -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