Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Allow users to bulk recategorize silently (#33490)

Linca7a099ebb6a4c
diff --git a/app/assets/javascripts/discourse/app/components/bulk-select-topics-dropdown.gjs b/app/assets/javascripts/discourse/app/components/bulk-select-topics-dropdown.gjs
index 3d8acb61a9b..4bb1dcb27cf 100644
--- a/app/assets/javascripts/discourse/app/components/bulk-select-topics-dropdown.gjs
+++ b/app/assets/javascripts/discourse/app/components/bulk-select-topics-dropdown.gjs
@@ -221,6 +221,7 @@ export default class BulkSelectTopicsDropdown extends Component {
break;
case "update-category":
this.showBulkTopicActionsModal(actionId, "change_category", {
+ allowSilent: true,
description: i18n(`topic_bulk_actions.update_category.description`),
});
break;
diff --git a/app/assets/javascripts/discourse/app/components/modal/bulk-topic-actions.gjs b/app/assets/javascripts/discourse/app/components/modal/bulk-topic-actions.gjs
index 2a385613ffa..5f5c8522e76 100644
--- a/app/assets/javascripts/discourse/app/components/modal/bulk-topic-actions.gjs
+++ b/app/assets/javascripts/discourse/app/components/modal/bulk-topic-actions.gjs
@@ -88,9 +88,7 @@ export default class BulkTopicActions extends Component {
const options = {};
if (this.isSilent) {
- const newType =
- operation.type === "close" ? "silent_close" : operation.type;
- operation.type = newType;
+ operation.silent = true;
}
if (this.isCloseAction && this.closeNote) {
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 36b5b7ac6ef..90604fa5dc6 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -1048,12 +1048,17 @@ class TopicsController < ApplicationController
:category_id,
:notification_level_id,
:message,
+ :silent,
*DiscoursePluginRegistry.permitted_bulk_action_parameters,
tags: [],
)
.to_h
.symbolize_keys
+ if operation.has_key? :silent
+ operation[:silent] = ActiveModel::Type::Boolean.new.cast(operation[:silent])
+ end
+
raise ActionController::ParameterMissing.new(:operation_type) if operation[:type].blank?
operator = TopicsBulkAction.new(current_user, topic_ids, operation, group: operation[:group])
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 27eb3a5b11c..0548398703c 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -1021,7 +1021,7 @@ class Topic < ActiveRecord::Base
cattr_accessor :update_featured_topics
- def changed_to_category(new_category)
+ def changed_to_category(new_category, silent: nil)
return true if new_category.blank? || Category.exists?(topic_id: id)
if new_category.id == SiteSetting.uncategorized_category_id &&
@@ -1053,7 +1053,9 @@ 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 !SiteSetting.disable_category_edit_notifications && (post = self.ordered_posts.first)
+ skip_alert = silent || SiteSetting.disable_category_edit_notifications
+
+ if !skip_alert && (post = self.ordered_posts.first)
notified_user_ids = [post.user_id, post.last_editor_id].uniq
DB.after_commit do
Jobs.enqueue(
@@ -1130,7 +1132,7 @@ class Topic < ActiveRecord::Base
new_post
end
- def change_category_to_id(category_id)
+ def change_category_to_id(category_id, silent: nil)
return false if private_message?
new_category_id = category_id.to_i
@@ -1144,7 +1146,7 @@ class Topic < ActiveRecord::Base
reviewables.update_all(category_id: new_category_id)
- changed_to_category(cat)
+ changed_to_category(cat, silent: silent)
end
def remove_allowed_group(removed_by, name)
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index af0ee1e1865..5881853d0aa 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -96,7 +96,7 @@ class PostRevisor
end
tc.record_change("category_id", current_category&.id, new_category&.id)
- tc.check_result(tc.topic.change_category_to_id(new_category_id))
+ tc.check_result(tc.topic.change_category_to_id(new_category_id, silent: @silent))
create_small_action_for_category_change(
topic: tc.topic,
user: tc.user,
@@ -215,6 +215,7 @@ class PostRevisor
# - skip_validations: ask ActiveRecord to skip validations
# - skip_revision: do not create a new PostRevision record
# - skip_staff_log: skip creating an entry in the staff action log
+ # - silent: don't send notifications to user
def revise!(editor, fields, opts = {})
@editor = editor
@fields = fields.with_indifferent_access
@@ -271,6 +272,9 @@ class PostRevisor
@skip_revision = false
@skip_revision = @opts[:skip_revision] if @opts.has_key?(:skip_revision)
+ @silent = false
+ @silent = @opts[:silent] if @opts.has_key?(:silent)
+
@post.incoming_email&.update(imap_sync: true) if @post.incoming_email&.imap_uid
old_raw = @post.raw
diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb
index 5cdd985aabf..707ade47f06 100644
--- a/lib/topics_bulk_action.rb
+++ b/lib/topics_bulk_action.rb
@@ -13,7 +13,6 @@ class TopicsBulkAction
@operations ||= %w[
change_category
close
- silent_close
archive
change_notification_level
destroy_post_timing
@@ -136,7 +135,12 @@ class TopicsBulkAction
updatable_topics = topics.where.not(category_id: @operation[:category_id])
if SiteSetting.create_revision_on_bulk_topic_moves
- opts = { bypass_bump: true, validate_post: false, bypass_rate_limiter: true }
+ opts = {
+ bypass_bump: true,
+ validate_post: false,
+ bypass_rate_limiter: true,
+ silent: @operation[:silent],
+ }
updatable_topics.each do |t|
if guardian.can_edit?(t)
@@ -147,7 +151,9 @@ class TopicsBulkAction
else
updatable_topics.each do |t|
if guardian.can_edit?(t)
- @changed_ids << t.id if t.change_category_to_id(@operation[:category_id])
+ if t.change_category_to_id(@operation[:category_id], silent: @operation[:silent])
+ @changed_ids << t.id
+ end
end
end
end
@@ -167,22 +173,13 @@ class TopicsBulkAction
end
def close
- topics.each do |t|
- if guardian.can_moderate?(t)
- t.update_status("closed", true, @user, { message: @operation[:message] })
- @changed_ids << t.id
- end
- end
- end
-
- def silent_close
topics.each do |t|
if guardian.can_moderate?(t)
t.update_status(
"closed",
true,
@user,
- { message: @operation[:message], silent_tracking: true },
+ { message: @operation[:message], silent_tracking: @operation[:silent] },
)
@changed_ids << t.id
end
diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb
index f97db6d86cf..49f9ec6450d 100644
--- a/spec/lib/topics_bulk_action_spec.rb
+++ b/spec/lib/topics_bulk_action_spec.rb
@@ -160,6 +160,58 @@ RSpec.describe TopicsBulkAction do
fab!(:category)
fab!(:fist_post) { Fabricate(:post, topic: topic) }
+ describe "option 'perform action silently'" do
+ fab!(:watcher) { Fabricate(:user) }
+ fab!(:admin)
+
+ before do
+ Jobs.run_immediately!
+ TopicUser.change(
+ watcher,
+ topic.id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+ end
+
+ shared_examples "performing with `silent` option provided" do
+ context "when 'silent` option is `false` or not present" do
+ it "will send notification to users watching the topic" do
+ expect do
+ TopicsBulkAction.new(
+ admin,
+ [topic.id],
+ type: "change_category",
+ category_id: category.id,
+ ).perform!
+ end.to change { Notification.where(user: watcher).count }
+ end
+ end
+
+ context "when 'silent' option is `true`" do
+ it "will not send notification to users watching the topic" do
+ expect do
+ TopicsBulkAction.new(
+ admin,
+ [topic.id],
+ type: "change_category",
+ category_id: category.id,
+ silent: true,
+ ).perform!
+ end.to_not change { Notification.where(user: watcher).count }
+ end
+ end
+ end
+
+ context "when 'create_revision_on_bulk_topic_moves' enabled" do
+ SiteSetting.create_revision_on_bulk_topic_moves = true
+ include_examples "performing with `silent` option provided"
+ end
+ context "when 'create_revision_on_bulk_topic_moves' disabled" do
+ SiteSetting.create_revision_on_bulk_topic_moves = false
+ include_examples "performing with `silent` option provided"
+ end
+ end
+
context "when the user can edit the topic" do
context "with 'create_revision_on_bulk_topic_moves' setting enabled" do
before { SiteSetting.create_revision_on_bulk_topic_moves = true }
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index c399555426b..fa20009110d 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -1909,6 +1909,14 @@ describe Topic do
expect(topic.category_id).to eq(new_category.id)
end
+ it "should not generate a notification if options: silent is true" do
+ expect do topic.change_category_to_id(new_category.id, silent: true) end.not_to change {
+ Notification.count
+ }
+
+ 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/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 848e2c92169..41c542c00d3 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -3743,7 +3743,7 @@ RSpec.describe TopicsController do
describe "when logged in" do
before { sign_in(user) }
- let!(:operation) { { type: "change_category", category_id: "1" } }
+ let!(:operation) { { type: "change_category", category_id: "1", silent: true } }
let!(:topic_ids) { [1, 2, 3] }
it "requires a list of topic_ids or filter" do