Skip to content

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

UX: Consolidate bulk tag actions into a single "Manage tags" modal (#39340)

Alan Guo Xiang Tan6d2f80fef51d
diff --git a/app/assets/stylesheets/common/components/_index.scss b/app/assets/stylesheets/common/components/_index.scss
index a33b09d52dd..e1d87f32590 100644
--- a/app/assets/stylesheets/common/components/_index.scss
+++ b/app/assets/stylesheets/common/components/_index.scss
@@ -9,6 +9,7 @@
@import "bookmark-list";
@import "bookmark-modal";
@import "bookmark-menu";
+@import "bulk-topic-actions-manage-tags";
@import "buttons";
@import "d-segmented-control";
@import "d-select";
diff --git a/app/assets/stylesheets/common/components/bulk-topic-actions-manage-tags.scss b/app/assets/stylesheets/common/components/bulk-topic-actions-manage-tags.scss
new file mode 100644
index 00000000000..c51a281d688
--- /dev/null
+++ b/app/assets/stylesheets/common/components/bulk-topic-actions-manage-tags.scss
@@ -0,0 +1,63 @@
+@use "lib/viewport";
+
+.manage-tags-form {
+ &__remove-section {
+ position: relative;
+
+ #control-remove_all_tags {
+ position: absolute;
+ top: 0;
+ right: 0;
+ width: auto;
+
+ .form-kit__container-title {
+ color: var(--primary-medium);
+ font-weight: normal !important;
+ white-space: nowrap;
+ }
+ }
+ }
+
+ &__warning {
+ margin-top: 2rem;
+ }
+
+ &__replace {
+ .form-kit__container-content {
+ flex-direction: column;
+ row-gap: var(--space-2);
+ }
+
+ .form-kit__row {
+ padding-top: 0;
+
+ // Form-kit collapses every `--col-*` to 100% width below `lg`, which
+ // stacks the from-chooser, arrow, to-chooser and remove button into a
+ // tall column. Restore the desktop ratios so the replace row stays on
+ // a single line on mobile.
+ // TODO: looking at adding a proper opt-out in form-kit so we don't
+ // need this override.
+ @include viewport.until(lg) {
+ .--col-1 {
+ width: 8.33333333% !important;
+ }
+
+ .--col-5 {
+ width: 41.66666667% !important;
+ }
+ }
+ }
+ }
+
+ &__replace-arrow {
+ text-align: center;
+ height: 2.25em;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ }
+
+ &__replace-row-add {
+ width: 100%;
+ }
+}
diff --git a/app/assets/stylesheets/common/modal/modal-overrides.scss b/app/assets/stylesheets/common/modal/modal-overrides.scss
index 211f3cb4f26..4ce0f6fe695 100644
--- a/app/assets/stylesheets/common/modal/modal-overrides.scss
+++ b/app/assets/stylesheets/common/modal/modal-overrides.scss
@@ -252,12 +252,6 @@
}
}
-.topic-bulk-actions-modal {
- &__selection-info {
- margin-bottom: 0.5em;
- }
-}
-
.d-modal.edit-slow-mode-modal {
.slow-mode-label {
display: inline-flex;
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 203da4187d6..28ca8db1191 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -1139,14 +1139,18 @@ class TopicsController < ApplicationController
:silent,
:pinned_globally,
:pinned_until,
+ :remove_all_tags,
*DiscoursePluginRegistry.permitted_bulk_action_parameters,
tag_ids: [],
tags: [],
+ add_tag_ids: [],
+ remove_tag_ids: [],
+ replace_tags: %i[from_tag_id to_tag_id],
)
.to_h
- .symbolize_keys
+ .deep_symbolize_keys
- %i[silent pinned_globally].each do |key|
+ %i[silent pinned_globally remove_all_tags].each do |key|
operation[key] = ActiveModel::Type::Boolean.new.cast(operation[key]) if operation.has_key?(
key,
)
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 694ec195f7f..b041ab0c091 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3791,27 +3791,19 @@ en:
notification_level: "Update notifications"
change_notification_level: "Change Notification Level"
choose_new_category: "Choose the new category for the topics:"
- selected_sole_category:
- one: "You have selected <b>%{count}</b> topic from category:"
- other: "You have selected <b>%{count}</b> topics from category:"
selected_count:
one: "%{count} selected"
other: "%{count} selected"
- change_tags: "Replace tags"
- append_tags: "Add tags"
- choose_new_tags: "Choose new tags for these topics:"
- choose_append_tags: "Add tags"
- changed_tags: "The tags of those topics were changed."
- remove_tags: "Remove tags"
- confirm_remove_tags:
- one: "All tags will be removed from this topic. Are you sure?"
- other: "All tags will be removed from %{count} topics. Are you sure?"
+ manage_tags: "Manage tags"
confirm_unpin_topics:
one: "Un-pin %{count} topic"
other: "Un-pin %{count} topics"
confirm_update_topics:
one: "Update %{count} topic"
other: "Update %{count} topics"
+ confirm_apply_to_topics:
+ one: "Apply to %{count} topic"
+ other: "Apply to %{count} topics"
confirm_close_topics:
one: "Close %{count} topic"
other: "Close %{count} topics"
@@ -3891,15 +3883,25 @@ en:
description: "Unlisted topics are hidden from the topic list but can still be accessed via a direct link. <a href='https://meta.discourse.org/t/understanding-closed-unlisted-and-archived-topics/51238' target='_blank'>Learn more</a>"
relist_topics:
name: "Relist"
- remove_tags:
- name: "Remove Tags"
- description: "Remove all tags from the selected topics."
- append_tags:
- name: "Add Tags"
- description: "Add the following tags to the selected topics:"
- replace_tags:
- name: "Replace Tags"
- description: "Replace all existing tags on the selected topics with the following:"
+ manage_tags:
+ name: "Manage tags"
+ add:
+ title: "Add tags"
+ description: "Added to all selected topics"
+ remove:
+ title: "Remove tags"
+ description: "Removed whenever present"
+ all_toggle: "Remove all tags"
+ all_warning: "<strong>All existing tags</strong> will be removed from the selected topics. This cannot be undone."
+ replace:
+ title: "Replace tags"
+ description: "Swaps one tag for another"
+ add_replacement: "Add replacement"
+ remove_replacement: "Remove replacement"
+ from_placeholder: "Tag to replace"
+ to_placeholder: "Replacement tag"
+ missing_from: "A source tag is required"
+ missing_to: "A replacement tag is required"
delete_topics:
name: "Delete"
description: "Permanently delete the selected topics. This action cannot be undone."
diff --git a/frontend/discourse/app/components/bulk-actions/append-tags.gjs b/frontend/discourse/app/components/bulk-actions/append-tags.gjs
deleted file mode 100644
index b46c0db0330..00000000000
--- a/frontend/discourse/app/components/bulk-actions/append-tags.gjs
+++ /dev/null
@@ -1,24 +0,0 @@
-import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { fn, hash } from "@ember/helper";
-import DButton from "discourse/components/d-button";
-import TagChooser from "discourse/select-kit/components/tag-chooser";
-import { not } from "discourse/truth-helpers";
-import { i18n } from "discourse-i18n";
-
-export default class AppendTags extends Component {
- @tracked tags = [];
-
- <template>
- <p>{{i18n "topics.bulk.choose_append_tags"}}</p>
-
- <p><TagChooser @tags={{this.tags}} @categoryId={{@categoryId}} /></p>
-
- <DButton
- @action={{fn @performAndRefresh (hash type="append_tags" tags=this.tags)}}
- @disabled={{not this.tags}}
- @label="topics.bulk.append_tags"
- class="topic-bulk-actions__append-tags"
- />
- </template>
-}
diff --git a/frontend/discourse/app/components/bulk-actions/change-tags.gjs b/frontend/discourse/app/components/bulk-actions/change-tags.gjs
deleted file mode 100644
index d7d3774741c..00000000000
--- a/frontend/discourse/app/components/bulk-actions/change-tags.gjs
+++ /dev/null
@@ -1,23 +0,0 @@
-import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { fn, hash } from "@ember/helper";
-import DButton from "discourse/components/d-button";
-import TagChooser from "discourse/select-kit/components/tag-chooser";
-import { not } from "discourse/truth-helpers";
-import { i18n } from "discourse-i18n";
-
-export default class ChangeTags extends Component {
- @tracked tags = [];
-
- <template>
- <p>{{i18n "topics.bulk.choose_new_tags"}}</p>
-
- <p><TagChooser @tags={{this.tags}} @categoryId={{@categoryId}} /></p>
-
- <DButton
- @action={{fn @performAndRefresh (hash type="change_tags" tags=this.tags)}}
- @disabled={{not this.tags}}
- @label="topics.bulk.change_tags"
- />
- </template>
-}
diff --git a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
index 7a51c4bc528..41369c3fe26 100644
--- a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
+++ b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
@@ -92,6 +92,13 @@ export default class BulkSelectTopicsDropdown extends Component {
icon: "topic.closed",
name: i18n("topic_bulk_actions.close_topics.name"),
},
+ {
+ id: "manage-tags",
+ icon: "tag",
+ name: i18n("topic_bulk_actions.manage_tags.name"),
+ visible: ({ currentUser, siteSettings }) =>
+ siteSettings.tagging_enabled && currentUser.canManageTopic,
+ },
{
id: "pin-topics",
icon: "thumbtack",
@@ -154,27 +161,6 @@ export default class BulkSelectTopicsDropdown extends Component {
topics.some((t) => !t.visible) &&
!topics.some((t) => t.isPrivateMessage),
},
- {
- id: "append-tags",
- icon: "tag",
- name: i18n("topic_bulk_actions.append_tags.name"),
- visible: ({ currentUser, siteSettings }) =>
- siteSettings.tagging_enabled && currentUser.canManageTopic,
- },
- {
- id: "replace-tags",
- icon: "tag",
- name: i18n("topic_bulk_actions.replace_tags.name"),
- visible: ({ currentUser, siteSettings }) =>
- siteSettings.tagging_enabled && currentUser.canManageTopic,
- },
- {
- id: "remove-tags",
- icon: "tag",
- name: i18n("topic_bulk_actions.remove_tags.name"),
- visible: ({ currentUser, siteSettings }) =>
- siteSettings.tagging_enabled && currentUser.canManageTopic,
- },
{
id: "delete-topics",
icon: "trash-can",
@@ -330,22 +316,9 @@ export default class BulkSelectTopicsDropdown extends Component {
confirmButtonTranslationKey: "topics.bulk.confirm_relist_topics",
});
break;
- case "append-tags":
- this.showBulkTopicActionsModal(actionId, "append_tags", {
- description: i18n(`topic_bulk_actions.append_tags.description`),
- confirmButtonTranslationKey: "topics.bulk.confirm_update_topics",
- });
- break;
- case "replace-tags":
- this.showBulkTopicActionsModal(actionId, "change_tags", {
- description: i18n(`topic_bulk_actions.replace_tags.description`),
- confirmButtonTranslationKey: "topics.bulk.confirm_update_topics",
- });
- break;
- case "remove-tags":
- this.showBulkTopicActionsModal(actionId, "remove_tags", {
- description: i18n(`topic_bulk_actions.remove_tags.description`),
- confirmButtonTranslationKey: "topics.bulk.confirm_update_topics",
+ case "manage-tags":
+ this.showBulkTopicActionsModal(actionId, "manage_tags", {
+ confirmButtonTranslationKey: "topics.bulk.confirm_apply_to_topics",
});
break;
case "delete-topics":
diff --git a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
index 99e9e2abbc7..bf735adc7e9 100644
--- a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
+++ b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
@@ -10,15 +10,14 @@ import { Promise } from "rsvp";
import ConditionalLoadingSection from "discourse/components/conditional-loading-section";
import DButton from "discourse/components/d-button";
import DModal from "discourse/components/d-modal";
+import ManageTagsForm from "discourse/components/modal/bulk-topic-actions/manage-tags-form";
import BulkPinOptions from "discourse/components/modal/feature-topic/bulk-pin-options";
import RadioButton from "discourse/components/radio-button";
-import { categoryBadgeHTML } from "discourse/helpers/category-link";
import { topicLevels } from "discourse/lib/notification-levels";
import Category from "discourse/models/category";
import Topic from "discourse/models/topic";
import autoFocus from "discourse/modifiers/auto-focus";
import CategoryChooser from "discourse/select-kit/components/category-chooser";
-import TagChooser from "discourse/select-kit/components/tag-chooser";
import { i18n } from "discourse-i18n";
const _customActions = {};
@@ -32,7 +31,6 @@ export default class BulkTopicActions extends Component {
@tracked activeComponent = null;
@tracked activeComponentProps = null;
- @tracked tags = [];
@tracked categoryId;
@tracked loading;
@tracked errors;
@@ -43,11 +41,17 @@ export default class BulkTopicActions extends Component {
@tracked skippedTopicCount = 0;
@tracked notificationLevelId = null;
+ @tracked customSubmitDisabled = false;
constructor() {
super(...arguments);
- if (this.model.initialAction === "set-component") {
+ if (this.model.action === "manage-tags") {
+ this.setComponent(ManageTagsForm, {
+ categoryId: this.soleCategoryId,
+ onPerform: this.performAndRefresh,
+ });
+ } else if (this.model.initialAction === "set-component") {
if (this.model.initialActionLabel in _customActions) {
_customActions[this.model.initialActionLabel]({
setComponent: this.setComponent.bind(this),
@@ -108,6 +112,10 @@ export default class BulkTopicActions extends Component {
operation["message"] = this.closeNote;
}
+ if (operation.type === "manage_tags") {
+ options.asJSON = true;
+ }
+
const tasks = topicChunks.map((topics) => async () => {
const result = await Topic.bulkOperation(topics, operation, options);
this.processedTopicCount += topics.length;
@@ -159,7 +167,6 @@ export default class BulkTopicActions extends Component {
@action
performAction(opts = {}) {
- this.loading = true;
switch (this.model.action) {
case "close":
this.forEachPerformed({ type: "close" }, (t) => t.set("closed", true));
@@ -207,21 +214,6 @@ export default class BulkTopicActions extends Component {
case "pin":
this.performAndRefresh({ type: "pin", ...opts });
break;
- case "append-tags":
- this.performAndRefresh({
- type: "append_tags",
- tag_ids: this.tags?.map((t) => t.id),
- });
- break;
- case "replace-tags":
- this.performAndRefresh({
- type: "change_tags",
- tag_ids: this.tags?.map((t) => t.id),
- });
- break;
- case "remove-tags":
- this.performAndRefresh({ type: "remove_tags" });
- break;
case "delete":
this.performAndRefresh({ type: "delete" });
break;
@@ -304,6 +296,7 @@ export default class BulkTopicActions extends Component {
@action
async forEachPerformed(operation, cb) {
+ this.loading = true;
const totalCount = this.model.bulkSelectHelper.selected.length;
const result = await this.perform(operation);
@@ -324,6 +317,7 @@ export default class BulkTopicActions extends Component {
@action
async performAndRefresh(operation) {
+ this.loading = true;
const totalCount = this.model.bulkSelectHelper.selected.length;
const result = await this.perform(operation);
@@ -341,13 +335,6 @@ export default class BulkTopicActions extends Component {
}
}
- get isTagAction() {
- return (
- this.model.action === "append-tags" ||
- this.model.action === "replace-tags"
- );
- }
-
get isNotificationAction() {
return this.model.action === "update-notifications";
}
@@ -401,16 +388,6 @@ export default class BulkTopicActions extends Component {
return Category.findById(this.soleCategoryId);
}
- get soleCategoryBadgeHTML() {
- return categoryBadgeHTML(this.soleCategory, {
- allowUncategorized: true,
- });
- }
-
- get showSoleCategoryTip() {
- return this.soleCategory && this.isTagAction;
- }
-
get confirmButtonLabel() {
if (this.model.confirmButtonTranslationKey) {
return i18n(this.model.confirmButtonTranslationKey, {
@@ -425,7 +402,12 @@ export default class BulkTopicActions extends Component {
return !this.notificationLevelId || this.loading;
}
- return this.loading;
+ return this.customSubmitDisabled || this.loading;
+ }
+
+ @action
+ setSubmitDisabled(value) {
+ this.customSubmitDisabled = value;
}
@action
@@ -437,7 +419,7 @@ export default class BulkTopicActions extends Component {
<DModal
@title={{@model.title}}
@closeModal={{@closeModal}}
- class="topic-bulk-actions-modal -large"
+ class="topic-bulk-actions-modal"
>
<:body>
<ConditionalLoadingSection
@@ -480,18 +462,6 @@ export default class BulkTopicActions extends Component {
}}</p>
{{/if}}
- {{#if this.showSoleCategoryTip}}
- <div class="topic-bulk-actions-modal__selection-info">
- {{trustHTML
- (i18n
- "topics.bulk.selected_sole_category"
- )
- }}
- {{trustHTML this.soleCategoryBadgeHTML}}
- </div>
- {{/if}}
-
{{#if this.isCategoryAction}}
<p>
<CategoryChooser
@@ -523,19 +493,15 @@ export default class BulkTopicActions extends Component {
</div>
{{/if}}
- {{#if this.isTagAction}}
- <p><TagChooser
- @tags={{this.tags}}
- @categoryId={{this.soleCategoryId}}
- /></p>
- {{/if}}
-
{{#if this.activeComponent}}
{{component
this.activeComponent
onRegisterAction=this.registerCustomAction
+ setSubmitDisabled=this.setSubmitDisabled
topics=this.activeComponentProps.topics
afterBulkAction=this.activeComponentProps.afterBulkAction
+ categoryId=this.activeComponentProps.categoryId
+ onPerform=this.activeComponentProps.onPerform
}}
{{/if}}
diff --git a/frontend/discourse/app/components/modal/bulk-topic-actions/manage-tags-form.gjs b/frontend/discourse/app/components/modal/bulk-topic-actions/manage-tags-form.gjs
new file mode 100644
index 00000000000..4ea0784c7fd
--- /dev/null
+++ b/frontend/discourse/app/components/modal/bulk-topic-actions/manage-tags-form.gjs
@@ -0,0 +1,290 @@
+import Component from "@glimmer/component";
+import { array, fn, hash } from "@ember/helper";
+import { action } from "@ember/object";
+import { trustHTML } from "@ember/template";
+import Form from "discourse/components/form";
+import icon from "discourse/helpers/d-icon";
+import { i18n } from "discourse-i18n";
+
+export default class ManageTagsForm extends Component {
+ formApi;
+
+ initialData = {
+ remove_all_tags: false,
+ remove_tags: [],
+ add_tags: [],
+ replace_rows: [{ from: [], to: [] }],
+ };
+
+ constructor() {
+ super(...arguments);
+ this.args.onRegisterAction?.(this.triggerSubmit);
+ this.args.setSubmitDisabled?.(true);
+ }
+
+ @action
+ registerApi(api) {
+ this.formApi = api;
+ }
+
+ @action
+ triggerSubmit() {
+ this.formApi?.submit();
+ }
+
+ @action
+ afterFieldSet(value, { set, name }) {
+ set(name, value);
+ this.args.setSubmitDisabled?.(this.#isEmpty());
+ }
+
+ @action
+ blockedTagsFor(rows, index, side) {
+ const tags = rows?.[index]?.[side];
+ return tags?.length ? tags : undefined;
+ }
+
+ @action
+ removeReplaceRow(collection, index) {
+ collection.remove(index);
+ // `collection.remove` doesn't re-key row errors, so drop them all.
+ this.formApi.removeErrors();
+ this.args.setSubmitDisabled?.(this.#isEmpty());
+ }
+
+ #isEmpty() {
+ const removeAll = this.formApi.get("remove_all_tags") ?? false;
+ const removeTags = this.formApi.get("remove_tags") ?? [];
+ const addTags = this.formApi.get("add_tags") ?? [];
+ const rows = this.formApi.get("replace_rows") ?? [];
+
+ return (
+ !removeAll &&
+ addTags.length === 0 &&
+ removeTags.length === 0 &&
+ rows.every((row) => this.#replaceRowStatus(row) === "empty")
+ );
+ }
+
+ @action
+ validateReplaceRows(data, { addError, removeError }) {
+ const title = i18n("topic_bulk_actions.manage_tags.replace.title");
+
+ data.replace_rows.forEach((row, index) => {
+ const fromName = `replace_rows.${index}.from`;
+ const toName = `replace_rows.${index}.to`;
+ removeError(fromName);
+ removeError(toName);
+
+ const status = this.#replaceRowStatus(row);
+ if (status === "valid" || status === "empty") {
+ return;
+ }
+
+ const message = this.#messageForStatus(status);
+
+ if (status === "missing-from") {
+ addError(fromName, { title, message });
+ } else {
+ addError(toName, { title, message });
+ }
+ });
+ }
+
+ @action
+ onSubmit(data) {
+ this.args.onPerform?.({ type: "manage_tags", ...this.#buildPayload(data) });
+ }
+
+ #replaceRowStatus(row) {
+ const from = row?.from?.[0];
+ const to = row?.to?.[0];
+
+ if (!from && !to) {
+ return "empty";
+ }
+
+ if (to && !from) {
+ return "missing-from";
+ }
+
+ if (from && !to) {
+ return "missing-to";
+ }
+
+ return "valid";
+ }
+
+ #messageForStatus(status) {
+ switch (status) {
+ case "missing-from":
+ return i18n("topic_bulk_actions.manage_tags.replace.missing_from");
+ case "missing-to":
+ return i18n("topic_bulk_actions.manage_tags.replace.missing_to");
+ default:
+ return null;
+ }
+ }
+
+ #buildPayload(data) {
+ const removeAll = data.remove_all_tags;
+ return {
+ add_tag_ids: data.add_tags.map((tag) => tag.id),
+ remove_tag_ids: removeAll ? [] : data.remove_tags.map((tag) => tag.id),
+ remove_all_tags: removeAll,
+ replace_tags: data.replace_rows
+ .filter((row) => this.#replaceRowStatus(row) === "valid")
+ .map(({ from, to }) => ({
+ from_tag_id: from[0].id,
+ to_tag_id: to[0].id,
+ })),
+ };
+ }
+
+ <template>
+ <Form
+ @data={{this.initialData}}
+ @onSubmit={{this.onSubmit}}
+ @onRegisterApi={{this.registerApi}}
+ @validate={{this.validateReplaceRows}}
+ class="manage-tags-form"
+ as |form transientData|
+ >
+
+ <form.Container @format="full" class="manage-tags-form__remove-section">
+ {{#if transientData.remove_all_tags}}
+ <form.Alert @type="error" class="manage-tags-form__warning">
+ {{trustHTML
+ (i18n "topic_bulk_actions.manage_tags.remove.all_warning")
+ }}
+ </form.Alert>
+ {{else}}
+ <form.Field
+ @name="remove_tags"
+ @title={{i18n "topic_bulk_actions.manage_tags.remove.title"}}
+ @description={{i18n
+ "topic_bulk_actions.manage_tags.remove.description"
+ }}
+ @type="tag-chooser"
+ @showOptional={{false}}
+ @format="full"
+ @onSet={{this.afterFieldSet}}
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ {{/if}}
+
+ <form.Field
+ @name="remove_all_tags"
+ @title={{i18n "topic_bulk_actions.manage_tags.remove.all_toggle"}}
+ @type="toggle"
+ @showOptional={{false}}
+ @onSet={{this.afterFieldSet}}
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ </form.Container>
+
+ <form.Field
+ @name="add_tags"
+ @title={{i18n "topic_bulk_actions.manage_tags.add.title"}}
+ @description={{i18n "topic_bulk_actions.manage_tags.add.description"}}
+ @type="tag-chooser"
+ @showOptional={{false}}
+ @format="full"
+ @onSet={{this.afterFieldSet}}
+ as |field|
+ >
+ <field.Control @categoryId={{@categoryId}} />
+ </form.Field>
+
+ <form.Container
+ @title={{i18n "topic_bulk_actions.manage_tags.replace.title"}}
+ @subtitle={{i18n "topic_bulk_actions.manage_tags.replace.description"}}
+ @format="full"
+ class="manage-tags-form__replace"
+ >
+ <form.Collection @name="replace_rows" as |collection index|>
+ <form.Row as |row|>
+ <row.Col @size={{5}}>
+ <collection.Field
+ @name="from"
+ @title={{i18n
+ "topic_bulk_actions.manage_tags.replace.from_placeholder"
+ }}
+ @showTitle={{false}}
+ @type="tag-chooser"
+ @format="full"
+ @onSet={{this.afterFieldSet}}
+ as |field|
+ >
+ <field.Control
+ @maximum={{1}}
+ @placeholder="topic_bulk_actions.manage_tags.replace.from_placeholder"
+ @blockedTags={{this.blockedTagsFor
+ transientData.replace_rows
+ index
+ "to"
+ }}
+ />
+ </collection.Field>
+ </row.Col>
+
+ <row.Col @size={{1}} class="manage-tags-form__replace-arrow">
+ {{icon "arrow-right"}}
+ </row.Col>
+
+ <row.Col @size={{5}}>
+ <collection.Field
+ @name="to"
+ @title={{i18n
+ "topic_bulk_actions.manage_tags.replace.to_placeholder"
+ }}
+ @showTitle={{false}}
+ @type="tag-chooser"
+ @format="full"
+ @onSet={{this.afterFieldSet}}
+ as |field|
+ >
+ <field.Control
+ @categoryId={{@categoryId}}
+ @maximum={{1}}
+ @placeholder="topic_bulk_actions.manage_tags.replace.to_placeholder"
+ @blockedTags={{this.blockedTagsFor
+ transientData.replace_rows
+ index
+ "from"
+ }}
+ />
+ </collection.Field>
+ </row.Col>
+
+ <row.Col @size={{1}}>
+ <form.Button
+ @icon="xmark"
+ @action={{fn this.removeReplaceRow collection index}}
+ @title="topic_bulk_actions.manage_tags.replace.remove_replacement"
+ class="manage-tags-form__replace-row-remove"
+ />
+ </row.Col>
+ </form.Row>
+ </form.Collection>
+
+ <form.Button
+ @icon="plus"
+ @translatedLabel={{i18n
+ "topic_bulk_actions.manage_tags.replace.add_replacement"
+ }}
+ @action={{fn
+ form.addItemToCollection
+ "replace_rows"
+ (hash from=(array) to=(array))
+ }}
+ class="btn-default manage-tags-form__replace-row-add"
+ />
+ </form.Container>
+ </Form>
+ </template>
+}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs b/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs
index c9315cc8dce..a337d9c5f71 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/tag-chooser.gjs
@@ -21,7 +21,12 @@ export default class FKControlTagChooser extends FKBaseControl {
@unlimitedTagCount={{@unlimited}}
@categoryId={{@categoryId}}
@allowCreate={{@allowCreate}}
- @options={{hash [email protected] filterPlaceholder=@placeholder}}
+ @blockedTags={{@blockedTags}}
+ @options={{hash
+ filterPlaceholder=@placeholder
+ maximum=@maximum
+ }}
class="form-kit__control-tag-chooser"
/>
</template>
diff --git a/frontend/discourse/app/form-kit/components/fk/field-data.gjs b/frontend/discourse/app/form-kit/components/fk/field-data.gjs
index 5ab9af03db8..75f873ff289 100644
--- a/frontend/discourse/app/form-kit/components/fk/field-data.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/field-data.gjs
@@ -189,6 +189,16 @@ export default class FKFieldData extends Component {
return this.args.showTitle ?? true;
}
+ /**
+ * Indicates whether to show the `(optional)` marker next to the title when
+ * the field has no `required` validation rule.
+ * Defaults to `true`.
+ * @type {boolean}
+ */
+ get showOptional() {
+ return this.args.showOptional ?? true;
+ }
+
/**
* Function to add errors to the field.
* @type {Function}
diff --git a/frontend/discourse/app/form-kit/components/fk/field.gjs b/frontend/discourse/app/form-kit/components/fk/field.gjs
index b7d05f46fbf..c5968c0cef6 100644
--- a/frontend/discourse/app/form-kit/components/fk/field.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/field.gjs
@@ -150,7 +150,9 @@ export default class FKField extends Component {
>
<span>{{field.title}}</span>
- <FKOptional @field={{field}} />
+ {{#if field.showOptional}}
+ <FKOptional @field={{field}} />
+ {{/if}}
<FKTooltip @field={{field}} />
</FKLabel>
{{/if}}
diff --git a/frontend/discourse/app/form-kit/components/fk/section.gjs b/frontend/discourse/app/form-kit/components/fk/section.gjs
index f8ca90321fe..e20913f8bf0 100644
--- a/frontend/discourse/app/form-kit/components/fk/section.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/section.gjs
@@ -4,15 +4,21 @@ import { or } from "discourse/truth-helpers/index";
const FKSection = <template>
<div class={{concatClass "form-kit__section" @class}} ...attributes>
- {{#if (or @title @subtitle)}}
+ {{#if (or @title @subtitle (has-block "header"))}}
<div class="form-kit__section-header">
- {{#if @title}}
- <h2 class="form-kit__section-title">{{@title}}</h2>
- {{/if}}
+ {{#if (or @title @subtitle)}}
+ <div class="form-kit__section-heading">
+ {{#if @title}}
+ <h2 class="form-kit__section-title">{{@title}}</h2>
+ {{/if}}
- {{#if @subtitle}}
- <span class="form-kit__section-subtitle">{{@subtitle}}</span>
+ {{#if @subtitle}}
+ <span class="form-kit__section-subtitle">{{@subtitle}}</span>
+ {{/if}}
+ </div>
{{/if}}
+
+ {{yield to="header"}}
</div>
{{/if}}
diff --git a/frontend/discourse/app/models/topic.js b/frontend/discourse/app/models/topic.js
index 587e7b31933..23cc75ba1b9 100644
--- a/frontend/discourse/app/models/topic.js
+++ b/frontend/discourse/app/models/topic.js
@@ -210,10 +210,17 @@ export default class Topic extends RestModel {
}
}
- return ajax("/topics/bulk", {
+ const request = {
type: "PUT",
data,
- });
+ };
+
+ if (options?.asJSON) {
+ request.contentType = "application/json";
+ request.data = JSON.stringify(data);
+ }
+
+ return ajax("/topics/bulk", request);
}
static bulkOperationByFilter(filter, operation, options, isTracked) {
diff --git a/frontend/discourse/select-kit/components/tag-chooser.js b/frontend/discourse/select-kit/components/tag-chooser.js
index 9249c572fab..e5e59a0d056 100644
--- a/frontend/discourse/select-kit/components/tag-chooser.js
+++ b/frontend/discourse/select-kit/components/tag-chooser.js
@@ -128,8 +128,13 @@ export default class TagChooser extends MultiSelectComponent {
return this.tagUtils.createContentFromInput(input);
}
+ get _normalizedBlockedTags() {
+ return makeArray(this.blockedTags).filter(Boolean);
+ }
+
search(query) {
const selectedTags = makeArray(this.tags).filter(Boolean);
+ const blockedTags = this._normalizedBlockedTags;
const data = {
q: query,
@@ -137,9 +142,9 @@ export default class TagChooser extends MultiSelectComponent {
categoryId: this.categoryId,
};
- if (selectedTags.length || this.blockedTags.length) {
+ if (selectedTags.length || blockedTags.length) {
const allTags = uniqueItemsFromArray(
- selectedTags.concat(this.blockedTags)
+ selectedTags.concat(blockedTags)
).slice(0, 100);
// Extract IDs for objects, keep strings for legacy/blocked tags
@@ -186,9 +191,10 @@ export default class TagChooser extends MultiSelectComponent {
termMatchErrorMessage: json.forbidden_message,
});
- if (this.blockedTags) {
+ const blockedTags = this._normalizedBlockedTags;
+ if (blockedTags.length) {
// extract names from blockedTags (may be strings or objects)
- const blockedNames = this.blockedTags.map((t) =>
+ const blockedNames = blockedTags.map((t) =>
typeof t === "string" ? t : t.name
);
results = results.filter((result) => {
diff --git a/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs b/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
index e3392ab1cde..16460be4722 100644
--- a/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
+++ b/frontend/discourse/tests/integration/components/bulk-select-topics-dropdown-test.gjs
@@ -58,15 +58,13 @@ module("Integration | Component | BulkSelectTopicsDropdown", function (hooks) {
await click(".bulk-select-topics-dropdown-trigger");
assert
.dom(".fk-d-menu__inner-content .dropdown-menu__item")
- .exists({ count: 7 });
+ .exists({ count: 5 });
[
"update-notifications",
"reset-bump-dates",
"close-topics",
- "append-tags",
- "replace-tags",
- "remove-tags",
+ "manage-tags",
"delete-topics",
].forEach((action) => {
assert
@@ -140,11 +138,9 @@ module("Integration | Component | BulkSelectTopicsDropdown", function (hooks) {
);
await click(".bulk-select-topics-dropdown-trigger");
- ["append-tags", "replace-tags", "remove-tags"].forEach((action) => {
- assert
- .dom(`.fk-d-menu__inner-content .dropdown-menu__item .${action}`)
- .doesNotExist();
- });
+ assert
+ .dom(".fk-d-menu__inner-content .dropdown-menu__item .manage-tags")
+ .doesNotExist();
});
test("does not allow tagging actions if user cannot manage topic", async function (assert) {
@@ -157,11 +153,9 @@ module("Integration | Component | BulkSelectTopicsDropdown", function (hooks) {
);
await click(".bulk-select-topics-dropdown-trigger");
- ["append-tags", "replace-tags", "remove-tags"].forEach((action) => {
- assert
- .dom(`.fk-d-menu__inner-content .dropdown-menu__item .${action}`)
- .doesNotExist();
- });
+ assert
+ .dom(".fk-d-menu__inner-content .dropdown-menu__item .manage-tags")
+ .doesNotExist();
});
test("does not allow deleting topics if user is not staff", async function (assert) {
diff --git a/frontend/discourse/tests/integration/components/form-kit/field-test.gjs b/frontend/discourse/tests/integration/components/form-kit/field-test.gjs
index 99b301f8121..7b55e355b31 100644
--- a/frontend/discourse/tests/integration/components/form-kit/field-test.gjs
+++ b/frontend/discourse/tests/integration/components/form-kit/field-test.gjs
@@ -250,6 +250,39 @@ module("Integration | Component | FormKit | Field", function (hooks) {
assert.dom(".form-kit__container-title").doesNotExist();
});
+ test("@showOptional", async function (assert) {
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field
+ @type="input"
+ @name="foo"
+ @title="Foo"
+ as |field|
+ ><field.Control /></form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.dom(".form-kit__container-optional").exists();
+
+ await render(
+ <template>
+ <Form as |form|>
+ <form.Field
+ @type="input"
+ @name="foo"
+ @title="Foo"
+ @showOptional={{false}}
+ as |field|
+ ><field.Control /></form.Field>
+ </Form>
+ </template>
+ );
+
+ assert.dom(".form-kit__container-optional").doesNotExist();
+ });
+
test("@format", async function (assert) {
await render(
<template>
diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb
index acb49b2526a..307ebbb41f9 100644
--- a/lib/topics_bulk_action.rb
+++ b/lib/topics_bulk_action.rb
@@ -27,6 +27,7 @@ class TopicsBulkAction
change_tags
append_tags
remove_tags
+ manage_tags
relist
dismiss_topics
reset_bump_dates
@@ -301,47 +302,58 @@ class TopicsBulkAction
def change_tags
tags = resolve_tag_names || []
-
- topics_with_tags.each do |t|
- next unless guardian.can_edit?(t)
- next unless t.first_post
-
- if t.first_post.revise(@user, { tags: tags }, bulk_tag_opts)
- @changed_ids << t.id
- else
- t.errors.full_messages.each { |msg| @errors[msg] += 1 }
- end
- end
+ topics_with_tags.each { |t| apply_tag_revision(t, tags) }
end
def append_tags
tags = resolve_tag_names || []
-
return if tags.blank?
topics_with_tags.each do |t|
- next unless guardian.can_edit?(t)
- next unless t.first_post
-
merged = t.tags.map(&:name) | tags
- if t.first_post.revise(@user, { tags: merged }, bulk_tag_opts)
- @changed_ids << t.id
- else
- t.errors.full_messages.each { |msg| @errors[msg] += 1 }
- end
+ apply_tag_revision(t, merged)
end
end
def remove_tags
- topics_with_tags.each do |t|
- next unless guardian.can_edit?(t)
- next unless t.first_post
+ topics_with_tags.each { |t| apply_tag_revision(t, []) }
+ end
- if t.first_post.revise(@user, { tags: [] }, bulk_tag_opts)
- @changed_ids << t.id
- else
- t.errors.full_messages.each { |msg| @errors[msg] += 1 }
+ def manage_tags
+ add_ids = (@operation[:add_tag_ids] || []).map(&:to_i)
+ remove_ids = (@operation[:remove_tag_ids] || []).map(&:to_i)
+ replace_pairs =
+ (@operation[:replace_tags] || []).map { |e| [e[:from_tag_id].to_i, e[:to_tag_id].to_i] }
+
+ valid_ids = Tag.where(id: (add_ids + remove_ids + replace_pairs).flatten).pluck(:id)
+ add_ids &= valid_ids
+ remove_ids &= valid_ids
+ replace_map =
+ replace_pairs.each_with_object({}) do |(from_id, to_id), map|
+ map[from_id] ||= to_id if valid_ids.include?(from_id) && valid_ids.include?(to_id)
end
+
+ topics_with_tags.each do |t|
+ current = t.tags.map(&:id)
+ base = @operation[:remove_all_tags] ? [] : current - remove_ids
+ final = (base | add_ids).map { |id| replace_map[id] || id }.uniq
+
+ next if final.sort == current.sort
+
+ apply_tag_revision(t, Tag.where(id: final).pluck(:name))
+ end
+ end
+
+ def apply_tag_revision(topic, tag_names)
+ return false unless guardian.can_edit?(topic)
+ return false unless topic.first_post
+
+ if topic.first_post.revise(@user, { tags: tag_names }, bulk_tag_opts)
+ @changed_ids << topic.id
+ true
+ else
+ topic.errors.full_messages.each { |msg| @errors[msg] += 1 }
+ false
end
end
diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb
index 048c7827585..92debabf654 100644
--- a/spec/lib/topics_bulk_action_spec.rb
+++ b/spec/lib/topics_bulk_action_spec.rb
@@ -906,4 +906,172 @@ RSpec.describe TopicsBulkAction do
expect(operator.errors).to be_present
end
end
+
+ describe "#manage_tags" do
+ fab!(:tag_1, :tag)
+ fab!(:tag_2, :tag)
+ fab!(:tag_3, :tag)
+ fab!(:tag_4, :tag)
+ fab!(:topic_1) { Fabricate(:topic_with_op, user: user, tags: [tag_1, tag_2]) }
+ fab!(:topic_2) { Fabricate(:topic_with_op, user: user, tags: [tag_2, tag_3]) }
+ fab!(:topic_3) { Fabricate(:topic_with_op, user: user, tags: [tag_1]) }
+
+ before do
+ SiteSetting.tagging_enabled = true
+ SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
+ end
+
+ it "adds tags to each topic on top of its existing tags" do
+ topic_ids =
+ TopicsBulkAction.new(
+ user,
+ [topic_1.id, topic_2.id, topic_3.id],
+ type: "manage_tags",
+ add_tag_ids: [tag_3.id, tag_4.id],
+ ).perform!
+
+ expect(topic_ids).to contain_exactly(topic_1.id, topic_2.id, topic_3.id)
+ expect(topic_1.reload.tags).to contain_exactly(tag_1, tag_2, tag_3, tag_4)
+ expect(topic_2.reload.tags).to contain_exactly(tag_2, tag_3, tag_4)
+ expect(topic_3.reload.tags).to contain_exactly(tag_1, tag_3, tag_4)
+ end
+
+ it "removes specific tags only from topics that have them" do
+ topic_ids =
+ TopicsBulkAction.new(
+ user,
+ [topic_1.id, topic_2.id, topic_3.id],
+ type: "manage_tags",
+ remove_tag_ids: [tag_1.id, tag_2.id],
+ ).perform!
+
+ expect(topic_ids).to contain_exactly(topic_1.id, topic_2.id, topic_3.id)
+ expect(topic_1.reload.tags).to be_empty
+ expect(topic_2.reload.tags).to contain_exactly(tag_3)
+ expect(topic_3.reload.tags).to be_empty
+ end
+
+ it "replaces tags on topics that have the source tag" do
+ topic_ids =
+ TopicsBulkAction.new(
+ user,
+ [topic_1.id, topic_2.id, topic_3.id],
+ type: "manage_tags",
+ replace_tags: [
+ { from_tag_id: tag_2.id, to_tag_id: tag_3.id },
+ { from_tag_id: tag_3.id, to_tag_id: tag_4.id },
+ ],
+ ).perform!
+
+ expect(topic_ids).to contain_exactly(topic_1.id, topic_2.id)
+ expect(topic_1.reload.tags).to contain_exactly(tag_1, tag_3)
+ expect(topic_2.reload.tags).to contain_exactly(tag_3, tag_4)
+ expect(topic_3.reload.tags).to contain_exactly(tag_1)
+ end
+
+ it "applies remove, then add, then replace in that order" do
+ TopicsBulkAction.new(
+ user,
+ [topic_1.id, topic_2.id, topic_3.id],
+ type: "manage_tags",
+ remove_tag_ids: [tag_1.id, tag_2.id],
+ add_tag_ids: [tag_1.id, tag_3.id],
+ replace_tags: [
+ { from_tag_id: tag_1.id, to_tag_id: tag_4.id },
+ { from_tag_id: tag_3.id, to_tag_id: tag_2.id },
+ ],
+ ).perform!
+
+ expect(topic_1.reload.tags).to contain_exactly(tag_2, tag_4)
+ expect(topic_2.reload.tags).to contain_exactly(tag_2, tag_4)
+ expect(topic_3.reload.tags).to contain_exactly(tag_2, tag_4)
+ end
+
+ it "can clear all tags with remove_all_tags" do
+ topic_ids =
+ TopicsBulkAction.new(
+ user,
+ [topic_1.id, topic_2.id, topic_3.id],
+ type: "manage_tags",
+ remove_all_tags: true,
+ ).perform!
+
+ expect(topic_ids).to contain_exactly(topic_1.id, topic_2.id, topic_3.id)
+ expect(topic_1.reload.tags).to be_empty
+ expect(topic_2.reload.tags).to be_empty
+ expect(topic_3.reload.tags).to be_empty
+ end
+
+ it "applies replacements sequentially so the first matching pair wins" do
+ TopicsBulkAction.new(
+ user,
+ [topic_1.id, topic_2.id, topic_3.id],
+ type: "manage_tags",
+ replace_tags: [
+ { from_tag_id: tag_1.id, to_tag_id: tag_2.id },
+ { from_tag_id: tag_1.id, to_tag_id: tag_3.id },
+ ],
+ ).perform!
+
+ expect(topic_1.reload.tags).to contain_exactly(tag_2)
+ expect(topic_2.reload.tags).to contain_exactly(tag_2, tag_3)
+ expect(topic_3.reload.tags).to contain_exactly(tag_2)
+ end
+
+ it "does not bump, revise, or notify watchers" do
+ topic_watcher = Fabricate(:user)
+ Jobs.run_immediately!
+ TopicUser.change(
+ topic_watcher,
+ topic_1.id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+ topic_1.update!(bumped_at: 1.week.ago)
+
+ notifications_before = Notification.where(user: topic_watcher).count
+ bumped_at_before = topic_1.bumped_at
+ version_before = topic_1.first_post.version
+
+ TopicsBulkAction.new(
+ Fabricate(:admin),
+ [topic_1.id],
+ type: "manage_tags",
+ add_tag_ids: [tag_4.id],
+ ).perform!
+
+ expect(Notification.where(user: topic_watcher).count).to eq(notifications_before)
+ expect(topic_1.reload.bumped_at).to be_within(1.second).of(bumped_at_before)
+ expect(topic_1.first_post.reload.version).to eq(version_before)
+ end
+
+ it "only updates topics the acting user can edit" do
+ other_user = Fabricate(:user, refresh_auto_groups: true)
+ other_topic = Fabricate(:topic_with_op, user: other_user, tags: [tag_1])
+
+ topic_ids =
+ TopicsBulkAction.new(
+ other_user,
+ [topic_1.id, other_topic.id],
+ type: "manage_tags",
+ add_tag_ids: [tag_4.id],
+ ).perform!
+
+ expect(topic_ids).to contain_exactly(other_topic.id)
+ expect(topic_1.reload.tags).to contain_exactly(tag_1, tag_2)
+ expect(other_topic.reload.tags).to contain_exactly(tag_1, tag_4)
+ end
+
+ it "ignores unknown tag ids" do
+ topic_ids =
+ TopicsBulkAction.new(
+ user,
+ [topic_1.id],
+ type: "manage_tags",
+ add_tag_ids: [999_999],
+ ).perform!
+
+ expect(topic_ids).to be_empty
+ expect(topic_1.reload.tags).to contain_exactly(tag_1, tag_2)
+ end
+ end
end
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index e2afd679a07..7e7972f92f6 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -4631,6 +4631,56 @@ RSpec.describe TopicsController do
expect(topic.reload.tags).to include(tag1)
end
+ it "can manage tags via add, remove and replace across multiple topics" do
+ SiteSetting.tagging_enabled = true
+ SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
+ tag_1 = Fabricate(:tag)
+ tag_2 = Fabricate(:tag)
+ tag_3 = Fabricate(:tag)
+ tag_4 = Fabricate(:tag)
+ topic_1 = Fabricate(:topic_with_op, user: user, tags: [tag_1, tag_2])
+ topic_2 = Fabricate(:topic_with_op, user: user, tags: [tag_2, tag_3])
+ topic_3 = Fabricate(:topic_with_op, user: user, tags: [tag_1])
+
+ put "/topics/bulk.json",
+ params: {
+ topic_ids: [topic_1.id, topic_2.id, topic_3.id],
+ operation: {
+ type: "manage_tags",
+ add_tag_ids: [tag_4.id],
+ remove_tag_ids: [tag_2.id],
+ replace_tags: [{ from_tag_id: tag_1.id, to_tag_id: tag_3.id }],
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(topic_1.reload.tags).to contain_exactly(tag_3, tag_4)
+ expect(topic_2.reload.tags).to contain_exactly(tag_3, tag_4)
+ expect(topic_3.reload.tags).to contain_exactly(tag_3, tag_4)
+ end
+
+ it "can clear all tags with remove_all_tags across multiple topics" do
+ SiteSetting.tagging_enabled = true
+ SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
+ tag_1 = Fabricate(:tag)
+ tag_2 = Fabricate(:tag)
+ topic_1 = Fabricate(:topic_with_op, user: user, tags: [tag_1, tag_2])
+ topic_2 = Fabricate(:topic_with_op, user: user, tags: [tag_2])
+
+ put "/topics/bulk.json",
+ params: {
+ topic_ids: [topic_1.id, topic_2.id],
+ operation: {
+ type: "manage_tags",
+ remove_all_tags: true,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(topic_1.reload.tags).to be_empty
+ expect(topic_2.reload.tags).to be_empty
+ end
+
it "includes errors in the response when operations partially fail" do
sign_in(Fabricate(:admin))
diff --git a/spec/system/page_objects/components/topic_list.rb b/spec/system/page_objects/components/topic_list.rb
index e8563a8ca84..3ad9e47752e 100644
--- a/spec/system/page_objects/components/topic_list.rb
+++ b/spec/system/page_objects/components/topic_list.rb
@@ -112,8 +112,12 @@ module PageObjects
)
end
- def has_topic_tags?(topic, *tag_names)
- tag_names.all? { |name| has_topic_tag?(topic, name) }
+ def has_topic_tags?(topic, tags:)
+ tags.all? { |tag| has_topic_tag?(topic, tag.name) } &&
+ page.has_css?(
+ "#{topic_list_item_class(topic)} .discourse-tags .discourse-tag",
+ count: tags.size,
+ )
end
def has_no_topic_tags?(topic)
diff --git a/spec/system/page_objects/modals/manage_tags.rb b/spec/system/page_objects/modals/manage_tags.rb
new file mode 100644
index 00000000000..b7f9fda1042
--- /dev/null
+++ b/spec/system/page_objects/modals/manage_tags.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Modals
+ class ManageTags < PageObjects::Modals::Base
+ MODAL_SELECTOR = ".topic-bulk-actions-modal"
+ ROOT_SELECTOR = ".manage-tags-form"
+
+ def add_tag_selector
+ tag_selector("[data-name='add_tags'] .tag-chooser")
+ end
+
+ def add_tags(*tag_names)
+ select_tags(add_tag_selector, tag_names)
+ end
+
+ def remove_tags(*tag_names)
+ select_tags(remove_tag_selector, tag_names)
+ end
+
+ def select_replace_from(tag_name, index: 0)
+ select_tags(replace_from_selector(index), [tag_name])
+ end
+
+ def select_replace_to(tag_name, index: 0)
+ select_tags(replace_to_selector(index), [tag_name])
+ end
+
+ def has_replace_row_error?(index: 0, text:)
+ has_css?(
+ "#{ROOT_SELECTOR} [data-name^='replace_rows.#{index}.'] .form-kit__errors",
+ text: text,
+ )
+ end
+
+ def has_no_replace_row_error?(index: 0)
+ has_no_css?("#{ROOT_SELECTOR} [data-name^='replace_rows.#{index}.'] .form-kit__errors")
+ end
+
+ def toggle_remove_all
+ PageObjects::Components::DToggleSwitch.new(
+ "#{ROOT_SELECTOR} [data-name='remove_all_tags'] .d-toggle-switch__checkbox",
+ ).toggle
+ end
+
+ def click_confirm
+ find("#bulk-topics-confirm").click
+ end
+
+ def has_disabled_submit?
+ has_css?("#bulk-topics-confirm[disabled]")
+ end
+
+ def has_remove_all_notice?
+ has_css?("#{ROOT_SELECTOR} .manage-tags-form__warning")
+ end
+
+ def has_no_remove_tag_selector?
+ has_no_css?("#{ROOT_SELECTOR} [data-name='remove_tags'] .tag-chooser")
+ end
+
+ private
+
+ def tag_selector(scope)
+ PageObjects::Components::SelectKit.new("#{ROOT_SELECTOR} #{scope}")
+ end
+
+ def remove_tag_selector
+ tag_selector("[data-name='remove_tags'] .tag-chooser")
+ end
+
+ def replace_from_selector(index = 0)
+ tag_selector("[data-name='replace_rows.#{index}.from'] .tag-chooser")
+ end
+
+ def replace_to_selector(index = 0)
+ tag_selector("[data-name='replace_rows.#{index}.to'] .tag-chooser")
+ end
+
+ def select_tags(selector, tag_names)
+ selector.expand
+ tag_names.each do |name|
+ selector.search(name)
+ selector.select_row_by_name(name)
+ end
+ selector.collapse
+ end
+ end
+ end
+end
diff --git a/spec/system/page_objects/modals/topic_bulk_actions.rb b/spec/system/page_objects/modals/topic_bulk_actions.rb
index 8c64198aac8..3af8ce0ad39 100644
--- a/spec/system/page_objects/modals/topic_bulk_actions.rb
+++ b/spec/system/page_objects/modals/topic_bulk_actions.rb
@@ -4,10 +4,6 @@ module PageObjects
class TopicBulkActions < PageObjects::Modals::Base
MODAL_SELECTOR = ".topic-bulk-actions-modal"
- def tag_selector
- PageObjects::Components::SelectKit.new(".tag-chooser")
- end
-
def category_selector
PageObjects::Components::SelectKit.new(".category-chooser")
end
diff --git a/spec/system/search_spec.rb b/spec/system/search_spec.rb
index e1b2b8d6679..f53b94e9337 100644
--- a/spec/system/search_spec.rb
+++ b/spec/system/search_spec.rb
@@ -255,13 +255,10 @@ describe "Search" do
find(".search-info .bulk-select").click
find(".fps-result .fps-topic[data-topic-id=\"#{topic.id}\"] .bulk-select input").click
find(".search-info .bulk-select-topics-dropdown-trigger").click
- find(".bulk-select-topics-dropdown-content .append-tags").click
- expect(topic_bulk_actions_modal).to be_open
- tag_selector = PageObjects::Components::SelectKit.new(".tag-chooser")
- tag_selector.search(tag1.name)
- tag_selector.select_row_by_name(tag1.name)
- tag_selector.collapse
- topic_bulk_actions_modal.click_bulk_topics_confirm
+ PageObjects::Components::TopicListHeader.new.click_bulk_button("manage-tags")
+ manage_tags_modal = PageObjects::Modals::ManageTags.new
+ manage_tags_modal.add_tags(tag1.name)
+ manage_tags_modal.click_confirm
expect(
find(".fps-result .fps-topic[data-topic-id=\"#{topic.id}\"] .discourse-tags"),
).to have_content(tag1.name)
diff --git a/spec/system/tags/tags_spec.rb b/spec/system/tags/tags_spec.rb
index 67554a50332..69cf0019e5f 100644
--- a/spec/system/tags/tags_spec.rb
+++ b/spec/system/tags/tags_spec.rb
@@ -91,7 +91,7 @@ describe "Tags" do
# /latest
expect(discovery.topic_list).to have_topic_tag(topic_with_one_tag, "tag-one")
- expect(discovery.topic_list).to have_topic_tags(topic_with_two_tags, "tag-one", "tag-two")
+ expect(discovery.topic_list).to have_topic_tags(topic_with_two_tags, tags: [tag_one, tag_two])
expect(discovery.topic_list).to have_no_topic_tags(topic_with_no_tags)
expect(discovery.tag_drop).to have_selected_name("tags") # unselected
diff --git a/spec/system/topic_bulk_select_spec.rb b/spec/system/topic_bulk_select_spec.rb
index 10c437a5c5f..b3ed53ecf9b 100644
--- a/spec/system/topic_bulk_select_spec.rb
+++ b/spec/system/topic_bulk_select_spec.rb
@@ -141,72 +141,87 @@ describe "Topic bulk select" do
end
end
- context "when appending tags" do
+ context "when managing tags" do
fab!(:tag1, :tag)
fab!(:tag2, :tag)
fab!(:tag3, :tag)
+ fab!(:topic) { Fabricate(:post).topic }
+ fab!(:topic_2) { Fabricate(:post).topic }
before { SiteSetting.tagging_enabled = true }
- def open_append_modal(topics_to_select = nil)
+ def open_manage_tags_modal(topics_to_select)
sign_in(admin)
visit("/latest")
- open_bulk_actions_modal(topics_to_select, "append-tags")
+ open_bulk_actions_modal(topics_to_select, "manage-tags")
+ PageObjects::Modals::ManageTags.new
end
context "when in mobile", mobile: true do
it "is working" do
# behavior is already tested on desktop, we simply ensure
# the general workflow is working on mobile
- open_append_modal
+ open_manage_tags_modal([topic, topic_2])
end
end
- it "appends tags to selected topics" do
- open_append_modal
+ it "removes all tags when the toggle is enabled" do
+ topic.update!(tags: [tag1, tag2, tag3])
+ topic_2.update!(tags: [tag1, tag2])
- topic_bulk_actions_modal.tag_selector.expand
- topic_bulk_actions_modal.tag_selector.search(tag1.name)
- topic_bulk_actions_modal.tag_selector.select_row_by_name(tag1.name)
- topic_bulk_actions_modal.tag_selector.search(tag2.name)
- topic_bulk_actions_modal.tag_selector.select_row_by_name(tag2.name)
+ modal = open_manage_tags_modal([topic, topic_2])
+ modal.toggle_remove_all
- topic_bulk_actions_modal.click_bulk_topics_confirm
+ expect(modal).to have_remove_all_notice
+ expect(modal).to have_no_remove_tag_selector
+
+ modal.click_confirm
- expect(
- find(topic_list.topic_list_item_class(topics.last)).find(".discourse-tags"),
- ).to have_content(tag1.name)
- expect(
- find(topic_list.topic_list_item_class(topics.last)).find(".discourse-tags"),
- ).to have_content(tag2.name)
+ expect(topic_list).to have_no_topic_tags(topic)
+ expect(topic_list).to have_no_topic_tags(topic_2)
end
- context "when selecting topics in different categories" do
- before do
- topics
- .last(2)
- .each do |topic|
- topic.update!(category: Fabricate(:category))
- topic.update!(category: Fabricate(:category))
- end
- end
+ it "adds, removes, and replaces tags in a single submission" do
+ tag4 = Fabricate(:tag)
+ tag5 = Fabricate(:tag)
+ topic.update!(tags: [tag1, tag2])
+ topic_2.update!(tags: [tag1, tag3])
- it "does not show an additional note about the category in the modal" do
- open_append_modal(topics.last(2))
+ modal = open_manage_tags_modal([topic, topic_2])
+ expect(modal).to have_disabled_submit
- expect(topic_bulk_actions_modal).to have_no_category_badge(topics.last.reload.category)
- end
+ modal.select_replace_from(tag1.name)
+ modal.select_replace_to(tag5.name)
+
+ modal.add_tags(tag4.name)
+ modal.remove_tags(tag2.name)
+
+ modal.click_confirm
+
+ expect(topic_list).to have_topic_tags(topic, tags: [tag4, tag5])
+ expect(topic_list).to have_topic_tags(topic_2, tags: [tag3, tag4, tag5])
end
context "when selecting topics that are all in the same category" do
fab!(:category)
- before { topics.last.update!(category_id: category.id) }
+ before do
+ topic.update!(category_id: category.id)
+ topic_2.update!(category_id: category.id)
+ end
+
+ it "limits tag search to restricted tags when category does not allow global tags" do
+ restricted_tag_group = Fabricate(:tag_group)
+ restricted_tag = Fabricate(:tag)
+ TagGroupMembership.create!(tag: restricted_tag, tag_group: restricted_tag_group)
+ CategoryTagGroup.create!(category: category, tag_group: restricted_tag_group)
+
+ modal = open_manage_tags_modal([topic, topic_2])
- it "shows an additional note about the category in the modal" do
- open_append_modal
- expect(topic_bulk_actions_modal).to have_category_badge(category)
+ modal.add_tag_selector.expand
+
+ expect(modal.add_tag_selector.option_names).to contain_exactly(restricted_tag.name)
end
it "allows for searching restricted tags for that category and other tags too if the category allows it" do
@@ -216,22 +231,14 @@ describe "Topic bulk select" do
CategoryTagGroup.create!(category: category, tag_group: restricted_tag_group)
category.update!(allow_global_tags: true)
- open_append_modal
+ modal = open_manage_tags_modal([topic, topic_2])
- topic_bulk_actions_modal.tag_selector.expand
- topic_bulk_actions_modal.tag_selector.search(restricted_tag.name)
- topic_bulk_actions_modal.tag_selector.select_row_by_name(restricted_tag.name)
- topic_bulk_actions_modal.tag_selector.search(tag1.name)
- topic_bulk_actions_modal.tag_selector.select_row_by_name(tag1.name)
+ modal.add_tags(restricted_tag.name, tag1.name)
- topic_bulk_actions_modal.click_bulk_topics_confirm
+ modal.click_confirm
- expect(
- find(topic_list.topic_list_item_class(topics.last)).find(".discourse-tags"),
- ).to have_content(restricted_tag.name)
- expect(
- find(topic_list.topic_list_item_class(topics.last)).find(".discourse-tags"),
- ).to have_content(tag1.name)
+ expect(topic_list).to have_topic_tags(topic, tags: [restricted_tag, tag1])
+ expect(topic_list).to have_topic_tags(topic_2, tags: [restricted_tag, tag1])
end
end
end