FEATURE: Add setting to disable notifications for topic category edits (#14632)

FEATURE: Add setting to disable notifications for topic category edits (#14632)

diff --git a/app/models/topic.rb b/app/models/topic.rb
index 4c9f0f8..5f807a7 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -844,7 +844,7 @@ class Topic < ActiveRecord::Base
         CategoryUser.auto_watch(category_id: new_category.id, topic_id: self.id)
         CategoryUser.auto_track(category_id: new_category.id, topic_id: self.id)
 
-        if post = self.ordered_posts.first
+        if !SiteSetting.disable_category_edit_notifications && (post = self.ordered_posts.first)
           notified_user_ids = [post.user_id, post.last_editor_id].uniq
           DB.after_commit do
             Jobs.enqueue(:notify_category_change, post_id: post.id, notified_user_ids: notified_user_ids)
diff --git a/app/services/post_action_notifier.rb b/app/services/post_action_notifier.rb
index 881af49..f58b784 100644
--- a/app/services/post_action_notifier.rb
+++ b/app/services/post_action_notifier.rb
@@ -103,6 +103,9 @@ class PostActionNotifier
     return if post.topic.blank?
     return if post.topic.private_message?
     return if SiteSetting.disable_system_edit_notifications && post_revision.user_id == Discourse::SYSTEM_USER_ID
+    if SiteSetting.disable_category_edit_notifications && post_revision.modifications&.dig("category_id").present?
+      return
+    end
 
     user_ids = []
 
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 43d4c43..da3b533 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2129,6 +2129,8 @@ en:
 
     disable_system_edit_notifications: "Disables edit notifications by the system user when 'download_remote_images_to_local' is active."
 
+    disable_category_edit_notifications: "Disable category edit notifications on topics."
+
     notification_consolidation_threshold: "Number of liked or membership request notifications received before the notifications are consolidated into a single one. Set to 0 to disable."
 
     likes_notification_consolidation_window_mins: "Duration in minutes where liked notifications are consolidated into a single notification once the threshold has been reached. The threshold can be configured via `SiteSetting.notification_consolidation_threshold`."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 5a5ff88..b7c97ef 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2210,6 +2210,9 @@ uncategorized:
 
   disable_system_edit_notifications: true
 
+  disable_category_edit_notifications:
+    default: false
+
   notification_consolidation_threshold:
     default: 3
     min: 0
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index cbc5bfb..64d599f 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -1529,6 +1529,7 @@ describe Topic do
     fab!(:category) { Fabricate(:category_with_definition, user: user) }
 
     describe 'without a previous category' do
+
       it 'changes the category' do
         topic.change_category_to_id(category.id)
         category.reload
@@ -1620,6 +1621,16 @@ describe Topic do
             ).exists?).to eq(true)
           end
 
+          it 'should not generate a notification if SiteSetting.disable_category_edit_notifications is enabled' do
+            SiteSetting.disable_category_edit_notifications = true
+
+            expect do
+              topic.change_category_to_id(new_category.id)
+            end.to change { Notification.count }.by(0)
+
+            expect(topic.category_id).to eq(new_category.id)
+          end
+
           it 'should generate the modified notification for the topic if already seen' do
             TopicUser.create!(
               topic_id: topic.id,
diff --git a/spec/services/post_action_notifier_spec.rb b/spec/services/post_action_notifier_spec.rb
index f1bf3e0..fbdbd67 100644
--- a/spec/services/post_action_notifier_spec.rb
+++ b/spec/services/post_action_notifier_spec.rb
@@ -153,6 +153,25 @@ describe PostActionNotifier do
 
     end
 
+    context "category edit notifications are disabled" do
+      it 'notifies a user of the revision made by another user' do
+        SiteSetting.disable_category_edit_notifications = false
+
+        expect {
+          post.revise(evil_trout, category_id: Fabricate(:category).id)
+        }.to change(post.user.notifications, :count).by(1)
+      end
+
+      it 'does not notify a user of the revision made by the system user' do
+        SiteSetting.disable_category_edit_notifications = true
+
+        expect {
+          post.revise(evil_trout, category_id: Fabricate(:category).id)
+        }.not_to change(post.user.notifications, :count)
+      end
+
+    end
+
     context 'when using plugin API to add custom recipients' do
       let(:lurker) { Fabricate(:user) }
 

GitHub sha: 6275658e3d3fc213e09746ad8200f0ca8b002e90

This commit appears in #14632 which was approved by CvX. It was merged by jmperez127.