From e3ae57ea7ab31de0d5a084d881eab4e379e87a9f Mon Sep 17 00:00:00 2001 From: Osama Sayegh Date: Wed, 15 May 2024 03:06:58 +0300 Subject: [PATCH] FIX: Create directory items for new users when in bootstrap mode (#27020) The users directory is updated on a daily cadence. However, when a site is new and doesn't have many users, it can be confusing that a user who has just joined doesn't show up in the users until a day after they join. To eliminate this confusion, this commit triggers a refresh for the users directory as soon as as a user joins, if the site is in bootstrap mode. The reason for the conditional trigger is that refreshing the users directory is an expensive operation and doing it often on a large site with many users could lead to performance problems. Internal topic: t/126076. --- app/models/user.rb | 5 +++++ spec/models/user_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 8a5d182965a..a327120e071 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -171,6 +171,7 @@ class User < ActiveRecord::Base after_create :set_default_categories_preferences after_create :set_default_tags_preferences after_create :set_default_sidebar_section_links + after_create :refresh_user_directory, if: Proc.new { SiteSetting.bootstrap_mode_enabled } after_update :set_default_sidebar_section_links, if: Proc.new { self.saved_change_to_staged? } after_update :trigger_user_updated_event, @@ -2169,6 +2170,10 @@ class User < ActiveRecord::Base def validate_status!(status) UserStatus.new(status).validate! end + + def refresh_user_directory + DirectoryItem.refresh! + end end # == Schema Information diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 95e237359f5..49423bd4eb8 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -129,6 +129,27 @@ RSpec.describe User do ) { user.update(name: "Batman") } end end + + describe "#refresh_user_directory" do + context "when bootstrap mode is enabled" do + before { SiteSetting.bootstrap_mode_enabled = true } + + it "creates directory items for a new user for all periods" do + expect do user = Fabricate(:user) end.to change { DirectoryItem.count }.by( + DirectoryItem.period_types.count, + ) + expect(DirectoryItem.where(user_id: user.id)).to exist + end + end + + context "when bootstrap mode is disabled" do + before { SiteSetting.bootstrap_mode_enabled = false } + + it "doesn't create directory items for a new user" do + expect do Fabricate(:user) end.not_to change { DirectoryItem.count } + end + end + end end describe "Validations" do