Repository navigation
Categories: pages carry which types each category is, not each type's form #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Every page preloads all the categories it may show, and each one carried | ||
| # the full definition of every type it is (category_types): title, | ||
| # description, every field of its settings form with labels and defaults, | ||
| # about 3.8 KB per category, so 113 KB on each page with 47 categories. | ||
| # Outside the category editor the browser only asks which types a category | ||
| # is (Category#isType, a plugin's `categoryTypes.documentation`), and shows | ||
| # a type's name and icon. The editor loads the category again through | ||
| # /c/:slug/find_by_slug.json (CategorySerializer), which keeps everything. | ||
| module DiscourseLeanSiteData | ||
| # What a type keeps in the preload | ||
| TYPE_KEYS = %i[id name title icon svg_icon].freeze | ||
|
|
||
| def self.enabled? | ||
| SiteSetting.jtech_enabled && SiteSetting.jtech_lean_category_types | ||
| end | ||
|
|
||
| def self.lean_types(types) | ||
| return types unless types.is_a?(Hash) | ||
| types.transform_values { |type| type.is_a?(Hash) ? type.slice(*TYPE_KEYS) : type } | ||
| end | ||
|
|
||
| module SiteCategorySerializerExtension | ||
| def category_types | ||
| types = super | ||
| # Only the site's own list: CategorySerializer (the editor's) and the | ||
| # admin one inherit from this and keep the full definitions | ||
| return types unless instance_of?(::SiteCategorySerializer) && DiscourseLeanSiteData.enabled? | ||
| DiscourseLeanSiteData.lean_types(types) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| # The categories every page preloads say which types they are, without each | ||
| # type's full definition; the category editor still gets all of it. | ||
| RSpec.describe "Category types in the site data" do | ||
| fab!(:admin) | ||
| fab!(:category) | ||
|
|
||
| before do | ||
| SiteSetting.jtech_enabled = true | ||
| SiteSetting.enable_simplified_category_creation = true | ||
| Site.clear_cache | ||
| end | ||
|
|
||
| def site_types(id) | ||
| get "/site.json" | ||
| expect(response.status).to eq(200) | ||
| response.parsed_body["categories"].find { it["id"] == id }["category_types"] | ||
| end | ||
|
|
||
| it "sends each type's id, name, title and icon only" do | ||
| types = site_types(category.id) | ||
| expect(types.keys).to include("discussion") | ||
| expect(types["discussion"].keys).to match_array(%w[id name title icon svg_icon]) | ||
| expect(types["discussion"]["id"]).to eq("discussion") | ||
| end | ||
|
|
||
| it "still gives the category editor the full definitions" do | ||
| sign_in(admin) | ||
| get "/c/#{category.slug}/find_by_slug.json" | ||
| expect(response.status).to eq(200) | ||
| expect(response.parsed_body.dig("category", "category_types", "discussion")).to include( | ||
| "configuration_schema", | ||
| "description", | ||
| ) | ||
| end | ||
|
|
||
| it "sends the full definitions when switched off" do | ||
| site_types(category.id) | ||
| SiteSetting.jtech_lean_category_types = false | ||
| expect(site_types(category.id)["discussion"]).to include("configuration_schema") | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
| # Jtech sub-plugin: less in the data every page preloads, where the browser | ||
| # never reads it there (lib/discourse_lean_site_data.rb). Loaded by | ||
| # Jtech/plugin.rb in the Plugin::Instance context. | ||
|
|
||
| require_relative "../lib/discourse_lean_site_data" | ||
|
|
||
| after_initialize do | ||
| reloadable_patch do | ||
| ::SiteCategorySerializer.prepend(DiscourseLeanSiteData::SiteCategorySerializerExtension) | ||
| end | ||
|
|
||
| # The site's category list is serialized once and cached | ||
| on(:site_setting_changed) do |name, _old, _new| | ||
| Site.clear_cache if name.to_s == "jtech_lean_category_types" | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an admin changes
jtech_enabled, this handler does not invalidate the serialized category cache: enabling the plugin reaches the callback but fails this name check, while disabling it prevents the plugin-gatedoncallback from running at all. Consequently, a populated cache can continue serving either full or stripped type definitions for up to its 30-minute lifetime, so the master switch does not take effect promptly. Use an ungated enabled-change handler and clearSite's cache for both switches.AGENTS.md reference: AGENTS.md:L21-L21
Useful? React with 👍 / 👎.