From 157c8e660aa2aa057b4c2fe5f9611028d198dc93 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba <70247653+Grubba27@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:05:16 -0300 Subject: [PATCH] FEATURE: Change tags sent in topic_tags_changed trigger in automation plugin (#28318) * FEATURE: Change tags sent in topic_tags_changed trigger in discourse_automation Before, it was sending the old tags and the current tags in topic. Now, it sends the removed tags and the added tags in the topic. * DEV: update `missing_tags` to be `removed_tags` * DEV: add spacing for better readability --- .../discourse_automation/event_handlers.rb | 12 ++++-- .../spec/triggers/topic_tags_changed_spec.rb | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/plugins/automation/lib/discourse_automation/event_handlers.rb b/plugins/automation/lib/discourse_automation/event_handlers.rb index 997a00c3eae..4926e1f0271 100644 --- a/plugins/automation/lib/discourse_automation/event_handlers.rb +++ b/plugins/automation/lib/discourse_automation/event_handlers.rb @@ -206,12 +206,16 @@ module DiscourseAutomation next if !watching_categories["value"].include?(topic.category_id) end + removed_tags = old_tag_names - new_tag_names + added_tags = new_tag_names - old_tag_names + watching_tags = automation.trigger_field("watching_tags") + if watching_tags["value"] should_skip = false watching_tags["value"].each do |tag| - should_skip = true if !old_tag_names.empty? && !old_tag_names.include?(tag) - should_skip = true if !new_tag_names.empty? && !new_tag_names.include?(tag) + should_skip = true if !removed_tags.empty? && !removed_tags.include?(tag) + should_skip = true if !added_tags.empty? && !added_tags.include?(tag) end next if should_skip end @@ -219,8 +223,8 @@ module DiscourseAutomation automation.trigger!( "kind" => name, "topic" => topic, - "removed_tags" => old_tag_names, - "added_tags" => new_tag_names, + "removed_tags" => removed_tags, + "added_tags" => added_tags, ) end end diff --git a/plugins/automation/spec/triggers/topic_tags_changed_spec.rb b/plugins/automation/spec/triggers/topic_tags_changed_spec.rb index 25e2c7190fe..d337ca0a0ef 100644 --- a/plugins/automation/spec/triggers/topic_tags_changed_spec.rb +++ b/plugins/automation/spec/triggers/topic_tags_changed_spec.rb @@ -5,6 +5,8 @@ describe DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED do fab!(:cool_tag) { Fabricate(:tag) } fab!(:bad_tag) { Fabricate(:tag) } + fab!(:another_tag) { Fabricate(:tag) } + fab!(:category) fab!(:user) @@ -132,5 +134,41 @@ describe DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED do expect(list.length).to eq(1) expect(list[0]["kind"]).to eq(DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED) end + + it "should send the correct removed tags in context" do + topic_0 = Fabricate(:topic, user: user, tags: [cool_tag], category: category) + + list = + capture_contexts do + DiscourseTagging.tag_topic_by_names( + topic_0, + Guardian.new(user), + [bad_tag.name, another_tag.name], + ) + end + + expect(list.length).to eq(1) + expect(list[0]["kind"]).to eq(DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED) + expect(list[0]["added_tags"]).to eq([bad_tag.name, another_tag.name]) + expect(list[0]["removed_tags"]).to eq([cool_tag.name]) + end + + it "should send the correct added tags in context" do + topic_0 = Fabricate(:topic, user: user, tags: [cool_tag], category: category) + + list = + capture_contexts do + DiscourseTagging.tag_topic_by_names( + topic_0, + Guardian.new(user), + [cool_tag.name, another_tag.name], + ) + end + + expect(list.length).to eq(1) + expect(list[0]["kind"]).to eq(DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED) + expect(list[0]["added_tags"]).to eq([another_tag.name]) + expect(list[0]["removed_tags"]).to eq([]) + end end end