From 9332d77483aa1c79e6f4df016e137aeb88ca303e Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Tue, 18 Apr 2023 13:27:17 -0500 Subject: [PATCH] FEATURE: Trigger follow rules when category changes (#164) --- app/services/manager.rb | 18 ++++++++++++++---- spec/services/manager_spec.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/app/services/manager.rb b/app/services/manager.rb index e8c370f..2baa7a3 100644 --- a/app/services/manager.rb +++ b/app/services/manager.rb @@ -15,9 +15,12 @@ module DiscourseChatIntegration # Abort if the post is blank return if post.blank? - # Abort if post is not either regular, or a 'tags_changed' small action + # Abort if post is not either regular, or a 'tags_changed'/'category_changed' small action if (post.post_type != Post.types[:regular]) && - !(post.post_type == Post.types[:small_action] && post.action_code == "tags_changed") + !( + post.post_type == Post.types[:small_action] && + %w[tags_changed category_changed].include?(post.action_code) + ) return end @@ -104,9 +107,16 @@ module DiscourseChatIntegration # If a matching rule is set to mute, we can discard it now matching_rules = matching_rules.select { |rule| rule.filter != "mute" } - # If this is not the first post, discard all "follow" rules + # If this is not the first post, discard all "follow" rules. Unless it's a + # category_changed action post. If category changed, filter out and rules + # that aren't specific to a category if !post.is_first_post? - matching_rules = matching_rules.select { |rule| rule.filter != "follow" } + matching_rules = + if post.action_code == "category_changed" + matching_rules.select { |rule| rule.category_id.present? } + else + matching_rules.select { |rule| rule.filter != "follow" } + end end # All remaining rules now require a notification to be sent diff --git a/spec/services/manager_spec.rb b/spec/services/manager_spec.rb index f0823bd..e9d8e5c 100644 --- a/spec/services/manager_spec.rb +++ b/spec/services/manager_spec.rb @@ -316,6 +316,38 @@ RSpec.describe DiscourseChatIntegration::Manager do expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id) end + describe "With `create_post_for_category_and_tag_changes` enabled" do + before(:each) { SiteSetting.create_post_for_category_and_tag_changes = true } + + let(:admin) { Fabricate(:admin) } + let(:other_topic) { Fabricate(:topic) } + let(:other_topic_post) { Fabricate(:post, topic: topic) } + + it "should trigger follow rules for specific categories when topic category changes" do + DiscourseChatIntegration::Rule.create!( + channel: chan1, + filter: "follow", + category_id: category.id, + ) + + PostRevisor.new(other_topic_post).revise!(admin, category_id: category.id) + + manager.trigger_notifications(topic.ordered_posts.last.id) + + expect(provider.sent_to_channel_ids).to contain_exactly(chan1.id) + end + + it "shouldn't trigger follow rules with wildcard category match" do + DiscourseChatIntegration::Rule.create!(channel: chan1, filter: "follow", category_id: nil) + + PostRevisor.new(other_topic_post).revise!(admin, category_id: category.id) + + manager.trigger_notifications(topic.ordered_posts.last.id) + + expect(provider.sent_to_channel_ids).to contain_exactly + end + end + describe "with tags enabled" do let(:tag) { Fabricate(:tag, name: "gsoc") } let(:tagged_topic) { Fabricate(:topic, category_id: category.id, tags: [tag]) }