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.
This commit is contained in:
parent
a97e3e249d
commit
8c4a11c006
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue