PERF: Remove category_user lookup when loading private messages.

Private messages do not belong to categories so the query is unnecessary
overhead.
This commit is contained in:
Guo Xiang Tan 2020-09-11 16:26:04 +08:00
parent cd78bcee3a
commit 543e972fec
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 26 additions and 2 deletions

View File

@ -80,7 +80,6 @@ class TopicList < DraftableList
# Attach some data for serialization to each topic
@topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user
@category_user_lookup = CategoryUser.lookup_for(@current_user, @topics.map(&:category_id).uniq) if @current_user
post_action_type =
if @current_user
@ -111,7 +110,10 @@ class TopicList < DraftableList
@topics.each do |ft|
ft.user_data = @topic_lookup[ft.id] if @topic_lookup.present?
ft.category_user_data = @category_user_lookup[ft.category_id] if @category_user_lookup.present?
if ft.regular? && category_user_lookup.present?
ft.category_user_data = @category_user_lookup[ft.category_id]
end
if ft.user_data && post_action_lookup && actions = post_action_lookup[ft.id]
ft.user_data.post_action_data = { post_action_type => actions }
@ -140,4 +142,16 @@ class TopicList < DraftableList
def attributes
{ 'more_topics_url' => page }
end
private
def category_user_lookup
@category_user_lookup ||= begin
if @current_user
CategoryUser.lookup_for(@current_user, @topics.map(&:category_id).uniq)
else
[]
end
end
end
end

View File

@ -48,6 +48,16 @@ describe TopicList do
end
end
describe '#load_topics' do
it 'loads additional data for serialization' do
category_user = CategoryUser.create!(user: user, category: topic.category)
topic = topic_list.load_topics.first
expect(topic.category_user_data).to eq(category_user)
end
end
describe '#top_tags' do
it 'should return the right tags' do
tag = Fabricate(:tag, topics: [topic])