2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
class WordWatcher
|
|
|
|
|
|
|
|
def initialize(raw)
|
|
|
|
@raw = raw
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.words_for_action(action)
|
|
|
|
WatchedWord.where(action: WatchedWord.actions[action.to_sym]).limit(1000).pluck(:word)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.words_for_action_exists?(action)
|
|
|
|
WatchedWord.where(action: WatchedWord.actions[action.to_sym]).exists?
|
|
|
|
end
|
|
|
|
|
2019-07-22 19:59:56 +08:00
|
|
|
def self.get_cached_words(action)
|
|
|
|
Discourse.cache.fetch(word_matcher_regexp_key(action), expires_in: 1.day) do
|
|
|
|
words_for_action(action).presence
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
def self.word_matcher_regexp(action)
|
2019-07-22 19:59:56 +08:00
|
|
|
words = get_cached_words(action)
|
|
|
|
if words
|
|
|
|
words = words.map do |w|
|
|
|
|
word = word_to_regexp(w)
|
|
|
|
word = "(#{word})" if SiteSetting.watched_words_regular_expressions?
|
|
|
|
word
|
|
|
|
end
|
|
|
|
regexp = words.join('|')
|
|
|
|
if !SiteSetting.watched_words_regular_expressions?
|
|
|
|
regexp = "(#{regexp})"
|
|
|
|
regexp = "(?<!\\w)(#{regexp})(?!\\w)"
|
2017-11-18 03:10:38 +08:00
|
|
|
end
|
2019-07-22 19:59:56 +08:00
|
|
|
Regexp.new(regexp, Regexp::IGNORECASE)
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-28 03:48:57 +08:00
|
|
|
def self.word_to_regexp(word)
|
2017-11-18 03:10:38 +08:00
|
|
|
if SiteSetting.watched_words_regular_expressions?
|
|
|
|
# Strip ruby regexp format if present, we're going to make the whole thing
|
|
|
|
# case insensitive anyway
|
|
|
|
return word.start_with?("(?-mix:") ? word[7..-2] : word
|
|
|
|
end
|
2017-09-28 03:48:57 +08:00
|
|
|
Regexp.escape(word).gsub("\\*", '\S*')
|
|
|
|
end
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
def self.word_matcher_regexp_key(action)
|
2019-07-22 19:59:56 +08:00
|
|
|
"watched-words-list:#{action}"
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache!
|
2019-06-25 07:06:32 +08:00
|
|
|
WatchedWord.actions.each do |a, i|
|
2017-06-29 04:56:44 +08:00
|
|
|
Discourse.cache.delete word_matcher_regexp_key(a)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def requires_approval?
|
|
|
|
word_matches_for_action?(:require_approval)
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_flag?
|
|
|
|
word_matches_for_action?(:flag)
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_block?
|
2019-07-22 19:59:56 +08:00
|
|
|
word_matches_for_action?(:block, all_matches: true)
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
2019-07-22 19:59:56 +08:00
|
|
|
def word_matches_for_action?(action, all_matches: false)
|
|
|
|
regexp = self.class.word_matcher_regexp(action)
|
|
|
|
if regexp
|
|
|
|
match = regexp.match(@raw)
|
|
|
|
return match if !all_matches || !match
|
2017-06-29 04:56:44 +08:00
|
|
|
|
2019-07-22 19:59:56 +08:00
|
|
|
if SiteSetting.watched_words_regular_expressions?
|
|
|
|
set = Set.new
|
|
|
|
@raw.scan(regexp).each do |m|
|
|
|
|
if Array === m
|
|
|
|
set.add(m.find(&:present?))
|
|
|
|
elsif String === m
|
|
|
|
set.add(m)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
matches = set.to_a
|
|
|
|
else
|
|
|
|
matches = @raw.scan(regexp)
|
|
|
|
matches.flatten!
|
|
|
|
matches.uniq!
|
|
|
|
end
|
|
|
|
matches.compact!
|
|
|
|
matches.sort!
|
|
|
|
matches
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|