Skip to content

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

FIX: Show warning when bulk category change fails due to tag restrictions (#37570)

Régis Hanolfc6f018cf88b
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index e2c30a1ae99..02abeec855e 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3682,6 +3682,9 @@ en:
notify: "Notify users of this change."
performing: "Performing bulk operations, please wait…"
completed: "Bulk operations completed successfully!"
+ not_completed:
+ one: "Bulk operation could not be completed for <strong>%{count}</strong> topic"
+ other: "Bulk operation could not be completed for <strong>%{count}</strong> topics"
none:
unread: "You have no unread topics."
diff --git a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
index d99e18dac95..02e7f5af065 100644
--- a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
+++ b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
@@ -37,6 +37,7 @@ export default class BulkTopicActions extends Component {
@tracked errors;
@tracked notifyUsers = false;
@tracked closeNote = null;
+ @tracked failedTopicCount = 0;
@tracked notificationLevelId = null;
@@ -247,6 +248,15 @@ export default class BulkTopicActions extends Component {
duration: "short",
data: { message: i18n("generic_error") },
});
+ } else if (this.failedTopicCount > 0) {
+ this.toasts.warning({
+ duration: "short",
+ data: {
+ message: htmlSafe(
+ i18n("topics.bulk.not_completed", { count: this.failedTopicCount })
+ ),
+ },
+ });
} else {
this.toasts.success({
duration: "short",
@@ -257,10 +267,12 @@ export default class BulkTopicActions extends Component {
@action
async forEachPerformed(operation, cb) {
+ const selectedCount = this.model.bulkSelectHelper.selected.length;
const topics = await this.perform(operation);
if (topics) {
topics.forEach(cb);
+ this.failedTopicCount = selectedCount - topics.length;
this.model.refreshClosure?.();
this.args.closeModal();
this.model.bulkSelectHelper.toggleBulkSelect();
@@ -270,8 +282,10 @@ export default class BulkTopicActions extends Component {
@action
async performAndRefresh(operation) {
- await this.perform(operation);
+ const selectedCount = this.model.bulkSelectHelper.selected.length;
+ const topics = await this.perform(operation);
+ this.failedTopicCount = topics ? selectedCount - topics.length : 0;
this.model.refreshClosure?.().then(() => {
this.args.closeModal();
this.model.bulkSelectHelper.toggleBulkSelect();
diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb
index 6198703e39b..1168dee3f67 100644
--- a/lib/topics_bulk_action.rb
+++ b/lib/topics_bulk_action.rb
@@ -66,6 +66,7 @@ class TopicsBulkAction
else
UserArchivedMessage.move_to_inbox!(@user.id, t)
end
+ @changed_ids << t.id
end
end
end
@@ -79,6 +80,7 @@ class TopicsBulkAction
else
UserArchivedMessage.archive!(@user.id, t)
end
+ @changed_ids << t.id
end
end
end
@@ -134,27 +136,18 @@ class TopicsBulkAction
def change_category
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,
- silent: @operation[:silent],
- }
-
- updatable_topics.each do |t|
- if guardian.can_edit?(t)
- changes = { category_id: @operation[:category_id] }
- @changed_ids << t.id if t.first_post.revise(@user, changes, opts)
- end
- end
- else
- updatable_topics.each do |t|
- if guardian.can_edit?(t)
- if t.change_category_to_id(@operation[:category_id], silent: @operation[:silent])
- @changed_ids << t.id
- end
- end
+ opts = {
+ bypass_bump: true,
+ validate_post: false,
+ bypass_rate_limiter: true,
+ silent: @operation[:silent],
+ skip_revision: !SiteSetting.create_revision_on_bulk_topic_moves,
+ }
+
+ updatable_topics.each do |t|
+ if guardian.can_edit?(t)
+ changes = { category_id: @operation[:category_id] }
+ @changed_ids << t.id if t.first_post.revise(@user, changes, opts)
end
end
end
diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb
index dedd026abe2..f529d1270f3 100644
--- a/spec/lib/topics_bulk_action_spec.rb
+++ b/spec/lib/topics_bulk_action_spec.rb
@@ -312,6 +312,78 @@ RSpec.describe TopicsBulkAction do
expect(topic.reload.category).to eq(original_category)
end
end
+
+ context "when destination category does not allow the topic's tags" do
+ fab!(:destination_category, :category)
+ fab!(:other_tag) { Fabricate(:tag, name: "other-tag") }
+ fab!(:restricted_tag) { Fabricate(:tag, name: "restricted-tag") }
+ fab!(:source_category) { Fabricate(:category, tags: [restricted_tag]) }
+ fab!(:admin)
+ fab!(:topic_with_tag) { Fabricate(:topic, category: source_category, tags: [restricted_tag]) }
+ fab!(:first_post_for_tagged_topic) { Fabricate(:post, topic: topic_with_tag) }
+
+ before { destination_category.update!(tags: [other_tag]) }
+
+ it "does not change category" do
+ original_category = topic_with_tag.category
+
+ topic_ids =
+ TopicsBulkAction.new(
+ admin,
+ [topic_with_tag.id],
+ type: "change_category",
+ category_id: destination_category.id,
+ ).perform!
+
+ expect(topic_ids).to eq([])
+ expect(topic_with_tag.reload.category).to eq(original_category)
+ end
+ end
+
+ context "when destination category has the same tag group as source" do
+ fab!(:restricted_tag) { Fabricate(:tag, name: "restricted-tag") }
+ fab!(:tag_group) { Fabricate(:tag_group, tags: [restricted_tag]) }
+ fab!(:source_category) { Fabricate(:category, tag_groups: [tag_group]) }
+ fab!(:destination_category) { Fabricate(:category, tag_groups: [tag_group]) }
+ fab!(:admin)
+ fab!(:topic_with_tag) { Fabricate(:topic, category: source_category, tags: [restricted_tag]) }
+ fab!(:first_post_for_tagged_topic) { Fabricate(:post, topic: topic_with_tag) }
+
+ it "changes category successfully" do
+ topic_ids =
+ TopicsBulkAction.new(
+ admin,
+ [topic_with_tag.id],
+ type: "change_category",
+ category_id: destination_category.id,
+ ).perform!
+
+ expect(topic_ids).to eq([topic_with_tag.id])
+ expect(topic_with_tag.reload.category).to eq(destination_category)
+ end
+ end
+
+ context "when destination category has allow_global_tags enabled" do
+ fab!(:global_tag) { Fabricate(:tag, name: "global-tag") }
+ fab!(:source_category, :category)
+ fab!(:destination_category) { Fabricate(:category, allow_global_tags: true) }
+ fab!(:admin)
+ fab!(:topic_with_tag) { Fabricate(:topic, category: source_category, tags: [global_tag]) }
+ fab!(:first_post_for_tagged_topic) { Fabricate(:post, topic: topic_with_tag) }
+
+ it "changes category successfully for unrestricted tags" do
+ topic_ids =
+ TopicsBulkAction.new(
+ admin,
+ [topic_with_tag.id],
+ type: "change_category",
+ category_id: destination_category.id,
+ ).perform!
+
+ expect(topic_ids).to eq([topic_with_tag.id])
+ expect(topic_with_tag.reload.category).to eq(destination_category)
+ end
+ end
end
describe "destroy_post_timing" do
diff --git a/spec/system/page_objects/modals/topic_bulk_actions.rb b/spec/system/page_objects/modals/topic_bulk_actions.rb
index e366d5f8b44..96861182117 100644
--- a/spec/system/page_objects/modals/topic_bulk_actions.rb
+++ b/spec/system/page_objects/modals/topic_bulk_actions.rb
@@ -8,6 +8,10 @@ module PageObjects
PageObjects::Components::SelectKit.new(".tag-chooser")
end
+ def category_selector
+ PageObjects::Components::SelectKit.new(".category-chooser")
+ end
+
def click_bulk_topics_confirm
find("#bulk-topics-confirm").click
end
diff --git a/spec/system/topic_bulk_select_spec.rb b/spec/system/topic_bulk_select_spec.rb
index 453dcb9e334..c4a0e5ec912 100644
--- a/spec/system/topic_bulk_select_spec.rb
+++ b/spec/system/topic_bulk_select_spec.rb
@@ -448,4 +448,33 @@ describe "Topic bulk select", type: :system do
expect(topic_list).to have_no_topic(topics.second)
end
end
+
+ context "when changing category" do
+ fab!(:destination_category, :category)
+ fab!(:restricted_tag, :tag)
+
+ let(:toasts) { PageObjects::Components::Toasts.new }
+
+ before do
+ SiteSetting.tagging_enabled = true
+ topics.first.update!(tags: [restricted_tag])
+ topics.first.category.update!(tags: [restricted_tag])
+ end
+
+ it "shows a warning when some topics cannot be moved due to tag restrictions" do
+ original_category = topics.first.category
+ sign_in(admin)
+ visit("/latest")
+
+ open_bulk_actions_modal([topics.first], "update-category")
+
+ topic_bulk_actions_modal.category_selector.expand
+ topic_bulk_actions_modal.category_selector.select_row_by_value(destination_category.id)
+ topic_bulk_actions_modal.click_bulk_topics_confirm
+
+ expect(toasts).to have_warning("could not be completed")
+
+ expect(topics.first.reload.category).to eq(original_category)
+ end
+ end
end