A platform for community discussion. Free, open, simple.
FEATURE: Bulk enable/disable nested replies from topic list (#39944)
Mark VanLandingham66c2beab2d51
diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 6ae26f378ff..be9795f9485 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -3763,6 +3763,8 @@ en:relist_topics: "Relist topics"pin_topics: "Feature these topics"unpin_topics: "Un-pin topics"+ enable_nested_replies: "Enable nested replies"+ disable_nested_replies: "Disable nested replies"reset_bump_dates: "Reset bump dates"defer: "Mark unread"delete: "Delete topics"@@ -3821,6 +3823,12 @@ en:confirm_unpin_topics:one: "Un-pin %{count} topic"other: "Un-pin %{count} topics"+ confirm_enable_nested_replies:+ one: "Enable nested replies on %{count} topic"+ other: "Enable nested replies on %{count} topics"+ confirm_disable_nested_replies:+ one: "Disable nested replies on %{count} topic"+ other: "Disable nested replies on %{count} topics"confirm_update_topics:one: "Update %{count} topic"other: "Update %{count} topics"@@ -3950,6 +3958,12 @@ en:unpin_topics:name: "Un-pin"description: "Un-pin all the selected topics that are pinned to a category or pinned globally"+ enable_nested_replies:+ name: "Enable nested replies"+ description: "Switch the selected topics to nested view. Replies will be displayed as threads."+ disable_nested_replies:+ name: "Disable nested replies"+ description: "Switch the selected topics back to flat view."nested_replies:nested_view: "Nested view"diff --git a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjsindex b87c319f3d5..c9b837dc8a6 100644--- a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs+++ b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs@@ -145,6 +145,28 @@ export default class BulkSelectTopicsDropdown extends Component {visible: ({ topics, currentUser }) =>currentUser.staff && topics.every((t) => !t.isPrivateMessage),},+ {+ id: "enable-nested-replies",+ icon: "nested-thread",+ name: i18n("topic_bulk_actions.enable_nested_replies.name"),+ visible: ({ topics, currentUser, siteSettings }) =>+ currentUser.staff &&+ siteSettings.nested_replies_enabled &&+ !siteSettings.nested_replies_default &&+ !topics.some((t) => t.isPrivateMessage) &&+ topics.some((t) => !t.is_nested_view),+ },+ {+ id: "disable-nested-replies",+ icon: "nested-thread",+ name: i18n("topic_bulk_actions.disable_nested_replies.name"),+ visible: ({ topics, currentUser, siteSettings }) =>+ currentUser.staff &&+ siteSettings.nested_replies_enabled &&+ !siteSettings.nested_replies_default &&+ !topics.some((t) => t.isPrivateMessage) &&+ topics.some((t) => t.is_nested_view),+ },{id: "unlist-topics",icon: "far-eye-slash",@@ -344,6 +366,24 @@ export default class BulkSelectTopicsDropdown extends Component {confirmButtonTranslationKey: "topics.bulk.confirm_unpin_topics",});break;+ case "enable-nested-replies":+ this.showBulkTopicActionsModal(actionId, "enable_nested_replies", {+ description: i18n(+ "topic_bulk_actions.enable_nested_replies.description"+ ),+ confirmButtonTranslationKey:+ "topics.bulk.confirm_enable_nested_replies",+ });+ break;+ case "disable-nested-replies":+ this.showBulkTopicActionsModal(actionId, "disable_nested_replies", {+ description: i18n(+ "topic_bulk_actions.disable_nested_replies.description"+ ),+ confirmButtonTranslationKey:+ "topics.bulk.confirm_disable_nested_replies",+ });+ break;case "defer":this.deferTopics();break;diff --git a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs b/frontend/discourse/app/components/modal/bulk-topic-actions.gjsindex 0e604779bed..8b8a6487aba 100644--- a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs+++ b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs@@ -211,6 +211,16 @@ export default class BulkTopicActions extends Component {case "unpin":this.forEachPerformed({ type: "unpin" }, (t) => t.set("pinned", false));break;+ case "enable-nested-replies":+ this.forEachPerformed({ type: "enable_nested_view" }, (t) =>+ t.set("is_nested_view", true)+ );+ break;+ case "disable-nested-replies":+ this.forEachPerformed({ type: "disable_nested_view" }, (t) =>+ t.set("is_nested_view", false)+ );+ break;case "pin":this.performAndRefresh({ type: "pin", ...opts });break;diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rbindex 307ebbb41f9..13b9b694958 100644--- a/lib/topics_bulk_action.rb+++ b/lib/topics_bulk_action.rb@@ -35,6 +35,8 @@ class TopicsBulkActionunpinconvert_to_public_topicconvert_to_private_message+ enable_nested_view+ disable_nested_view]end@@ -300,6 +302,26 @@ class TopicsBulkActionendend+ def enable_nested_view+ toggle_nested_view(enabled: true)+ end++ def disable_nested_view+ toggle_nested_view(enabled: false)+ end++ def toggle_nested_view(enabled:)+ return unless SiteSetting.nested_replies_enabled+ return if SiteSetting.nested_replies_default++ topics.each do |t|+ next if t.private_message?++ result = NestedTopic::Toggle.call(guardian: guardian, params: { topic_id: t.id, enabled: })+ @changed_ids << t.id if result.success?+ end+ end+def change_tagstags = resolve_tag_names || []topics_with_tags.each { |t| apply_tag_revision(t, tags) }diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rbindex 7d878efac68..745fea4add9 100644--- a/spec/lib/topics_bulk_action_spec.rb+++ b/spec/lib/topics_bulk_action_spec.rb@@ -1157,4 +1157,99 @@ RSpec.describe TopicsBulkAction doexpect(topic_1.reload.tags).to contain_exactly(tag_1, tag_2)endend++ describe "#enable_nested_view" do+ fab!(:admin)+ fab!(:topic_2, :topic)++ before { SiteSetting.nested_replies_enabled = true }++ it "creates NestedTopic records for selected topics when user is staff" do+ topic_ids =+ TopicsBulkAction.new(admin, [topic.id, topic_2.id], type: "enable_nested_view").perform!++ expect(topic_ids).to contain_exactly(topic.id, topic_2.id)+ expect(NestedTopic.where(topic_id: [topic.id, topic_2.id]).count).to eq(2)+ end++ it "is idempotent when a NestedTopic already exists" do+ Fabricate(:nested_topic, topic: topic)++ expect {+ TopicsBulkAction.new(admin, [topic.id], type: "enable_nested_view").perform!+ }.not_to change { NestedTopic.count }+ end++ it "skips private messages" do+ pm = Fabricate(:private_message_topic)++ topic_ids = TopicsBulkAction.new(admin, [pm.id], type: "enable_nested_view").perform!++ expect(topic_ids).to be_empty+ expect(NestedTopic.where(topic_id: pm.id)).not_to exist+ end++ it "does nothing when user is not staff" do+ topic_ids =+ TopicsBulkAction.new(user, [topic.id, topic_2.id], type: "enable_nested_view").perform!++ expect(topic_ids).to be_empty+ expect(NestedTopic.where(topic_id: [topic.id, topic_2.id])).not_to exist+ end++ it "does nothing when nested_replies_enabled is off" do+ SiteSetting.nested_replies_enabled = false++ topic_ids =+ TopicsBulkAction.new(admin, [topic.id, topic_2.id], type: "enable_nested_view").perform!++ expect(topic_ids).to be_empty+ expect(NestedTopic.count).to eq(0)+ end++ it "does nothing when nested_replies_default is on" do+ SiteSetting.nested_replies_default = true++ topic_ids =+ TopicsBulkAction.new(admin, [topic.id, topic_2.id], type: "enable_nested_view").perform!++ expect(topic_ids).to be_empty+ expect(NestedTopic.count).to eq(0)+ end+ end++ describe "#disable_nested_view" do+ fab!(:admin)+ fab!(:topic_2, :topic)++ before do+ SiteSetting.nested_replies_enabled = true+ Fabricate(:nested_topic, topic: topic)+ Fabricate(:nested_topic, topic: topic_2)+ end++ it "destroys NestedTopic records for selected topics when user is staff" do+ topic_ids =+ TopicsBulkAction.new(admin, [topic.id, topic_2.id], type: "disable_nested_view").perform!++ expect(topic_ids).to contain_exactly(topic.id, topic_2.id)+ expect(NestedTopic.where(topic_id: [topic.id, topic_2.id])).not_to exist+ end++ it "is a no-op for topics without a NestedTopic record" do+ topic_3 = Fabricate(:topic)++ topic_ids = TopicsBulkAction.new(admin, [topic_3.id], type: "disable_nested_view").perform!++ expect(topic_ids).to eq([topic_3.id])+ end++ it "does nothing when user is not staff" do+ topic_ids =+ TopicsBulkAction.new(user, [topic.id, topic_2.id], type: "disable_nested_view").perform!++ expect(topic_ids).to be_empty+ expect(NestedTopic.where(topic_id: [topic.id, topic_2.id]).count).to eq(2)+ end+ endend