A platform for community discussion. Free, open, simple.
FIX: Make autotag watched words case insensitive (#13043)
Bianca Nenciu3a1b05f21971
diff --git a/app/controllers/admin/watched_words_controller.rb b/app/controllers/admin/watched_words_controller.rbindex bfc039441f1..5423c329729 100644--- a/app/controllers/admin/watched_words_controller.rb+++ b/app/controllers/admin/watched_words_controller.rb@@ -6,7 +6,9 @@ class Admin::WatchedWordsController < Admin::AdminControllerskip_before_action :check_xhr, only: [:download]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)enddef creatediff --git a/app/serializers/watched_word_list_serializer.rb b/app/serializers/watched_word_list_serializer.rbindex 5cc151c2093..ec71b87af2e 100644--- a/app/serializers/watched_word_list_serializer.rb+++ b/app/serializers/watched_word_list_serializer.rb@@ -4,7 +4,8 @@ class WatchedWordListSerializer < ApplicationSerializerattributes :actions, :words, :regular_expressions, :compiled_regular_expressionsdef actions- WatchedWord.actions.keys+ SiteSetting.tagging_enabled ? WatchedWord.actions.keys+ : WatchedWord.actions.keys.filter { |k| k != :tag }enddef words@@ -21,7 +22,7 @@ class WatchedWordListSerializer < ApplicationSerializerdef compiled_regular_expressionsexpressions = {}- WatchedWord.actions.keys.each do |action|+ actions.each do |action|expressions[action] = WordWatcher.word_matcher_regexp(action)&.sourceendexpressionsdiff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rbindex c37daea4da2..d3164e44a8c 100644--- a/app/services/word_watcher.rb+++ b/app/services/word_watcher.rb@@ -111,11 +111,7 @@ class WordWatcherendend- def matches?(word)- if SiteSetting.watched_words_regular_expressions?- Regexp.new(word).match?(@raw)- else- @raw.include?(word)- end+ def word_matches?(word)+ Regexp.new(WordWatcher.word_to_regexp(word), Regexp::IGNORECASE).match?(@raw)endenddiff --git a/lib/topic_creator.rb b/lib/topic_creator.rbindex e1a872ecc01..7ba59e945aa 100644--- a/lib/topic_creator.rb+++ b/lib/topic_creator.rb@@ -169,23 +169,23 @@ class TopicCreatorenddef setup_tags(topic)- return if @opts[:tags].blank?-- valid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags])- unless valid_tags- topic.errors.add(:base, :unable_to_tag)- rollback_from_errors!(topic)+ if @opts[:tags].present?+ valid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags])+ unless valid_tags+ topic.errors.add(:base, :unable_to_tag)+ rollback_from_errors!(topic)+ endend- 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(",")+ 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- DiscourseTagging.tag_topic_by_names(topic, guardian, word_watcher_tags)enddef setup_auto_close_time(topic)diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rbindex e437e42406c..3f4262a65ae 100644--- a/spec/components/post_creator_spec.rb+++ b/spec/components/post_creator_spec.rb@@ -503,10 +503,10 @@ describe PostCreator docontext "without regular expressions" doit "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- expect(@post.topic.tags.map(&:name)).to match_array(tag_names + ['greetings', 'hey'])+ @post = creator.create+ expect(@post.topic.tags.map(&:name)).to match_array(['greetings', 'hey'])endit "does not treat as regular expressions" do