2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-20 20:43:47 -05:00
|
|
|
# We use ActiveSupport mb_chars from here to properly support non ascii downcase
|
2016-07-12 10:08:55 -05:00
|
|
|
# TODO remove when ruby 2.4 lands
|
|
|
|
require "active_support/core_ext/string/multibyte"
|
|
|
|
|
2013-02-06 19:09:31 -06:00
|
|
|
#
|
2013-04-10 04:00:50 -05:00
|
|
|
# Given a string, tell us whether or not is acceptable.
|
2013-02-06 19:09:31 -06:00
|
|
|
#
|
|
|
|
class TextSentinel
|
|
|
|
attr_accessor :text
|
|
|
|
|
2013-08-30 10:27:24 -05:00
|
|
|
ENTROPY_SCALE ||= 0.7
|
2013-08-28 10:04:28 -05:00
|
|
|
|
2013-04-10 04:00:50 -05:00
|
|
|
def initialize(text, opts = nil)
|
|
|
|
@opts = opts || {}
|
2013-05-22 23:52:12 -05:00
|
|
|
@text = text.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
|
2013-04-10 04:00:50 -05:00
|
|
|
end
|
|
|
|
|
2013-06-13 03:18:17 -05:00
|
|
|
def self.body_sentinel(text, opts = {})
|
|
|
|
entropy = SiteSetting.body_min_entropy
|
|
|
|
if opts[:private_message]
|
2018-01-30 23:56:00 -06:00
|
|
|
scale_entropy =
|
|
|
|
SiteSetting.min_personal_message_post_length.to_f / SiteSetting.min_post_length.to_f
|
2013-06-13 03:18:17 -05:00
|
|
|
entropy = (entropy * scale_entropy).to_i
|
2018-01-30 23:56:00 -06:00
|
|
|
entropy =
|
|
|
|
(SiteSetting.min_personal_message_post_length.to_f * ENTROPY_SCALE).to_i if entropy >
|
|
|
|
SiteSetting.min_personal_message_post_length
|
2013-08-28 10:04:28 -05:00
|
|
|
else
|
|
|
|
entropy = (SiteSetting.min_post_length.to_f * ENTROPY_SCALE).to_i if entropy >
|
|
|
|
SiteSetting.min_post_length
|
2013-06-13 03:18:17 -05:00
|
|
|
end
|
|
|
|
TextSentinel.new(text, min_entropy: entropy)
|
2013-02-06 19:09:31 -06:00
|
|
|
end
|
|
|
|
|
2013-02-08 15:55:40 -06:00
|
|
|
def self.title_sentinel(text)
|
2013-08-28 10:04:28 -05:00
|
|
|
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
|
2015-04-02 00:46:53 -05:00
|
|
|
TextSentinel.new(text, min_entropy: entropy, max_word_length: SiteSetting.title_max_word_length)
|
2013-02-08 15:55:40 -06:00
|
|
|
end
|
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
# Entropy is a number of how many unique characters the string needs.
|
2013-06-07 13:47:07 -05:00
|
|
|
# Non-ASCII characters are weighted heavier since they contain more "information"
|
2013-02-06 19:09:31 -06:00
|
|
|
def entropy
|
2013-06-07 13:47:07 -05:00
|
|
|
chars = @text.to_s.strip.split("")
|
|
|
|
@entropy ||= chars.pack("M*" * chars.size).gsub("\n", "").split("=").uniq.size
|
2013-02-06 19:09:31 -06:00
|
|
|
end
|
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
def valid?
|
2013-05-23 12:48:37 -05:00
|
|
|
@text.present? && seems_meaningful? && seems_pronounceable? && seems_unpretentious? &&
|
2013-06-18 00:49:10 -05:00
|
|
|
seems_quiet?
|
2013-05-23 12:48:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def seems_meaningful?
|
2013-05-22 23:52:12 -05:00
|
|
|
# Minimum entropy if entropy check required
|
2013-05-23 12:48:37 -05:00
|
|
|
@opts[:min_entropy].blank? || (entropy >= @opts[:min_entropy])
|
|
|
|
end
|
2013-02-06 19:09:31 -06:00
|
|
|
|
2013-05-23 12:48:37 -05:00
|
|
|
def seems_pronounceable?
|
2013-05-22 23:52:12 -05:00
|
|
|
# At least some non-symbol characters
|
|
|
|
# (We don't have a comprehensive list of symbols, but this will eliminate some noise)
|
2013-05-23 12:48:37 -05:00
|
|
|
@text.gsub(symbols_regex, "").size > 0
|
|
|
|
end
|
2013-02-06 19:09:31 -06:00
|
|
|
|
2013-05-23 12:48:37 -05:00
|
|
|
def seems_unpretentious?
|
2019-04-05 08:07:49 -05:00
|
|
|
return true if skipped_locale.include?(SiteSetting.default_locale)
|
2013-05-22 23:52:12 -05:00
|
|
|
# Don't allow super long words if there is a word length maximum
|
2021-05-03 02:21:35 -05:00
|
|
|
|
|
|
|
@opts[:max_word_length].blank? ||
|
|
|
|
(@text.split(%r{\s|/|-|\.|:}).map(&:size).max || 0) <= @opts[:max_word_length]
|
2013-05-23 12:48:37 -05:00
|
|
|
end
|
2013-02-06 19:09:31 -06:00
|
|
|
|
2013-05-23 12:48:37 -05:00
|
|
|
def seems_quiet?
|
2019-04-05 08:07:49 -05:00
|
|
|
return true if skipped_locale.include?(SiteSetting.default_locale)
|
2016-07-12 10:08:55 -05:00
|
|
|
# We don't allow all upper case content
|
|
|
|
SiteSetting.allow_uppercase_posts || @text == @text.mb_chars.downcase.to_s ||
|
|
|
|
@text != @text.mb_chars.upcase.to_s
|
2013-02-06 19:09:31 -06:00
|
|
|
end
|
|
|
|
|
2020-11-11 07:11:36 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def symbols_regex
|
|
|
|
/[\ -\/\[-\`\:-\@\{-\~]/m
|
|
|
|
end
|
|
|
|
|
|
|
|
def skipped_locale
|
|
|
|
%w[zh_CN zh_TW ko ja].freeze
|
|
|
|
end
|
2013-02-06 19:09:31 -06:00
|
|
|
end
|