mirror of
https://github.com/discourse/discourse.git
synced 2025-03-09 14:34:35 +00:00
PERF: avoid N+1 on topic view
Serializer is injecting information into cooked and reaching direct to custom fields that were not preloaded This amends it so basic post serializer can use the proper interface That said we should probably follow this up so we don't reach for this info on every post.
This commit is contained in:
parent
10b36c6446
commit
accbbded15
app/serializers
@ -10,6 +10,8 @@ class BasicPostSerializer < ApplicationSerializer
|
||||
:cooked,
|
||||
:cooked_hidden
|
||||
|
||||
attr_accessor :topic_view
|
||||
|
||||
def name
|
||||
object.user && object.user.name
|
||||
end
|
||||
@ -41,6 +43,12 @@ class BasicPostSerializer < ApplicationSerializer
|
||||
cooked = object.filter_quotes(@parent_post)
|
||||
|
||||
if scope&.user
|
||||
|
||||
# PERF: this should not run on every post, only specific ones
|
||||
# also, why is this in basic post serializer?
|
||||
requested_group_id = post_custom_fields['requested_group_id'].to_i
|
||||
|
||||
if requested_group_id > 0
|
||||
group = Group
|
||||
.joins('JOIN group_users ON groups.id = group_users.group_id')
|
||||
.find_by(
|
||||
@ -57,6 +65,7 @@ class BasicPostSerializer < ApplicationSerializer
|
||||
EOF
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cooked
|
||||
end
|
||||
@ -66,4 +75,12 @@ class BasicPostSerializer < ApplicationSerializer
|
||||
SiteSetting.enable_names?
|
||||
end
|
||||
|
||||
def post_custom_fields
|
||||
@post_custom_fields ||= if @topic_view
|
||||
(@topic_view.post_custom_fields || {})[object.id] || {}
|
||||
else
|
||||
object.custom_fields
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -4,7 +4,6 @@ class PostSerializer < BasicPostSerializer
|
||||
|
||||
# To pass in additional information we might need
|
||||
INSTANCE_VARS ||= [
|
||||
:topic_view,
|
||||
:parent_post,
|
||||
:add_raw,
|
||||
:add_title,
|
||||
@ -490,12 +489,4 @@ private
|
||||
@post_actions ||= (@topic_view&.all_post_actions || {})[object.id]
|
||||
end
|
||||
|
||||
def post_custom_fields
|
||||
@post_custom_fields ||= if @topic_view
|
||||
(@topic_view.post_custom_fields || {})[object.id] || {}
|
||||
else
|
||||
object.custom_fields
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user