FIX: Prevent discourse_reactions from overriding topic_view.posts preloads (#36041)

Meta:
https://meta.discourse.org/t/tl-lang-crawler-view-not-showing-posts-in-lang/388443/6

When loading topics at `/t/:id` we use the `topic_view` and have some
preloads:

https://github.com/discourse/discourse/blob/d4313be4859bd07d95c620b15179424a87d94ddd/lib/topic_view.rb#L925-L939

Unfortunately these preloads have been overridden in reaction's
`TopicViewSerializer` as overriding posts with a new `post.includes`
would create a new query, so we're using `Preloader` here to preserve
existing associations.
This commit is contained in:
Natalie Tay
2025-11-14 09:53:53 -05:00
committed by GitHub
parent 0320843ef5
commit 1417b9828d
2 changed files with 8 additions and 5 deletions
+1 -2
View File
@@ -929,9 +929,8 @@ class TopicView
:deleted_by,
:incoming_email,
:image_upload,
:localizations,
)
@posts = @posts.includes(:localizations) if SiteSetting.content_localization_enabled
@posts = @posts.includes({ user: :user_status }) if SiteSetting.enable_user_status
@posts = apply_default_scope(@posts)
@@ -3,7 +3,13 @@
module DiscourseReactions::PostsReactionLoader
def posts_with_reactions
if SiteSetting.discourse_reactions_enabled
posts = object.posts.includes(:post_actions, reactions: { reaction_users: :user })
posts = object.posts
ActiveRecord::Associations::Preloader.new(
records: posts,
associations: [:post_actions, { reactions: { reaction_users: :user } }],
).call
post_ids = posts.map(&:id).uniq
posts_reaction_users_count = TopicViewSerializer.posts_reaction_users_count(post_ids)
post_actions_with_reaction_users =
@@ -14,8 +20,6 @@ module DiscourseReactions::PostsReactionLoader
post.reaction_users_count = posts_reaction_users_count[post.id].to_i
post.post_actions_with_reaction_users = post_actions_with_reaction_users[post.id] || {}
end
object.instance_variable_set(:@posts, posts)
end
end
end