A platform for community discussion. Free, open, simple.
DEV: Add modifier for default_navigation_menu_categories (#39354)
Gabriel Grubba6e294019a114
diff --git a/app/models/user.rb b/app/models/user.rbindex f68ba9b8d4d..c8e0f192dd4 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -2217,6 +2217,12 @@ class User < ActiveRecord::Baseif SiteSetting.default_navigation_menu_categories.present?categories_to_update = SiteSetting.default_navigation_menu_categories.split("|")+ categories_to_update =+ DiscoursePluginRegistry.apply_modifier(+ :default_navigation_categories,+ categories_to_update,+ user: self,+ )SidebarSectionLinksUpdater.update_category_section_links(self,diff --git a/frontend/discourse/app/components/sidebar/anonymous/categories-section.gjs b/frontend/discourse/app/components/sidebar/anonymous/categories-section.gjsindex 5244c35627e..e53367b7d6d 100644--- a/frontend/discourse/app/components/sidebar/anonymous/categories-section.gjs+++ b/frontend/discourse/app/components/sidebar/anonymous/categories-section.gjs@@ -1,3 +1,4 @@+import { applyValueTransformer } from "discourse/lib/transformer";import Category from "discourse/models/category";import { i18n } from "discourse-i18n";import AllCategoriesSectionLink from "../common/all-categories-section-link";@@ -6,19 +7,24 @@ import Section from "../section";import SectionLink from "../section-link";export default class SidebarAnonymousCategoriesSection extends SidebarCommonCategoriesSection {- shouldSortCategoriesByDefault =- !!this.siteSettings.default_navigation_menu_categories;+ shouldSortCategoriesByDefault = this.#defaultCategoryIds().length > 0;get categories() {- if (this.siteSettings.default_navigation_menu_categories) {- return Category.findByIds(- this.siteSettings.default_navigation_menu_categories- .split("|")- .map((categoryId) => parseInt(categoryId, 10))- );- } else {- return this.topSiteCategories;+ const ids = this.#defaultCategoryIds();+ if (ids.length) {+ return Category.findByIds(ids);}+ return this.topSiteCategories;+ }++ #defaultCategoryIds() {+ const raw = this.siteSettings.default_navigation_menu_categories;+ const initial = raw ? raw.split("|").map((id) => parseInt(id, 10)) : [];++ return applyValueTransformer(+ "sidebar-anonymous-default-categories",+ initial+ );}<template>diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.jsindex a8b8332bd22..91c31f4d0e5 100644--- a/frontend/discourse/app/lib/registry/transformers.js+++ b/frontend/discourse/app/lib/registry/transformers.js@@ -102,6 +102,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["poster-name-user-title","preferences-save-attributes","quote-params",+ "sidebar-anonymous-default-categories","small-user-attrs","tag-separator","topic-list-class",diff --git a/frontend/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.js b/frontend/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.jsindex 0dcf189f5c3..09ef3c7bba5 100644--- a/frontend/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.js+++ b/frontend/discourse/tests/acceptance/sidebar-anonymous-categories-section-test.js@@ -1,5 +1,6 @@import { visit } from "@ember/test-helpers";import { test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";import Site from "discourse/models/site";import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers";@@ -76,6 +77,112 @@ acceptance("Sidebar - Anonymous - Categories Section", function (needs) {.exists("all categories link is visible");});+ test("sidebar-anonymous-default-categories value transformer overrides the category IDs used in the sidebar", async function (assert) {+ this.siteSettings.fixed_category_positions = true;+ this.siteSettings.default_navigation_menu_categories = "1";++ withPluginApi((api) => {+ api.registerValueTransformer(+ "sidebar-anonymous-default-categories",+ () => [3, 13]+ );+ });++ await visit("/");++ assert+ .dom(+ ".sidebar-section[data-section-name='categories'] .sidebar-section-link-wrapper[data-category-id='3']"+ )+ .exists("category 3 (from the override) is in the sidebar");+ assert+ .dom(+ ".sidebar-section[data-section-name='categories'] .sidebar-section-link-wrapper[data-category-id='13']"+ )+ .exists("category 13 (from the override) is in the sidebar");+ assert+ .dom(+ ".sidebar-section[data-section-name='categories'] .sidebar-section-link-wrapper[data-category-id='1']"+ )+ .doesNotExist(+ "category 1 (from the original setting) is replaced by the override"+ );+ });++ test("sidebar-anonymous-default-categories value transformer receives the ids parsed from default_navigation_menu_categories", async function (assert) {+ this.siteSettings.default_navigation_menu_categories = "1|3";++ let received;++ withPluginApi((api) => {+ api.registerValueTransformer(+ "sidebar-anonymous-default-categories",+ ({ value }) => {+ received = value;+ return value;+ }+ );+ });++ await visit("/");++ assert.deepEqual(received, [1, 3]);+ });++ test("sidebar-anonymous-default-categories value transformer appends a category to the ids received from default_navigation_menu_categories", async function (assert) {+ this.siteSettings.default_navigation_menu_categories = "1";++ let received;++ withPluginApi((api) => {+ api.registerValueTransformer(+ "sidebar-anonymous-default-categories",+ ({ value }) => {+ received = value;+ return [...value, 3];+ }+ );+ });++ await visit("/");++ assert.deepEqual(+ received,+ [1],+ "transformer receives the ids parsed from the site setting"+ );+ assert+ .dom(+ ".sidebar-section[data-section-name='categories'] .sidebar-section-link-wrapper[data-category-id='1']"+ )+ .exists("category 1 (from the original setting) is in the sidebar");+ assert+ .dom(+ ".sidebar-section[data-section-name='categories'] .sidebar-section-link-wrapper[data-category-id='3']"+ )+ .exists("category 3 (appended by the transformer) is in the sidebar");+ });++ test("sidebar-anonymous-default-categories value transformer can fall back to top site categories by returning an empty array", async function (assert) {+ this.siteSettings.default_navigation_menu_categories = "1|3";++ withPluginApi((api) => {+ api.registerValueTransformer(+ "sidebar-anonymous-default-categories",+ () => []+ );+ });++ await visit("/");++ assert+ .dom(".sidebar-section[data-section-name='categories']")+ .exists("categories section is still rendered");+ assert+ .dom("a.sidebar-section-link[data-link-name='all-categories']")+ .exists("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;diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex 676a64bc1ee..79d51f5cd5b 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -121,6 +121,50 @@ RSpec.describe User doexpect(SidebarSectionLink.exists?(linkable_type: "Category", user_id: user.id)).to eq(true)expect(SidebarSectionLink.exists?(linkable_type: "Tag", user_id: user.id)).to eq(false)end++ describe "the :default_navigation_categories modifier" do+ fab!(:alternate_category, :category)++ let!(:plugin) { Plugin::Instance.new }+ let!(:modifier) { :default_navigation_categories }++ it "lets plugins override the category ids used to seed the sidebar" do+ block =+ Proc.new do |_default_categories, opts|+ expect(opts[:user]).to be_a(User)+ [alternate_category.id.to_s]+ end++ DiscoursePluginRegistry.register_modifier(plugin, modifier, &block)++ user = Fabricate(:user)++ expect(+ SidebarSectionLink.where(linkable_type: "Category", user_id: user.id).pluck(+ :linkable_id,+ ),+ ).to contain_exactly(alternate_category.id)+ ensure+ DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &block)+ end++ it "passes the user to the modifier so plugins can branch on user attributes" do+ received_users = []+ block =+ Proc.new do |default_categories, opts|+ received_users << opts[:user]+ default_categories+ end++ DiscoursePluginRegistry.register_modifier(plugin, modifier, &block)++ user = Fabricate(:user)++ expect(received_users).to contain_exactly(user)+ ensure+ DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &block)+ end+ endenddescribe "#change_display_name" do