A platform for community discussion. Free, open, simple.
FIX: Delete the unused seeded Uncategorized category instead of exposing it
Régis Hanola9f8c119c714
diff --git a/app/jobs/onceoff/remove_unused_uncategorized_category.rb b/app/jobs/onceoff/remove_unused_uncategorized_category.rbnew file mode 100644index 00000000000..d725eceef38--- /dev/null+++ b/app/jobs/onceoff/remove_unused_uncategorized_category.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++module Jobs+ class RemoveUnusedUncategorizedCategory < ::Jobs::Onceoff+ def execute_onceoff(args)+ action = SiteSetting::Action::RemoveAndReplaceUncategorizedToggled+ change = action::UPCOMING_CHANGE+ return if !UpcomingChanges.exists?(change) || !UpcomingChanges.enabled?(change)++ action.call(enabled: true)+ end+ end+enddiff --git a/app/services/site_setting/action/remove_and_replace_uncategorized_toggled.rb b/app/services/site_setting/action/remove_and_replace_uncategorized_toggled.rbindex 38787f82ceb..15202d81f3a 100644--- a/app/services/site_setting/action/remove_and_replace_uncategorized_toggled.rb+++ b/app/services/site_setting/action/remove_and_replace_uncategorized_toggled.rb@@ -4,19 +4,9 @@# 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 exemption from definition topics moves-# from the setting onto the category itself.-## 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.+# so that opt-out can restore the special category, and a category the site+# never exposed can be removed.class SiteSetting::Action::RemoveAndReplaceUncategorizedToggled < Service::ActionBaseUPCOMING_CHANGE = :remove_and_replace_uncategorized@@ -36,86 +26,95 @@ class SiteSetting::Action::RemoveAndReplaceUncategorizedToggled < Service::Actioprivatedef enable- # -1 is the canonical "already migrated" marker, so this is idempotent.- return if SiteSetting.uncategorized_category_id == -1+ demote if SiteSetting.uncategorized_category_id != -1+ remove_unused_category+ end- former_uncategorized_id = SiteSetting.uncategorized_category_id+ def demote+ event = snapshot_events.last+ return if event.nil?- 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,- )+ allow_uncategorized_topics = SiteSetting.allow_uncategorized_topics+ default_composer_category = SiteSetting.default_composer_category+ uncategorized_category_id = SiteSetting.uncategorized_category_id- if SiteSetting.default_composer_category.blank?- SiteSetting.set_and_log(:default_composer_category, former_uncategorized_id.to_s)+ ActiveRecord::Base.transaction do+ if stored_snapshot.blank?+ event.update!(+ event_data: {+ allow_uncategorized_topics:,+ default_composer_category:,+ uncategorized_category_id:,+ },+ )end- SiteSetting.set_and_log(:uncategorized_category_id, -1)- SiteSetting.set_and_log(:allow_uncategorized_topics, false)+ if allow_uncategorized_topics && default_composer_category.blank?+ SiteSetting.set_and_log(:default_composer_category, uncategorized_category_id.to_s)+ end# Definition topics were skipped for the special category only by virtue# of `uncategorized_category_id` pointing at it, so move the exemption# onto the category before it stops matching.- Category.find_by(id: former_uncategorized_id)&.upsert_custom_fields(+ Category.find_by(id: uncategorized_category_id)&.upsert_custom_fields(Category::SKIP_DEFINITION_CUSTOM_FIELD => true,)++ SiteSetting.set_and_log(:uncategorized_category_id, -1)+ SiteSetting.set_and_log(:allow_uncategorized_topics, false)endSite.clear_cacheend+ def remove_unused_category+ snapshot = stored_snapshot+ return if snapshot.blank? || snapshot["allow_uncategorized_topics"]++ category = Category.find_by(id: snapshot["uncategorized_category_id"])+ guardian = Discourse.system_user.guardian+ return if category.nil? || !guardian.can_delete_category?(category) || !pristine?(category)++ CategoryDestroyer.destroy(guardian, category)+ end++ def pristine?(category)+ category.name == I18n.t("uncategorized_category_name", locale: SiteSetting.default_locale) &&+ category.category_groups.none? && !category.topics.with_deleted.exists?+ end+def disable- snapshot = read_snapshot- return if snapshot.blank?+ snapshot = stored_snapshot+ return if snapshot.blank? || !snapshot["allow_uncategorized_topics"]- # If the site was not using uncategorized topics at opt-in, there is no- # special category to restore.- return unless snapshot["allow_uncategorized_topics"]+ category = Category.find_by(id: snapshot["uncategorized_category_id"])+ return if category.nil?ActiveRecord::Base.transaction do- SiteSetting.set_and_log(:uncategorized_category_id, snapshot["uncategorized_category_id"])+ SiteSetting.set_and_log(:uncategorized_category_id, 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+ SiteSetting.set_and_log(+ :default_composer_category,+ snapshot["default_composer_category"].to_s,+ )# The category is special again, so the setting covers it once more and# the exemption returns to being absent rather than explicitly false.- CategoryCustomField.where(- category_id: snapshot["uncategorized_category_id"],- name: Category::SKIP_DEFINITION_CUSTOM_FIELD,- ).delete_all+ category.custom_fields.delete(Category::SKIP_DEFINITION_CUSTOM_FIELD)+ category.save_custom_fieldsendSite.clear_cacheend- # 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+ def stored_snapshot+ snapshot_events.where.not(event_data: nil).last&.event_dataend- 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+ def snapshot_events+ UpcomingChangeEvent.where(+ upcoming_change_name: UPCOMING_CHANGE,+ event_type: SNAPSHOT_EVENT_TYPES,+ )endenddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 8e1f7a220ce..8f3c2ac6408 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -3303,6 +3303,7 @@ en:discourse_connect_cannot_be_enabled_if_second_factor_enforced: "You cannot enable DiscourseConnect if 2FA is enforced."delete_rejected_email_after_days: "This setting cannot be set lower than the delete_email_logs_after_days setting or greater than %{max}"invalid_uncategorized_category_setting: 'The "Uncategorized" category cannot be selected if ''allow uncategorized topics'' is not enabled.'+ uncategorized_category_missing: "'allow uncategorized topics' cannot be enabled because the \"Uncategorized\" category no longer exists."invalid_search_ranking_weights: "Value is invalid for search_ranking_weights site setting. Example: '{0.1,0.2,0.3,1.0}'. Note that maximum value for each weight is 1.0."content_localization_locale_limit: "The number of supported locales cannot exceed %{max}."content_localization_language_switcher_requirements: "The language switcher requires {{setting:allow_user_locale}} to be enabled, and {{setting:content_localization_supported_locales}} to have at least one language. {{settings:content_localization_language_switcher,allow_user_locale,content_localization_supported_locales|All required settings}}"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex cb75f43c039..3d6b2faa370 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1425,6 +1425,7 @@ posting:client: truedefault: falserefresh: true+ validator: "AllowUncategorizedTopicsValidator"area: "posts_and_topics"remove_and_replace_uncategorized:default: falsediff --git a/lib/validators/allow_uncategorized_topics_validator.rb b/lib/validators/allow_uncategorized_topics_validator.rbnew file mode 100644index 00000000000..4e9de55ee26--- /dev/null+++ b/lib/validators/allow_uncategorized_topics_validator.rb@@ -0,0 +1,15 @@+# frozen_string_literal: true++class AllowUncategorizedTopicsValidator+ def initialize(opts = {})+ @opts = opts+ end++ def valid_value?(val)+ val == "f" || Category.exists?(SiteSetting.uncategorized_category_id)+ end++ def error_message+ I18n.t("site_settings.errors.uncategorized_category_missing")+ end+enddiff --git a/spec/jobs/remove_unused_uncategorized_category_spec.rb b/spec/jobs/remove_unused_uncategorized_category_spec.rbnew file mode 100644index 00000000000..232093e0b29--- /dev/null+++ b/spec/jobs/remove_unused_uncategorized_category_spec.rb@@ -0,0 +1,32 @@+# frozen_string_literal: true++RSpec.describe Jobs::RemoveUnusedUncategorizedCategory do+ let!(:uncategorized) { Category.find(SiteSetting.uncategorized_category_id) }++ before do+ UpcomingChangeEvent.create!(+ event_type: :automatically_promoted,+ upcoming_change_name:+ SiteSetting::Action::RemoveAndReplaceUncategorizedToggled::UPCOMING_CHANGE,+ event_data: {+ allow_uncategorized_topics: false,+ uncategorized_category_id: uncategorized.id,+ },+ )+ SiteSetting.uncategorized_category_id = -1+ end++ it "removes the category demoted on a site that was not allowing uncategorized topics" do+ described_class.new.execute_onceoff({})++ expect(Category.exists?(uncategorized.id)).to eq(false)+ end++ it "does nothing when the change is opted out of" do+ SiteSetting.remove_and_replace_uncategorized = false++ described_class.new.execute_onceoff({})++ expect(Category.exists?(uncategorized.id)).to eq(true)+ end+enddiff --git a/spec/lib/validators/allow_uncategorized_topics_validator_spec.rb b/spec/lib/validators/allow_uncategorized_topics_validator_spec.rbnew file mode 100644index 00000000000..b362ebb91ef--- /dev/null+++ b/spec/lib/validators/allow_uncategorized_topics_validator_spec.rb@@ -0,0 +1,14 @@+# frozen_string_literal: true++RSpec.describe AllowUncategorizedTopicsValidator do+ subject(:validator) { described_class.new }++ it "only allows enabling while the uncategorized category exists" do+ expect(validator.valid_value?("t")).to eq(true)++ SiteSetting.uncategorized_category_id = -1++ expect(validator.valid_value?("t")).to eq(false)+ expect(validator.valid_value?("f")).to eq(true)+ end+enddiff --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.rbindex be103f400c7..d129269b99b 100644--- 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@@ -1,141 +1,136 @@# frozen_string_literal: trueRSpec.describe SiteSetting::Action::RemoveAndReplaceUncategorizedToggled do- fab!(:uncategorized) { Fabricate(:category, name: "Special category") }+ let!(:uncategorized) { Category.find(SiteSetting.uncategorized_category_id) }++ def create_event(event_type, event_data = nil)+ UpcomingChangeEvent.create!(+ event_type:,+ upcoming_change_name: described_class::UPCOMING_CHANGE,+ event_data:,+ )+ end- before do- SiteSetting.uncategorized_category_id = uncategorized.id- SiteSetting.allow_uncategorized_topics = true+ def snapshot(allow_uncategorized_topics:, default_composer_category: "")+ {+ allow_uncategorized_topics:,+ default_composer_category:,+ uncategorized_category_id: uncategorized.id,+ }enddescribe "enabling" do- it "demotes the special category in place and disallows uncategorized topics" do+ let!(:event) { create_event(:automatically_promoted) }++ it "demotes the special category in place, pins it as the composer default and exempts it from definition topics" dodescribed_class.call(enabled: true)expect(SiteSetting.uncategorized_category_id).to eq(-1)expect(SiteSetting.allow_uncategorized_topics).to eq(false)+ expect(SiteSetting.default_composer_category).to eq(uncategorized.id.to_s)expect(uncategorized.reload.uncategorized?).to eq(false)+ expect(uncategorized.custom_fields[Category::SKIP_DEFINITION_CUSTOM_FIELD]).to eq(true)end- it "exempts the demoted category from being backfilled a definition topic" do+ it "does nothing without an opt-in or promotion event" do+ event.destroy!+described_class.call(enabled: true)- Category.ensure_consistency!- expect(uncategorized.reload.topic_id).to eq(nil)+ expect(SiteSetting.uncategorized_category_id).to eq(uncategorized.id)end- it "leaves default_composer_category pointing at the now-normal category" do- SiteSetting.default_composer_category = uncategorized.id.to_s+ it "leaves an explicit default_composer_category untouched" do+ other = Fabricate(:category)+ SiteSetting.default_composer_category = other.id.to_sdescribed_class.call(enabled: true)- expect(SiteSetting.default_composer_category).to eq(uncategorized.id.to_s)+ expect(SiteSetting.default_composer_category).to eq(other.id.to_s)end- it "pins the now-normal category as the composer default when none was set" do- SiteSetting.default_composer_category = ""+ it "snapshots the prior state onto the event, ignoring other events' data" do+ create_event(:status_changed, { previous_value: nil, new_value: "experimental" })described_class.call(enabled: true)- expect(SiteSetting.default_composer_category).to eq(uncategorized.id.to_s)+ expect(event.reload.event_data).to eq(+ "allow_uncategorized_topics" => true,+ "default_composer_category" => "",+ "uncategorized_category_id" => uncategorized.id,+ )end- it "leaves an unrelated default_composer_category untouched" do- other_category = Fabricate(:category)- SiteSetting.default_composer_category = other_category.id.to_s+ it "keeps the original snapshot on a later opt-in" do+ event.update!(event_data: snapshot(allow_uncategorized_topics: true))+ reopt_in = create_event(:manual_opt_in)described_class.call(enabled: true)- expect(SiteSetting.default_composer_category).to eq(other_category.id.to_s)+ expect(reopt_in.reload.event_data).to eq(nil)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",- )+ it "keeps a category holding even a trashed topic, without pinning it as the composer default" do+ Fabricate(:topic, category: uncategorized).trash!+ SiteSetting.allow_uncategorized_topics = falsedescribed_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,- )+ expect(Category.exists?(uncategorized.id)).to eq(true)+ expect(SiteSetting.default_composer_category).to eq("")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",- )+ context "when the site was not allowing uncategorized topics" do+ before { SiteSetting.allow_uncategorized_topics = false }- described_class.call(enabled: true)+ it "removes the unused category instead of exposing it" do+ 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+ expect(Category.exists?(uncategorized.id)).to eq(false)+ 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",- )+ it "keeps a category an admin has renamed" do+ uncategorized.update!(name: "Lounge")- described_class.call(enabled: true)+ described_class.call(enabled: true)- expect(opt_in.reload.event_data).to include("uncategorized_category_id" => uncategorized.id)- end+ expect(Category.exists?(uncategorized.id)).to eq(true)+ 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+ it "keeps a category whose permissions were changed" do+ Fabricate(:category_group, category: uncategorized, group: Group[:staff])- # Simulate drift that should be ignored because the change is already applied.- SiteSetting.uncategorized_category_id = -1- described_class.call(enabled: true)+ described_class.call(enabled: true)++ expect(Category.exists?(uncategorized.id)).to eq(true)+ end++ it "keeps a category that has subcategories" do+ event.update!(event_data: snapshot(allow_uncategorized_topics: false))+ SiteSetting.uncategorized_category_id = -1+ Fabricate(:category, parent_category: uncategorized)- expect(UpcomingChangeEvent.count).to eq(original_event_count)- expect(UpcomingChangeEvent.last.event_data).to eq(snapshot)+ described_class.call(enabled: true)++ expect(Category.exists?(uncategorized.id)).to eq(true)+ endendenddescribe "disabling" do+ before do+ SiteSetting.uncategorized_category_id = -1+ SiteSetting.allow_uncategorized_topics = false+ end+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: {+ create_event(+ :automatically_promoted,+ snapshot(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 = ""uncategorized.upsert_custom_fields(Category::SKIP_DEFINITION_CUSTOM_FIELD => true)described_class.call(enabled: false)@@ -143,72 +138,45 @@ RSpec.describe SiteSetting::Action::RemoveAndReplaceUncategorizedToggled doexpect(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)- expect(uncategorized.custom_fields).not_to include(Category::SKIP_DEFINITION_CUSTOM_FIELD)+ expect(uncategorized.reload.custom_fields).not_to include(+ Category::SKIP_DEFINITION_CUSTOM_FIELD,+ )endit "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+ create_event(:automatically_promoted, snapshot(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)endit "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)+ end++ it "does nothing when the demoted category has since been deleted" do+ create_event(:automatically_promoted, snapshot(allow_uncategorized_topics: true))+ uncategorized.deletedescribed_class.call(enabled: false)expect(SiteSetting.uncategorized_category_id).to eq(-1)- expect(SiteSetting.allow_uncategorized_topics).to eq(false)endenddescribe ".should_display_upcoming_change?" do- it "is displayed while the site allows uncategorized topics" do- SiteSetting.allow_uncategorized_topics = true+ it "is displayed while uncategorized topics are allowed or the change is enabled" doexpect(described_class.should_display_upcoming_change?).to eq(true)- end- it "is hidden once uncategorized topics are disallowed and the change is off" doSiteSetting.allow_uncategorized_topics = falseSiteSetting.remove_and_replace_uncategorized = falseexpect(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 = falseSiteSetting.remove_and_replace_uncategorized = trueexpect(described_class.should_display_upcoming_change?).to eq(true)endend-- 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- SiteSetting.remove_and_replace_uncategorized = false-- expect(SiteSetting.hidden_settings).not_to include(*legacy_settings)-- SiteSetting.remove_and_replace_uncategorized = true-- expect(SiteSetting.hidden_settings).to include(*legacy_settings)- end- endenddiff --git a/spec/services/upcoming_changes/notify_promotion_spec.rb b/spec/services/upcoming_changes/notify_promotion_spec.rbindex 464c277e60e..a36ce67f916 100644--- a/spec/services/upcoming_changes/notify_promotion_spec.rb+++ b/spec/services/upcoming_changes/notify_promotion_spec.rb@@ -60,6 +60,24 @@ RSpec.describe UpcomingChanges::NotifyPromotion doit { is_expected.to fail_a_policy(:meets_or_exceeds_status) }end+ context "when remove_and_replace_uncategorized is promoted" do+ let!(:uncategorized) { Category.find(SiteSetting.uncategorized_category_id) }+ let(:setting_name) { :remove_and_replace_uncategorized }+ let(:changes_already_notified_about_promotion) do+ UpcomingChangeEvent.change_names_with_event(:admins_notified_automatic_promotion)+ end++ it "snapshots the site state before dispatching the change" do+ expect(result).to run_successfully+ expect(+ UpcomingChangeEvent.find_by(+ event_type: :automatically_promoted,+ upcoming_change_name: setting_name,+ ).event_data,+ ).to include("uncategorized_category_id" => uncategorized.id)+ end+ end+context "when the change is owned by a plugin that is not configurable" dolet(:setting_name) { :enable_experimental_sample_plugin_feature }