FIX: Make autotag watched words case insensitive (#13043)

* FIX: Hide tag watched words if tagging is disabled

These 'autotag' words were shown even if tagging was disabled.

* FIX: Make autotag watched words case insensitive

This commit also fixes the bug when no tag was applied if no other tag
was already present.
This commit is contained in:
Bianca Nenciu 2021-05-14 16:52:10 +03:00 committed by GitHub
parent 0e6a8757fe
commit 3a1b05f219
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 27 deletions

View File

@ -6,7 +6,9 @@ class Admin::WatchedWordsController < Admin::AdminController
skip_before_action :check_xhr, only: [:download] skip_before_action :check_xhr, only: [:download]
def index def index
render_json_dump WatchedWordListSerializer.new(WatchedWord.by_action, scope: guardian, root: false) watched_words = WatchedWord.by_action
watched_words = watched_words.where.not(action: WatchedWord.actions[:tag]) if !SiteSetting.tagging_enabled
render_json_dump WatchedWordListSerializer.new(watched_words, scope: guardian, root: false)
end end
def create def create

View File

@ -4,7 +4,8 @@ class WatchedWordListSerializer < ApplicationSerializer
attributes :actions, :words, :regular_expressions, :compiled_regular_expressions attributes :actions, :words, :regular_expressions, :compiled_regular_expressions
def actions def actions
WatchedWord.actions.keys SiteSetting.tagging_enabled ? WatchedWord.actions.keys
: WatchedWord.actions.keys.filter { |k| k != :tag }
end end
def words def words
@ -21,7 +22,7 @@ class WatchedWordListSerializer < ApplicationSerializer
def compiled_regular_expressions def compiled_regular_expressions
expressions = {} expressions = {}
WatchedWord.actions.keys.each do |action| actions.each do |action|
expressions[action] = WordWatcher.word_matcher_regexp(action)&.source expressions[action] = WordWatcher.word_matcher_regexp(action)&.source
end end
expressions expressions

View File

@ -111,11 +111,7 @@ class WordWatcher
end end
end end
def matches?(word) def word_matches?(word)
if SiteSetting.watched_words_regular_expressions? Regexp.new(WordWatcher.word_to_regexp(word), Regexp::IGNORECASE).match?(@raw)
Regexp.new(word).match?(@raw)
else
@raw.include?(word)
end
end end
end end

View File

@ -169,23 +169,23 @@ class TopicCreator
end end
def setup_tags(topic) def setup_tags(topic)
return if @opts[:tags].blank? if @opts[:tags].present?
valid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags])
valid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags]) unless valid_tags
unless valid_tags topic.errors.add(:base, :unable_to_tag)
topic.errors.add(:base, :unable_to_tag) rollback_from_errors!(topic)
rollback_from_errors!(topic)
end
guardian = Guardian.new(Discourse.system_user)
word_watcher = WordWatcher.new("#{@opts[:title]} #{@opts[:raw]}")
word_watcher_tags = topic.tags.map(&:name)
WordWatcher.words_for_action(:tag).each do |word, tags|
if word_watcher.matches?(word)
word_watcher_tags += tags.split(",")
end end
end end
DiscourseTagging.tag_topic_by_names(topic, guardian, word_watcher_tags)
watched_words = WordWatcher.words_for_action(:tag)
if watched_words.present?
word_watcher = WordWatcher.new("#{@opts[:title]} #{@opts[:raw]}")
word_watcher_tags = topic.tags.map(&:name)
watched_words.each do |word, tags|
word_watcher_tags += tags.split(",") if word_watcher.word_matches?(word)
end
DiscourseTagging.tag_topic_by_names(topic, Discourse.system_user.guardian, word_watcher_tags)
end
end end
def setup_auto_close_time(topic) def setup_auto_close_time(topic)

View File

@ -503,10 +503,10 @@ describe PostCreator do
context "without regular expressions" do context "without regular expressions" do
it "works" do it "works" do
Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "hello", replacement: "greetings , hey") Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "HELLO", replacement: "greetings , hey")
@post = creator_with_tags.create @post = creator.create
expect(@post.topic.tags.map(&:name)).to match_array(tag_names + ['greetings', 'hey']) expect(@post.topic.tags.map(&:name)).to match_array(['greetings', 'hey'])
end end
it "does not treat as regular expressions" do it "does not treat as regular expressions" do