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.
This commit is contained in:
Osama Sayegh 2024-05-15 03:06:58 +03:00 committed by GitHub
parent 82be988313
commit e3ae57ea7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View File

@ -171,6 +171,7 @@ class User < ActiveRecord::Base
after_create :set_default_categories_preferences after_create :set_default_categories_preferences
after_create :set_default_tags_preferences after_create :set_default_tags_preferences
after_create :set_default_sidebar_section_links 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 :set_default_sidebar_section_links, if: Proc.new { self.saved_change_to_staged? }
after_update :trigger_user_updated_event, after_update :trigger_user_updated_event,
@ -2169,6 +2170,10 @@ class User < ActiveRecord::Base
def validate_status!(status) def validate_status!(status)
UserStatus.new(status).validate! UserStatus.new(status).validate!
end end
def refresh_user_directory
DirectoryItem.refresh!
end
end end
# == Schema Information # == Schema Information

View File

@ -129,6 +129,27 @@ RSpec.describe User do
) { user.update(name: "Batman") } ) { user.update(name: "Batman") }
end end
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 end
describe "Validations" do describe "Validations" do