DEV: Move autotag to topic creator (#12790)

DEV: Move autotag to topic creator (#12790)

This move was necessary to automatically tag the topic with the right tags from creation time. The process post job may be delayed for a short time.

diff --git a/app/jobs/regular/process_post.rb b/app/jobs/regular/process_post.rb
index bddfc50..b573f06 100644
--- a/app/jobs/regular/process_post.rb
+++ b/app/jobs/regular/process_post.rb
@@ -36,7 +36,6 @@ module Jobs
             post.update_column(:cooked, cp.html)
             post.topic.update_excerpt(post.excerpt_for_topic) if post.is_first_post?
             extract_links(post)
-            auto_tag(post) if SiteSetting.tagging_enabled? && post.post_number == 1
             post.publish_change_to_clients! :revised
           end
         end
@@ -61,25 +60,6 @@ module Jobs
       TopicLink.extract_from(post)
       QuotedPost.extract_from(post)
     end
-
-    def auto_tag(post)
-      word_watcher = WordWatcher.new("#{post.topic.title} #{post.raw}")
-
-      old_tags = post.topic.tags.pluck(:name).to_set
-      new_tags = old_tags.dup
-
-      WordWatcher.words_for_action(:tag).each do |word, tags|
-        new_tags += tags.split(",") if word_watcher.matches?(word)
-      end
-
-      if old_tags != new_tags
-        post.revise(
-          Discourse.system_user,
-          tags: new_tags.to_a,
-          edit_reason: I18n.t(:watched_words_auto_tag)
-        )
-      end
-    end
   end
 
 end
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index 7b0a43f..e1a872e 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -176,6 +176,16 @@ class TopicCreator
       topic.errors.add(:base, :unable_to_tag)
       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
+    DiscourseTagging.tag_topic_by_names(topic, guardian, word_watcher_tags)
   end
 
   def setup_auto_close_time(topic)
diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb
index 0d07c5a..e437e42 100644
--- a/spec/components/post_creator_spec.rb
+++ b/spec/components/post_creator_spec.rb
@@ -494,6 +494,39 @@ describe PostCreator do
               expect(@post.topic.tags.map(&:name)).to eq([existing_tag1.name])
             end
           end
+
+          context "automatically tags first posts" do
+            before do
+              SiteSetting.min_trust_to_create_tag = 0
+              SiteSetting.min_trust_level_to_tag_topics = 0
+            end
+
+            context "without regular expressions" do
+              it "works" do
+                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'])
+              end
+
+              it "does not treat as regular expressions" do
+                Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "he(llo|y)", replacement: "greetings , hey")
+
+                @post = creator_with_tags.create
+                expect(@post.topic.tags.map(&:name)).to match_array(tag_names)
+              end
+            end
+
+            context "with regular expressions" do
+              it "works" do
+                SiteSetting.watched_words_regular_expressions = true
+                Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "he(llo|y)", replacement: "greetings , hey")
+
+                @post = creator_with_tags.create
+                expect(@post.topic.tags.map(&:name)).to match_array(tag_names + ['greetings', 'hey'])
+              end
+            end
+          end
         end
       end
     end
diff --git a/spec/jobs/process_post_spec.rb b/spec/jobs/process_post_spec.rb
index 92920d7..0bc38d6 100644
--- a/spec/jobs/process_post_spec.rb
+++ b/spec/jobs/process_post_spec.rb
@@ -89,49 +89,6 @@ describe Jobs::ProcessPost do
       Jobs::ProcessPost.new.execute(post_id: post2.id)
       expect(post.topic.reload.excerpt).to eq("Some OP content")
     end
-
-    it "automatically tags first posts" do
-      SiteSetting.tagging_enabled = true
-
-      Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "Greetings?", replacement: "hello , world")
-
-      post = Fabricate(:post, raw: "Greeting", cooked: "")
-      Jobs::ProcessPost.new.execute(post_id: post.id)
-      expect(post.topic.reload.tags.pluck(:name)).to contain_exactly()
-
-      post = Fabricate(:post, raw: "Greetings", cooked: "")
-      Jobs::ProcessPost.new.execute(post_id: post.id)
-      expect(post.topic.reload.tags.pluck(:name)).to contain_exactly()
-
-      post = Fabricate(:post, raw: "Greetings?", cooked: "")
-      Jobs::ProcessPost.new.execute(post_id: post.id)
-      expect(post.topic.reload.tags.pluck(:name)).to contain_exactly("hello", "world")
-
-      topic = Fabricate(:topic, title: "Greetings? People")
-      post = Fabricate(:post, topic: topic, raw: "nothing yet", cooked: "")
-
-      Jobs::ProcessPost.new.execute(post_id: post.id)
-      expect(post.topic.reload.tags.pluck(:name)).to contain_exactly("hello", "world")
-    end
-
-    it "automatically tags first posts (regex)" do
-      SiteSetting.tagging_enabled = true
-      SiteSetting.watched_words_regular_expressions = true
-
-      Fabricate(:watched_word, action: WatchedWord.actions[:tag], word: "Greetings?", replacement: "hello , world")
-
-      post = Fabricate(:post, raw: "Greeting", cooked: "")
-      Jobs::ProcessPost.new.execute(post_id: post.id)
-      expect(post.topic.reload.tags.pluck(:name)).to contain_exactly("hello", "world")
-
-      post = Fabricate(:post, raw: "Greetings", cooked: "")
-      Jobs::ProcessPost.new.execute(post_id: post.id)
-      expect(post.topic.reload.tags.pluck(:name)).to contain_exactly("hello", "world")
-
-      post = Fabricate(:post, raw: "Greetings?", cooked: "")
-      Jobs::ProcessPost.new.execute(post_id: post.id)
-      expect(post.topic.reload.tags.pluck(:name)).to contain_exactly("hello", "world")
-    end
   end
 
 end

GitHub sha: 8c4a11c0

This commit appears in #12790 which was approved by ZogStriP. It was merged by nbianca.