PERF: Don't parse posts for mentions when user status is disabled (#19915)

Prior to this change, we were parsing `Post#cooked` every time we
serialize a post to extract the usernames of mentioned users in the
post. However, the only reason we have to do this is to support
displaying a user's status beside each mention in a post on the client side when
the `enable_user_status` site setting is enabled. When
`enable_user_status` is disabled, we should avoid having to parse
`Post#cooked` since there is no point in doing so.
This commit is contained in:
Alan Guo Xiang Tan
2023-01-20 07:58:00 +08:00
committed by GitHub
parent 6aae64d6f8
commit b00e160dae
4 changed files with 69 additions and 86 deletions

View File

@ -31,8 +31,6 @@ class TopicView
:personal_message,
:can_review_topic,
:page,
:mentioned_users,
:mentions,
)
alias queued_posts_enabled? queued_posts_enabled
@ -144,9 +142,6 @@ class TopicView
end
end
parse_mentions
load_mentioned_users
TopicView.preload(self)
@draft_key = @topic.draft_key
@ -700,17 +695,21 @@ class TopicView
@topic.published_page
end
def parse_mentions
@mentions = @posts.to_h { |p| [p.id, p.mentions] }.reject { |_, v| v.empty? }
end
def mentioned_users
@mentioned_users ||=
begin
mentions = @posts.to_h { |p| [p.id, p.mentions] }.reject { |_, v| v.empty? }
usernames = mentions.values
usernames.flatten!
usernames.uniq!
def load_mentioned_users
usernames = @mentions.values.flatten.uniq
mentioned_users = User.where(username: usernames)
users = User.where(username: usernames).includes(:user_status).index_by(&:username)
mentioned_users = mentioned_users.includes(:user_status) if SiteSetting.enable_user_status
@mentioned_users = mentioned_users.to_h { |u| [u.username, u] }
mentions.reduce({}) do |hash, (post_id, post_mentioned_usernames)|
hash[post_id] = post_mentioned_usernames.map { |username| users[username] }.compact
hash
end
end
end
def tags