Skip to content

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

DEV: remove and replace Uncategorized (#41169)

David Battersby1b3329843528
diff --git a/app/services/site_setting/action/remove_and_replace_uncategorized_toggled.rb b/app/services/site_setting/action/remove_and_replace_uncategorized_toggled.rb
new file mode 100644
index 00000000000..631547f0223
--- /dev/null
+++ b/app/services/site_setting/action/remove_and_replace_uncategorized_toggled.rb
@@ -0,0 +1,106 @@
+# frozen_string_literal: true
+
+# Reacts to the `remove_and_replace_uncategorized` upcoming change being
+# enabled or disabled (manually or via auto-promotion). See
+# config/initializers/015-track-upcoming-change-toggle.rb for the dispatch.
+#
+# Enabling demotes the special Uncategorized category to a normal category
+# in place by repointing `uncategorized_category_id` to -1 (the category's
+# "special" behavior is derived entirely from that setting), then disallows
+# uncategorized topics. No category is created and no topics are moved. If the
+# composer had no explicit default (so it fell back to the special
+# Uncategorized category), the now-normal category is pinned as the
+# `default_composer_category` so the composer keeps pre-selecting it. An
+# explicit default already pointing at that category needs no change — the id
+# simply becomes a normal category.
+#
+# The site's prior state is snapshotted onto the change's UpcomingChangeEvent
+# so that opt-out can either restore the special category or do nothing.
+
+class SiteSetting::Action::RemoveAndReplaceUncategorizedToggled < Service::ActionBase
+ UPCOMING_CHANGE = :remove_and_replace_uncategorized
+
+ SNAPSHOT_EVENT_TYPES = %i[manual_opt_in automatically_promoted].freeze
+
+ option :enabled
+
+ def call
+ enabled ? enable : disable
+ end
+
+ def self.should_display_upcoming_change?
+ SiteSetting.allow_uncategorized_topics || UpcomingChanges.enabled?(UPCOMING_CHANGE)
+ end
+
+ private
+
+ def enable
+ # -1 is the canonical "already migrated" marker, so this is idempotent.
+ return if SiteSetting.uncategorized_category_id == -1
+
+ former_uncategorized_id = SiteSetting.uncategorized_category_id
+
+ ActiveRecord::Base.transaction do
+ capture_snapshot(
+ allow_uncategorized_topics: SiteSetting.allow_uncategorized_topics,
+ default_composer_category: SiteSetting.default_composer_category,
+ uncategorized_category_id: former_uncategorized_id,
+ )
+
+ if SiteSetting.default_composer_category.blank?
+ SiteSetting.set_and_log(:default_composer_category, former_uncategorized_id.to_s)
+ end
+
+ SiteSetting.set_and_log(:uncategorized_category_id, -1)
+ SiteSetting.set_and_log(:allow_uncategorized_topics, false)
+ end
+
+ Site.clear_cache
+ end
+
+ def disable
+ snapshot = read_snapshot
+ return if snapshot.blank?
+
+ # If the site was not using uncategorized topics at opt-in, there is no
+ # special category to restore.
+ return unless snapshot["allow_uncategorized_topics"]
+
+ ActiveRecord::Base.transaction do
+ SiteSetting.set_and_log(:uncategorized_category_id, snapshot["uncategorized_category_id"])
+ SiteSetting.set_and_log(:allow_uncategorized_topics, true)
+
+ if SiteSetting.default_composer_category != snapshot["default_composer_category"]
+ SiteSetting.set_and_log(:default_composer_category, snapshot["default_composer_category"])
+ end
+ end
+
+ Site.clear_cache
+ end
+
+ # Capture once: the first opt-in/promotion snapshot is the canonical
+ # pre-migration state. Later opt-ins intentionally reuse it (so their events
+ # keep event_data: nil), and opt-out always restores that original state.
+ def capture_snapshot(snapshot)
+ return if snapshot_event.present?
+
+ UpcomingChangeEvent
+ .where(upcoming_change_name: UPCOMING_CHANGE.to_s, event_type: SNAPSHOT_EVENT_TYPES)
+ .order(created_at: :desc)
+ .first
+ &.update!(event_data: snapshot)
+ end
+
+ def read_snapshot
+ snapshot_event&.event_data
+ end
+
+ def snapshot_event
+ @snapshot_event ||=
+ UpcomingChangeEvent
+ .where(upcoming_change_name: UPCOMING_CHANGE.to_s, event_type: SNAPSHOT_EVENT_TYPES)
+ .where.not(event_data: nil)
+ .order(created_at: :desc)
+ .first
+ end
+end
diff --git a/app/services/upcoming_changes/notify_promotion.rb b/app/services/upcoming_changes/notify_promotion.rb
index 568ad5c8134..13b26c2757a 100644
--- a/app/services/upcoming_changes/notify_promotion.rb
+++ b/app/services/upcoming_changes/notify_promotion.rb
@@ -130,6 +130,11 @@ class UpcomingChanges::NotifyPromotion
upcoming_change_name: params.setting_name,
acting_user: Discourse.system_user,
)
+
+ UpcomingChangeEvent.find_or_create_by(
+ event_type: :automatically_promoted,
+ upcoming_change_name: params.setting_name,
+ ) { |event| event.acting_user = Discourse.system_user }
end
def trigger_discourse_event(params:)
diff --git a/config/initializers/015-track-upcoming-change-toggle.rb b/config/initializers/015-track-upcoming-change-toggle.rb
index c21d38ea991..1118499e69a 100644
--- a/config/initializers/015-track-upcoming-change-toggle.rb
+++ b/config/initializers/015-track-upcoming-change-toggle.rb
@@ -22,6 +22,8 @@ DiscourseEvent.on(:upcoming_change_enabled) do |setting_name|
SiteSetting::Action::SimpleEmailSubjectToggled.call(params: { setting_enabled: true })
elsif setting_name == :enable_horizon_high_context_topic_cards
Themes::Action::HorizonHighContextTopicCardsToggled.call(enabled: true)
+ elsif setting_name == :remove_and_replace_uncategorized
+ SiteSetting::Action::RemoveAndReplaceUncategorizedToggled.call(enabled: true)
end
end
@@ -31,5 +33,7 @@ DiscourseEvent.on(:upcoming_change_disabled) do |setting_name|
SiteSetting::Action::SimpleEmailSubjectToggled.call(params: { setting_enabled: false })
elsif setting_name == :enable_horizon_high_context_topic_cards
Themes::Action::HorizonHighContextTopicCardsToggled.call(enabled: false)
+ elsif setting_name == :remove_and_replace_uncategorized
+ SiteSetting::Action::RemoveAndReplaceUncategorizedToggled.call(enabled: false)
end
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index e07934f54dc..7b3e843be69 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1904,6 +1904,7 @@ en:
category_search_priority_high_weight: "Weight applied to ranking for high category search priority."
default_composer_category: "The category used to pre-populate the category dropdown when creating a new topic."
allow_uncategorized_topics: "Allow topics to be created without a category. WARNING: If there are any uncategorized topics, you must recategorize them before turning this off."
+ remove_and_replace_uncategorized: "Replaces the special Uncategorized category with a standard category of the same name, and migrates all topics over to it."
allow_duplicate_topic_titles: "Allow topics with identical, duplicate titles."
allow_duplicate_topic_titles_category: "Allow topics with identical, duplicate titles if the category is different. {{setting:allow_duplicate_topic_titles}} must be disabled."
unique_posts_mins: "How many minutes before a user can make a post with the same content again"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 39a7598983a..459f34735de 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1381,6 +1381,19 @@ posting:
default: false
refresh: true
area: "posts_and_topics"
+ remove_and_replace_uncategorized:
+ default: false
+ client: true
+ hidden: true
+ upcoming_change:
+ status: "experimental"
+ impact: "other,all_members"
+ allow_enabled_for:
+ - everyone
+ hide_settings:
+ - allow_uncategorized_topics
+ - suppress_uncategorized_badge
+ learn_more_url: "https://meta.discourse.org/t/-/406373"
allow_duplicate_topic_titles:
default: false
area: "posts_and_topics"
diff --git a/lib/upcoming_changes.rb b/lib/upcoming_changes.rb
index 5a82a4aad6c..e07757bfa99 100644
--- a/lib/upcoming_changes.rb
+++ b/lib/upcoming_changes.rb
@@ -32,6 +32,12 @@ module UpcomingChanges
def self.should_display_enable_gifs?
!DiscourseGifs.component_installed?
end
+
+ # Only relevant on sites that currently allow uncategorized topics, and must
+ # stay visible after being enabled (which disables that setting).
+ def self.should_display_remove_and_replace_uncategorized?
+ SiteSetting::Action::RemoveAndReplaceUncategorizedToggled.should_display_upcoming_change?
+ end
end
def self.user_enabled_reasons
diff --git a/spec/lib/site_setting_extension_spec.rb b/spec/lib/site_setting_extension_spec.rb
index 28412150e03..4b3a3e8a446 100644
--- a/spec/lib/site_setting_extension_spec.rb
+++ b/spec/lib/site_setting_extension_spec.rb
@@ -724,6 +724,7 @@ RSpec.describe SiteSettingExtension do
},
)
settings.refresh!
+ allow(UpcomingChanges).to receive(:enabled?).and_return(false)
allow(UpcomingChanges).to receive(:enabled?).with(:enable_cool_thing).and_return(true)
end
@@ -745,6 +746,7 @@ RSpec.describe SiteSettingExtension do
},
)
settings.refresh!
+ allow(UpcomingChanges).to receive(:enabled?).and_return(false)
allow(UpcomingChanges).to receive(:enabled?).with(:enable_cool_thing).and_return(true)
end
diff --git a/spec/services/site_setting/action/remove_and_replace_uncategorized_toggled_spec.rb b/spec/services/site_setting/action/remove_and_replace_uncategorized_toggled_spec.rb
new file mode 100644
index 00000000000..66a09ae6154
--- /dev/null
+++ b/spec/services/site_setting/action/remove_and_replace_uncategorized_toggled_spec.rb
@@ -0,0 +1,202 @@
+# frozen_string_literal: true
+
+RSpec.describe SiteSetting::Action::RemoveAndReplaceUncategorizedToggled do
+ fab!(:uncategorized) { Fabricate(:category, name: "Special category") }
+
+ before do
+ SiteSetting.uncategorized_category_id = uncategorized.id
+ SiteSetting.allow_uncategorized_topics = true
+ end
+
+ describe "enabling" do
+ it "demotes the special category in place and disallows uncategorized topics" do
+ described_class.call(enabled: true)
+
+ expect(SiteSetting.uncategorized_category_id).to eq(-1)
+ expect(SiteSetting.allow_uncategorized_topics).to eq(false)
+ expect(uncategorized.reload.uncategorized?).to eq(false)
+ end
+
+ it "leaves default_composer_category pointing at the now-normal category" do
+ SiteSetting.default_composer_category = uncategorized.id.to_s
+
+ described_class.call(enabled: true)
+
+ expect(SiteSetting.default_composer_category).to eq(uncategorized.id.to_s)
+ end
+
+ it "pins the now-normal category as the composer default when none was set" do
+ SiteSetting.default_composer_category = ""
+
+ described_class.call(enabled: true)
+
+ expect(SiteSetting.default_composer_category).to eq(uncategorized.id.to_s)
+ end
+
+ it "leaves an unrelated default_composer_category untouched" do
+ other_category = Fabricate(:category)
+ SiteSetting.default_composer_category = other_category.id.to_s
+
+ described_class.call(enabled: true)
+
+ expect(SiteSetting.default_composer_category).to eq(other_category.id.to_s)
+ end
+
+ it "snapshots the prior state onto the existing manual_opt_in event" do
+ SiteSetting.default_composer_category = uncategorized.id.to_s
+ event =
+ UpcomingChangeEvent.create!(
+ event_type: :manual_opt_in,
+ upcoming_change_name: "remove_and_replace_uncategorized",
+ )
+
+ described_class.call(enabled: true)
+
+ expect(event.reload.event_data).to eq(
+ "allow_uncategorized_topics" => true,
+ "default_composer_category" => uncategorized.id.to_s,
+ "uncategorized_category_id" => uncategorized.id,
+ )
+ end
+
+ it "snapshots the prior state onto the existing automatically_promoted event" do
+ event =
+ UpcomingChangeEvent.create!(
+ event_type: :automatically_promoted,
+ upcoming_change_name: "remove_and_replace_uncategorized",
+ )
+
+ described_class.call(enabled: true)
+
+ expect(event.reload.event_data["uncategorized_category_id"]).to eq(uncategorized.id)
+ expect(event.event_data["allow_uncategorized_topics"]).to eq(true)
+ end
+
+ it "captures the snapshot even when an unrelated event already carries event_data" do
+ # The framework records a `status_changed` event whose event_data holds the
+ # status transition. The snapshot lookup must ignore it, not treat it as an
+ # existing snapshot.
+ UpcomingChangeEvent.create!(
+ event_type: :status_changed,
+ upcoming_change_name: "remove_and_replace_uncategorized",
+ event_data: {
+ previous_value: nil,
+ new_value: "experimental",
+ },
+ )
+ opt_in =
+ UpcomingChangeEvent.create!(
+ event_type: :manual_opt_in,
+ upcoming_change_name: "remove_and_replace_uncategorized",
+ )
+
+ described_class.call(enabled: true)
+
+ expect(opt_in.reload.event_data).to include("uncategorized_category_id" => uncategorized.id)
+ end
+
+ it "is idempotent and does not re-snapshot when already migrated" do
+ UpcomingChangeEvent.create!(
+ event_type: :automatically_promoted,
+ upcoming_change_name: "remove_and_replace_uncategorized",
+ )
+ described_class.call(enabled: true)
+ snapshot = UpcomingChangeEvent.last.event_data
+ original_event_count = UpcomingChangeEvent.count
+
+ # Simulate drift that should be ignored because the change is already applied.
+ SiteSetting.uncategorized_category_id = -1
+ described_class.call(enabled: true)
+
+ expect(UpcomingChangeEvent.count).to eq(original_event_count)
+ expect(UpcomingChangeEvent.last.event_data).to eq(snapshot)
+ end
+ end
+
+ describe "disabling" do
+ it "restores the special category and the prior settings from the snapshot" do
+ SiteSetting.default_composer_category = uncategorized.id.to_s
+ UpcomingChangeEvent.create!(
+ event_type: :automatically_promoted,
+ upcoming_change_name: "remove_and_replace_uncategorized",
+ event_data: {
+ allow_uncategorized_topics: true,
+ default_composer_category: uncategorized.id.to_s,
+ uncategorized_category_id: uncategorized.id,
+ },
+ )
+ # Site is currently in the "enabled" state.
+ SiteSetting.uncategorized_category_id = -1
+ SiteSetting.allow_uncategorized_topics = false
+ SiteSetting.default_composer_category = ""
+
+ described_class.call(enabled: false)
+
+ expect(SiteSetting.uncategorized_category_id).to eq(uncategorized.id)
+ expect(SiteSetting.allow_uncategorized_topics).to eq(true)
+ expect(SiteSetting.default_composer_category).to eq(uncategorized.id.to_s)
+ expect(uncategorized.reload.uncategorized?).to eq(true)
+ end
+
+ it "does nothing when the site was not using uncategorized topics at opt-in" do
+ UpcomingChangeEvent.create!(
+ event_type: :automatically_promoted,
+ upcoming_change_name: "remove_and_replace_uncategorized",
+ event_data: {
+ allow_uncategorized_topics: false,
+ default_composer_category: "",
+ uncategorized_category_id: uncategorized.id,
+ },
+ )
+ SiteSetting.uncategorized_category_id = -1
+ SiteSetting.allow_uncategorized_topics = false
+
+ described_class.call(enabled: false)
+
+ expect(SiteSetting.uncategorized_category_id).to eq(-1)
+ expect(SiteSetting.allow_uncategorized_topics).to eq(false)
+ end
+
+ it "does nothing when there is no snapshot" do
+ SiteSetting.uncategorized_category_id = -1
+ SiteSetting.allow_uncategorized_topics = false
+
+ described_class.call(enabled: false)
+
+ expect(SiteSetting.uncategorized_category_id).to eq(-1)
+ expect(SiteSetting.allow_uncategorized_topics).to eq(false)
+ end
+ end
+
+ describe ".should_display_upcoming_change?" do
+ it "is displayed while the site allows uncategorized topics" do
+ SiteSetting.allow_uncategorized_topics = true
+ expect(described_class.should_display_upcoming_change?).to eq(true)
+ end
+
+ it "is hidden once uncategorized topics are disallowed and the change is off" do
+ SiteSetting.allow_uncategorized_topics = false
+ expect(described_class.should_display_upcoming_change?).to eq(false)
+ end
+
+ it "stays displayed after the change is enabled even though uncategorized is disallowed" do
+ SiteSetting.allow_uncategorized_topics = false
+ SiteSetting.remove_and_replace_uncategorized = true
+ expect(described_class.should_display_upcoming_change?).to eq(true)
+ end
+ end
+
+ describe "hidden legacy settings" do
+ # Declared via `hide_settings:` in the upcoming change metadata
+ # (config/site_settings.yml) and resolved live by the framework.
+ let(:legacy_settings) { %i[allow_uncategorized_topics suppress_uncategorized_badge] }
+
+ it "hides the legacy uncategorized settings only while the change is enabled" do
+ expect(SiteSetting.hidden_settings).not_to include(*legacy_settings)
+
+ SiteSetting.remove_and_replace_uncategorized = true
+
+ expect(SiteSetting.hidden_settings).to include(*legacy_settings)
+ end
+ end
+end
diff --git a/spec/services/upcoming_changes/notify_promotion_spec.rb b/spec/services/upcoming_changes/notify_promotion_spec.rb
index 5ebf031969c..de25649a5b9 100644
--- a/spec/services/upcoming_changes/notify_promotion_spec.rb
+++ b/spec/services/upcoming_changes/notify_promotion_spec.rb
@@ -162,6 +162,29 @@ RSpec.describe UpcomingChanges::NotifyPromotion do
}.by(1)
end
+ it "creates an automatically_promoted event recording the promotion" do
+ expect { result }.to change {
+ UpcomingChangeEvent.where(
+ event_type: :automatically_promoted,
+ upcoming_change_name: :enable_upload_debug_mode,
+ ).count
+ }.by(1)
+ end
+
+ it "does not create a duplicate automatically_promoted event when one already exists" do
+ UpcomingChangeEvent.create!(
+ event_type: :automatically_promoted,
+ upcoming_change_name: :enable_upload_debug_mode,
+ )
+
+ expect { result }.not_to change {
+ UpcomingChangeEvent.where(
+ event_type: :automatically_promoted,
+ upcoming_change_name: :enable_upload_debug_mode,
+ ).count
+ }
+ end
+
it "triggers DiscourseEvent for the promoted setting" do
expect(event[:params]).to eq([:enable_upload_debug_mode])
end