PERF: Preload user information when visiting user messages routes (#21929)

What is the problem?

The user messages routes are currently routed by the server to
`UserActionsController#private_messages`. However, the method is
essentially a no-op and does not do any preloading. As a result, when we
load the user private messages routes, the client ends up having to
issue another request to the server to get more information about the
user profile currently being viewed. This extra request is triggered by
the `user` model's `findDetails` method that is called from the `user`
route in the `afterModel` hook.

What is the solution?

The `user` model's `findDetails` method actually checks the preload
store to see if the `user_${username}` key is present in the store and
if it is, it will use the preloaded data instead of triggering another
request. Since the user private messages routes are nested under the
user route on the client side, we have to rely on the
`UsersController#show` controller action on the server side for the user private
messages route as the `UsersController#show` controller action preloads
the required user information for the client side.
This commit is contained in:
Alan Guo Xiang Tan 2023-06-05 20:24:22 +09:00 committed by GitHub
parent afdf1ac7c3
commit 2032d3c2fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 12 deletions

View File

@ -36,11 +36,6 @@ class UserActionsController < ApplicationController
render_serialized(UserAction.stream_item(params[:id], guardian), UserActionSerializer) render_serialized(UserAction.stream_item(params[:id], guardian), UserActionSerializer)
end end
def private_messages
# DO NOT REMOVE
# TODO should preload messages to avoid extra http req
end
private private
def user_actions_params def user_actions_params

View File

@ -521,29 +521,28 @@ Discourse::Application.routes.draw do
:constraints => { :constraints => {
token: /[0-9a-f]+/, token: /[0-9a-f]+/,
} }
get "#{root_path}/:username/private-messages" => "user_actions#private_messages", get "#{root_path}/:username/private-messages" => "users#show",
:constraints => { :constraints => {
username: RouteFormat.username, username: RouteFormat.username,
} }
get "#{root_path}/:username/private-messages/:filter" => "user_actions#private_messages", get "#{root_path}/:username/private-messages/:filter" => "users#show",
:constraints => { :constraints => {
username: RouteFormat.username, username: RouteFormat.username,
} }
get "#{root_path}/:username/messages" => "user_actions#private_messages", get "#{root_path}/:username/messages" => "users#show",
:constraints => { :constraints => {
username: RouteFormat.username, username: RouteFormat.username,
} }
get "#{root_path}/:username/messages/:filter" => "user_actions#private_messages", get "#{root_path}/:username/messages/:filter" => "users#show",
:constraints => { :constraints => {
username: RouteFormat.username, username: RouteFormat.username,
} }
get "#{root_path}/:username/messages/group/:group_name" => "user_actions#private_messages", get "#{root_path}/:username/messages/group/:group_name" => "users#show",
:constraints => { :constraints => {
username: RouteFormat.username, username: RouteFormat.username,
group_name: RouteFormat.username, group_name: RouteFormat.username,
} }
get "#{root_path}/:username/messages/group/:group_name/:filter" => get "#{root_path}/:username/messages/group/:group_name/:filter" => "users#show",
"user_actions#private_messages",
:constraints => { :constraints => {
username: RouteFormat.username, username: RouteFormat.username,
group_name: RouteFormat.username, group_name: RouteFormat.username,