DEV: Make lazy_load_categories setting use groups (#25282)

This allows certain users to test the new feature and avoid disruptions
in other's workflows.
This commit is contained in:
Bianca Nenciu 2024-01-17 20:26:51 +02:00 committed by GitHub
parent 719956f255
commit abad38c2e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 79 additions and 33 deletions

View File

@ -77,7 +77,7 @@ export default class CategoriesDisplay extends Component {
} }
get canLoadMore() { get canLoadMore() {
return this.siteSettings.lazy_load_categories && this.args.loadMore; return this.site.lazy_load_categories && this.args.loadMore;
} }
<template> <template>

View File

@ -201,7 +201,7 @@ const Group = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get watchingCategories() { get watchingCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.watching_category_ids) !Category.hasAsyncFoundAll(this.watching_category_ids)
) { ) {
Category.asyncFindByIds(this.watching_category_ids).then(() => Category.asyncFindByIds(this.watching_category_ids).then(() =>
@ -222,7 +222,7 @@ const Group = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get trackingCategories() { get trackingCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.tracking_category_ids) !Category.hasAsyncFoundAll(this.tracking_category_ids)
) { ) {
Category.asyncFindByIds(this.tracking_category_ids).then(() => Category.asyncFindByIds(this.tracking_category_ids).then(() =>
@ -243,7 +243,7 @@ const Group = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get watchingFirstPostCategories() { get watchingFirstPostCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.watching_first_post_category_ids) !Category.hasAsyncFoundAll(this.watching_first_post_category_ids)
) { ) {
Category.asyncFindByIds(this.watching_first_post_category_ids).then(() => Category.asyncFindByIds(this.watching_first_post_category_ids).then(() =>
@ -264,7 +264,7 @@ const Group = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get regularCategories() { get regularCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.regular_category_ids) !Category.hasAsyncFoundAll(this.regular_category_ids)
) { ) {
Category.asyncFindByIds(this.regular_category_ids).then(() => Category.asyncFindByIds(this.regular_category_ids).then(() =>
@ -285,7 +285,7 @@ const Group = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get mutedCategories() { get mutedCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.muted_category_ids) !Category.hasAsyncFoundAll(this.muted_category_ids)
) { ) {
Category.asyncFindByIds(this.muted_category_ids).then(() => Category.asyncFindByIds(this.muted_category_ids).then(() =>

View File

@ -857,7 +857,7 @@ const User = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get mutedCategories() { get mutedCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.muted_category_ids) !Category.hasAsyncFoundAll(this.muted_category_ids)
) { ) {
Category.asyncFindByIds(this.muted_category_ids).then(() => Category.asyncFindByIds(this.muted_category_ids).then(() =>
@ -877,7 +877,7 @@ const User = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get regularCategories() { get regularCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.regular_category_ids) !Category.hasAsyncFoundAll(this.regular_category_ids)
) { ) {
Category.asyncFindByIds(this.regular_category_ids).then(() => Category.asyncFindByIds(this.regular_category_ids).then(() =>
@ -897,7 +897,7 @@ const User = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get trackedCategories() { get trackedCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.tracked_category_ids) !Category.hasAsyncFoundAll(this.tracked_category_ids)
) { ) {
Category.asyncFindByIds(this.tracked_category_ids).then(() => Category.asyncFindByIds(this.tracked_category_ids).then(() =>
@ -917,7 +917,7 @@ const User = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get watchedCategories() { get watchedCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.watched_category_ids) !Category.hasAsyncFoundAll(this.watched_category_ids)
) { ) {
Category.asyncFindByIds(this.watched_category_ids).then(() => Category.asyncFindByIds(this.watched_category_ids).then(() =>
@ -937,7 +937,7 @@ const User = RestModel.extend({
@dependentKeyCompat @dependentKeyCompat
get watchedFirstPostCategories() { get watchedFirstPostCategories() {
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll(this.watched_first_post_category_ids) !Category.hasAsyncFoundAll(this.watched_first_post_category_ids)
) { ) {
Category.asyncFindByIds(this.watched_first_post_category_ids).then(() => Category.asyncFindByIds(this.watched_first_post_category_ids).then(() =>

View File

@ -18,7 +18,7 @@ import I18n from "discourse-i18n";
class AbstractCategoryRoute extends DiscourseRoute { class AbstractCategoryRoute extends DiscourseRoute {
@service composer; @service composer;
@service router; @service router;
@service siteSettings; @service site;
@service store; @service store;
@service topicTrackingState; @service topicTrackingState;
@service("search") searchService; @service("search") searchService;
@ -30,7 +30,7 @@ class AbstractCategoryRoute extends DiscourseRoute {
controllerName = "discovery/list"; controllerName = "discovery/list";
async model(params, transition) { async model(params, transition) {
const category = this.siteSettings.lazy_load_categories const category = this.site.lazy_load_categories
? await Category.asyncFindBySlugPathWithID( ? await Category.asyncFindBySlugPathWithID(
params.category_slug_path_with_id params.category_slug_path_with_id
) )

View File

@ -29,7 +29,7 @@ export default ComboBoxComponent.extend({
this._super(...arguments); this._super(...arguments);
if ( if (
this.siteSettings.lazy_load_categories && this.site.lazy_load_categories &&
!Category.hasAsyncFoundAll([this.value]) !Category.hasAsyncFoundAll([this.value])
) { ) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
@ -92,7 +92,7 @@ export default ComboBoxComponent.extend({
}, },
search(filter) { search(filter) {
if (this.siteSettings.lazy_load_categories) { if (this.site.lazy_load_categories) {
return Category.asyncSearch(this._normalize(filter), { return Category.asyncSearch(this._normalize(filter), {
scopedCategoryId: this.selectKit.options?.scopedCategoryId, scopedCategoryId: this.selectKit.options?.scopedCategoryId,
prioritizedCategoryId: this.selectKit.options?.prioritizedCategoryId, prioritizedCategoryId: this.selectKit.options?.prioritizedCategoryId,

View File

@ -135,7 +135,7 @@ export default ComboBoxComponent.extend({
), ),
async search(filter) { async search(filter) {
if (this.siteSettings.lazy_load_categories) { if (this.site.lazy_load_categories) {
const results = await Category.asyncSearch(filter, { const results = await Category.asyncSearch(filter, {
parentCategoryId: this.options.parentCategory?.id || -1, parentCategoryId: this.options.parentCategory?.id || -1,
includeUncategorized: this.siteSettings.allow_uncategorized_topics, includeUncategorized: this.siteSettings.allow_uncategorized_topics,

View File

@ -51,7 +51,7 @@ export default MultiSelectComponent.extend({
}, },
async search(filter) { async search(filter) {
if (!this.siteSettings.lazy_load_categories) { if (!this.site.lazy_load_categories) {
return this._super(filter); return this._super(filter);
} }

View File

@ -137,7 +137,7 @@ class CategoryList
query = self.class.order_categories(query) query = self.class.order_categories(query)
if SiteSetting.lazy_load_categories if @guardian.can_lazy_load_categories?
page = [1, @options[:page].to_i].max page = [1, @options[:page].to_i].max
query = query =
query query
@ -174,7 +174,7 @@ class CategoryList
notification_levels = CategoryUser.notification_levels_for(@guardian.user) notification_levels = CategoryUser.notification_levels_for(@guardian.user)
default_notification_level = CategoryUser.default_notification_level default_notification_level = CategoryUser.default_notification_level
if SiteSetting.lazy_load_categories if @guardian.can_lazy_load_categories?
subcategory_ids = {} subcategory_ids = {}
Category Category
.secured(@guardian) .secured(@guardian)

View File

@ -47,6 +47,7 @@ class SiteSerializer < ApplicationSerializer
:tos_url, :tos_url,
:privacy_policy_url, :privacy_policy_url,
:system_user_avatar_template, :system_user_avatar_template,
:lazy_load_categories,
) )
has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer has_many :archetypes, embed: :objects, serializer: ArchetypeSerializer
@ -239,7 +240,7 @@ class SiteSerializer < ApplicationSerializer
end end
def include_categories? def include_categories?
!SiteSetting.lazy_load_categories !scope.can_lazy_load_categories?
end end
def markdown_additional_options def markdown_additional_options
@ -345,6 +346,14 @@ class SiteSerializer < ApplicationSerializer
SiteSetting.show_user_menu_avatars SiteSetting.show_user_menu_avatars
end end
def lazy_load_categories
true
end
def include_lazy_load_categories?
scope.can_lazy_load_categories?
end
private private
def ordered_flags(flags) def ordered_flags(flags)

View File

@ -39,6 +39,6 @@ class TopicListSerializer < ApplicationSerializer
end end
def include_categories? def include_categories?
SiteSetting.lazy_load_categories scope.can_lazy_load_categories?
end end
end end

View File

@ -319,6 +319,6 @@ class TopicViewSerializer < ApplicationSerializer
end end
def include_categories? def include_categories?
SiteSetting.lazy_load_categories scope.can_lazy_load_categories?
end end
end end

View File

@ -2503,6 +2503,7 @@ en:
experimental_form_templates: "EXPERIMENTAL: Enable the form templates feature. <b>After enabled,</b> manage the templates at <a href='%{base_path}/admin/customize/form-templates'>Customize / Templates</a>." experimental_form_templates: "EXPERIMENTAL: Enable the form templates feature. <b>After enabled,</b> manage the templates at <a href='%{base_path}/admin/customize/form-templates'>Customize / Templates</a>."
admin_sidebar_enabled_groups: "EXPERIMENTAL: Enable sidebar navigation for the admin UI for the specified groups, which replaces the top-level admin navigation buttons." admin_sidebar_enabled_groups: "EXPERIMENTAL: Enable sidebar navigation for the admin UI for the specified groups, which replaces the top-level admin navigation buttons."
lazy_load_categories_groups: "EXPERIMENTAL: Lazy load category information only for users of these groups. This improves performance on sites with many categories."
page_loading_indicator: "Configure the loading indicator which appears during page navigations within Discourse. 'Spinner' is a full page indicator. 'Slider' shows a narrow bar at the top of the screen." page_loading_indicator: "Configure the loading indicator which appears during page navigations within Discourse. 'Spinner' is a full page indicator. 'Slider' shows a narrow bar at the top of the screen."
show_user_menu_avatars: "Show user avatars in the user menu" show_user_menu_avatars: "Show user avatars in the user menu"

View File

@ -2328,10 +2328,10 @@ developer:
default: "" default: ""
allow_any: false allow_any: false
refresh: true refresh: true
lazy_load_categories: lazy_load_categories_groups:
default: false default: ""
client: true type: group_list
hidden: true list_type: compact
warn_critical_js_deprecations: warn_critical_js_deprecations:
default: true default: true
client: true client: true

View File

@ -0,0 +1,31 @@
# frozen_string_literal: true
class MigrateLazyLoadCategoriesToGroups < ActiveRecord::Migration[7.0]
def up
execute <<~SQL
UPDATE site_settings
SET name = 'lazy_load_categories_groups', data_type = 20, value = '0'
WHERE name = 'lazy_load_categories' AND value = 't'
SQL
execute <<~SQL
UPDATE site_settings
SET name = 'lazy_load_categories_groups', data_type = 20, value = ''
WHERE name = 'lazy_load_categories' AND value = 'f'
SQL
end
def down
execute <<~SQL
UPDATE site_settings
SET name = 'lazy_load_categories', data_type = 5, value = 't'
WHERE name = 'lazy_load_categories_groups' AND value != ''
SQL
execute <<~SQL
UPDATE site_settings
SET name = 'lazy_load_categories', data_type = 5, value = 'f'
WHERE name = 'lazy_load_categories_groups' AND value = ''
SQL
end
end

View File

@ -628,6 +628,11 @@ class Guardian
@user.has_trust_level_or_staff?(SiteSetting.min_trust_level_for_here_mention) @user.has_trust_level_or_staff?(SiteSetting.min_trust_level_for_here_mention)
end end
def can_lazy_load_categories?
SiteSetting.lazy_load_categories_groups_map.include?(Group::AUTO_GROUPS[:everyone]) ||
@user.in_any_groups?(SiteSetting.lazy_load_categories_groups_map)
end
def is_me?(other) def is_me?(other)
other && authenticated? && other.is_a?(User) && @user == other other && authenticated? && other.is_a?(User) && @user == other
end end

View File

@ -375,11 +375,11 @@ RSpec.describe CategoryList do
end end
end end
describe "lazy_load_categories" do describe "with lazy load categories enabled" do
fab!(:category) { Fabricate(:category, user: admin) } fab!(:category) { Fabricate(:category, user: admin) }
fab!(:subcategory) { Fabricate(:category, user: admin, parent_category: category) } fab!(:subcategory) { Fabricate(:category, user: admin, parent_category: category) }
before { SiteSetting.lazy_load_categories = true } before { SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}" }
it "returns categories with subcategory_ids" do it "returns categories with subcategory_ids" do
expect(category_list.categories.size).to eq(3) expect(category_list.categories.size).to eq(3)

View File

@ -224,14 +224,14 @@ RSpec.describe ListController do
end end
end end
context "with lazy_load_categories" do context "with lazy load categories enabled" do
fab!(:category) fab!(:category)
fab!(:subcategory) { Fabricate(:category, parent_category: category) } fab!(:subcategory) { Fabricate(:category, parent_category: category) }
before { topic.update!(category: subcategory) } before { topic.update!(category: subcategory) }
it "returns categories and parent categories if true" do it "returns categories and parent categories if true" do
SiteSetting.lazy_load_categories = true SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"
get "/latest.json" get "/latest.json"
@ -245,7 +245,7 @@ RSpec.describe ListController do
end end
it "does not return categories if not true" do it "does not return categories if not true" do
SiteSetting.lazy_load_categories = false SiteSetting.lazy_load_categories_groups = ""
get "/latest.json" get "/latest.json"

View File

@ -3227,7 +3227,7 @@ RSpec.describe TopicsController do
end end
it "returns a list of categories" do it "returns a list of categories" do
SiteSetting.lazy_load_categories = true SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"
topic.update!(category: Fabricate(:category)) topic.update!(category: Fabricate(:category))
dest_topic.update!(category: Fabricate(:category)) dest_topic.update!(category: Fabricate(:category))

View File

@ -132,7 +132,7 @@ RSpec.describe SiteSerializer do
end end
it "does not include categories if lazy_load_categories" do it "does not include categories if lazy_load_categories" do
SiteSetting.lazy_load_categories = true SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json