FIX: Skip uncategorized category in sidebar when disabled (#18324)

When `allow_uncategorized_topics` is set to `false`, we do not want to
show the uncategorized in sidebar by default.

This commit updates a couple of places in the code related to sidebar
which was incorrectly using `suppress_uncategorized_badge` site setting
which is mainly used for hiding the category badge for uncategorized
category and should not be used to determine if uncategorized categories
should be allowed or not.
This commit is contained in:
Alan Guo Xiang Tan 2022-09-23 10:20:30 +08:00 committed by GitHub
parent 03f83c0eed
commit 0c45aa7900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 18 deletions

View File

@ -5,6 +5,7 @@
{{#each this.sectionLinks as |sectionLink|}} {{#each this.sectionLinks as |sectionLink|}}
<Sidebar::SectionLink <Sidebar::SectionLink
@linkName={{sectionLink.name}}
@route={{sectionLink.route}} @route={{sectionLink.route}}
@title={{sectionLink.title}} @title={{sectionLink.title}}
@content={{sectionLink.text}} @content={{sectionLink.text}}

View File

@ -2,6 +2,7 @@ import { cached } from "@glimmer/tracking";
import { inject as service } from "@ember/service"; import { inject as service } from "@ember/service";
import Component from "@glimmer/component"; import Component from "@glimmer/component";
import CategorySectionLink from "discourse/lib/sidebar/user/categories-section/category-section-link"; import CategorySectionLink from "discourse/lib/sidebar/user/categories-section/category-section-link";
import { canDisplayCategory } from "discourse/lib/sidebar/helpers";
export default class SidebarAnonymousCategoriesSection extends Component { export default class SidebarAnonymousCategoriesSection extends Component {
@service topicTrackingState; @service topicTrackingState;
@ -22,7 +23,11 @@ export default class SidebarAnonymousCategoriesSection extends Component {
); );
} else { } else {
categories = categories categories = categories
.filter((category) => !category.parent_category_id) .filter(
(category) =>
canDisplayCategory(category, this.siteSettings) &&
!category.parent_category_id
)
.slice(0, 5); .slice(0, 5);
} }

View File

@ -6,11 +6,13 @@ import { action } from "@ember/object";
import Component from "@glimmer/component"; import Component from "@glimmer/component";
import CategorySectionLink from "discourse/lib/sidebar/user/categories-section/category-section-link"; 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 { export default class SidebarUserCategoriesSection extends Component {
@service router; @service router;
@service topicTrackingState; @service topicTrackingState;
@service currentUser; @service currentUser;
@service siteSettings;
constructor() { constructor() {
super(...arguments); super(...arguments);
@ -30,7 +32,11 @@ export default class SidebarUserCategoriesSection extends Component {
get sectionLinks() { get sectionLinks() {
const links = []; 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( links.push(
new CategorySectionLink({ new CategorySectionLink({
category, category,

View File

@ -0,0 +1,7 @@
export function canDisplayCategory(category, siteSettings) {
if (siteSettings.allow_uncategorized_topics) {
return true;
}
return !category.isUncategorizedCategory;
}

View File

@ -90,8 +90,8 @@ const Site = RestModel.extend({
for (const category of categories) { for (const category of categories) {
if (category.isTracked) { if (category.isTracked) {
if ( if (
!this.siteSettings.suppress_uncategorized_badge || this.siteSettings.allow_uncategorized_topics ||
category.id !== this.uncategorized_category_id !category.isUncategorizedCategory
) { ) {
trackedCategories.push(category); trackedCategories.push(category);
} }

View File

@ -334,16 +334,9 @@ const User = RestModel.extend({
} }
return Site.current() return Site.current()
.categoriesList.filter((category) => { .categoriesList.filter((category) =>
if ( sidebarCategoryIds.includes(category.id)
this.siteSettings.suppress_uncategorized_badge && )
category.isUncategorizedCategory
) {
return false;
}
return sidebarCategoryIds.includes(category.id);
})
.sort((a, b) => a.name.localeCompare(b.name)); .sort((a, b) => a.name.localeCompare(b.name));
}, },

View File

@ -5,12 +5,12 @@ import {
exists, exists,
queryAll, queryAll,
} from "discourse/tests/helpers/qunit-helpers"; } from "discourse/tests/helpers/qunit-helpers";
import Site from "discourse/models/site";
acceptance("Sidebar - Anonymous Categories Section", function (needs) { acceptance("Sidebar - Anonymous Categories Section", function (needs) {
needs.settings({ needs.settings({
enable_experimental_sidebar_hamburger: true, enable_experimental_sidebar_hamburger: true,
enable_sidebar: true, enable_sidebar: true,
suppress_uncategorized_badge: false,
}); });
test("category section links", async function (assert) { 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"; this.siteSettings.default_sidebar_categories = "3|13|1";
await visit("/"); await visit("/");
@ -50,4 +50,25 @@ acceptance("Sidebar - Anonymous Categories Section", function (needs) {
"all categories link is visible" "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"
);
});
}); });

View File

@ -17,10 +17,10 @@ import categoryFixture from "discourse/tests/fixtures/category-fixtures";
import { cloneJSON } from "discourse-common/lib/object"; import { cloneJSON } from "discourse-common/lib/object";
acceptance( acceptance(
"Sidebar - Logged on user - Categories Section - suppress_uncategorized_badge enabled", "Sidebar - Logged on user - Categories Section - allow_uncategorized_topics disabled",
function (needs) { function (needs) {
needs.settings({ needs.settings({
suppress_uncategorized_badge: true, allow_uncategorized_topics: false,
enable_experimental_sidebar_hamburger: true, enable_experimental_sidebar_hamburger: true,
enable_sidebar: true, enable_sidebar: true,
}); });