diff --git a/app/assets/javascripts/discourse/app/components/sidebar/anonymous/categories-section.hbs b/app/assets/javascripts/discourse/app/components/sidebar/anonymous/categories-section.hbs index fe97ac63c96..c59afa73c53 100644 --- a/app/assets/javascripts/discourse/app/components/sidebar/anonymous/categories-section.hbs +++ b/app/assets/javascripts/discourse/app/components/sidebar/anonymous/categories-section.hbs @@ -5,6 +5,7 @@ {{#each this.sectionLinks as |sectionLink|}} !category.parent_category_id) + .filter( + (category) => + canDisplayCategory(category, this.siteSettings) && + !category.parent_category_id + ) .slice(0, 5); } diff --git a/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.js b/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.js index 71b670f7280..c7b95dc9a43 100644 --- a/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.js +++ b/app/assets/javascripts/discourse/app/components/sidebar/user/categories-section.js @@ -6,11 +6,13 @@ import { action } from "@ember/object"; import Component from "@glimmer/component"; import CategorySectionLink from "discourse/lib/sidebar/user/categories-section/category-section-link"; +import { canDisplayCategory } from "discourse/lib/sidebar/helpers"; export default class SidebarUserCategoriesSection extends Component { @service router; @service topicTrackingState; @service currentUser; + @service siteSettings; constructor() { super(...arguments); @@ -30,7 +32,11 @@ export default class SidebarUserCategoriesSection extends Component { get sectionLinks() { const links = []; - for (const category of this.currentUser.sidebarCategories) { + const categories = this.currentUser.sidebarCategories.filter((category) => { + return canDisplayCategory(category, this.siteSettings); + }); + + for (const category of categories) { links.push( new CategorySectionLink({ category, diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/helpers.js b/app/assets/javascripts/discourse/app/lib/sidebar/helpers.js new file mode 100644 index 00000000000..d7dd1e6014f --- /dev/null +++ b/app/assets/javascripts/discourse/app/lib/sidebar/helpers.js @@ -0,0 +1,7 @@ +export function canDisplayCategory(category, siteSettings) { + if (siteSettings.allow_uncategorized_topics) { + return true; + } + + return !category.isUncategorizedCategory; +} diff --git a/app/assets/javascripts/discourse/app/models/site.js b/app/assets/javascripts/discourse/app/models/site.js index 62ea5a98148..8e9d9c86476 100644 --- a/app/assets/javascripts/discourse/app/models/site.js +++ b/app/assets/javascripts/discourse/app/models/site.js @@ -90,8 +90,8 @@ const Site = RestModel.extend({ for (const category of categories) { if (category.isTracked) { if ( - !this.siteSettings.suppress_uncategorized_badge || - category.id !== this.uncategorized_category_id + this.siteSettings.allow_uncategorized_topics || + !category.isUncategorizedCategory ) { trackedCategories.push(category); } diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js index de55bd393ca..72b89785a33 100644 --- a/app/assets/javascripts/discourse/app/models/user.js +++ b/app/assets/javascripts/discourse/app/models/user.js @@ -334,16 +334,9 @@ const User = RestModel.extend({ } return Site.current() - .categoriesList.filter((category) => { - if ( - this.siteSettings.suppress_uncategorized_badge && - category.isUncategorizedCategory - ) { - return false; - } - - return sidebarCategoryIds.includes(category.id); - }) + .categoriesList.filter((category) => + sidebarCategoryIds.includes(category.id) + ) .sort((a, b) => a.name.localeCompare(b.name)); }, diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.js index 8edb367e620..e47acedff67 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.js @@ -5,12 +5,12 @@ import { exists, queryAll, } from "discourse/tests/helpers/qunit-helpers"; +import Site from "discourse/models/site"; acceptance("Sidebar - Anonymous Categories Section", function (needs) { needs.settings({ enable_experimental_sidebar_hamburger: true, enable_sidebar: true, - suppress_uncategorized_badge: false, }); test("category section links", async function (assert) { @@ -32,7 +32,7 @@ acceptance("Sidebar - Anonymous Categories Section", function (needs) { ); }); - test("default sidebar categories", async function (assert) { + test("category section links in sidebar when default_sidebar_categories site setting has been configured", async function (assert) { this.siteSettings.default_sidebar_categories = "3|13|1"; await visit("/"); @@ -50,4 +50,25 @@ acceptance("Sidebar - Anonymous Categories Section", function (needs) { "all categories link is visible" ); }); + + test("default uncategorized category section links is not shown when allow_uncategorized_topics is disabled", async function (assert) { + this.siteSettings.allow_uncategorized_topics = false; + this.siteSettings.fixed_category_positions = true; + const site = Site.current(); + + const firstCategory = Site.current().categories.find((category) => { + return !category.parent_category_id; + }); + + site.set("uncategorized_category_id", firstCategory.id); + + await visit("/"); + + assert.notOk( + exists( + `.sidebar-section-categories .sidebar-section-link-${firstCategory.slug}` + ), + "category section link is not shown in sidebar after being marked as uncategorized" + ); + }); }); diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-categories-section-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-categories-section-test.js index 6a07958aac3..4586df9ed26 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-categories-section-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-user-categories-section-test.js @@ -17,10 +17,10 @@ import categoryFixture from "discourse/tests/fixtures/category-fixtures"; import { cloneJSON } from "discourse-common/lib/object"; acceptance( - "Sidebar - Logged on user - Categories Section - suppress_uncategorized_badge enabled", + "Sidebar - Logged on user - Categories Section - allow_uncategorized_topics disabled", function (needs) { needs.settings({ - suppress_uncategorized_badge: true, + allow_uncategorized_topics: false, enable_experimental_sidebar_hamburger: true, enable_sidebar: true, });