[2.x] Fix N+1 group_user queries when serializing users - #4696
Merged
Conversation
The UserResource `groups` relationship getter issued a fresh `group_user` query for every serialized user, so the query count grew linearly with the number of users in a payload (e.g. one per post author on the posts endpoint). The getter now reads the (eager-)loaded `groups` relation and filters hidden groups in PHP, and the relevant endpoints eager-load `groups` so it is batched into a single query. This also shares the relation that `User::isAdmin()` reads (via the `email` field's `editCredentials` check), removing the redundant per-user load, and the email/isEmailConfirmed visibility checks now short-circuit on the cheap self comparison before invoking the policy. Fixes #4695
During fulltext search the index populates mostRelevantPost.user, whose isAdmin() check would otherwise lazy-load groups per discussion.
This was referenced Jun 13, 2026
Closed
imorland
added a commit
that referenced
this pull request
Jun 13, 2026
UserPolicy::editCredentials evaluated $user->isAdmin() (which reads $user->groups) before checking whether the actor can edit credentials at all. On render paths that serialize many users — post authors, and likers via flarum-likes' default `likes` include — UserResource's email-field visibility runs this check per serialized user, lazy-loading each one's groups: an N+1 that #4696 did not cover. Check the actor's `user.editCredentials` permission first and return no opinion when absent (guests and normal users — the common case), so the target user's groups are never touched. $user->isAdmin() is only consulted when the actor can actually edit credentials, where it is needed to protect admins. The visibility outcome is unchanged in every case. Adds a flarum-likes regression test asserting likers' groups are not loaded one query per liker. Fixes #4724.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Serializing users through
UserResourceissued redundantgroup_userqueries. Thegroupsrelationship getter called$user->groups()->get()/$user->visibleGroups()->get(), so a fresh query ran for every serialized user whenever groups were in the payload (e.g.user.groupson the posts endpoint), and the count grew linearly with the number of users.A secondary, constant inefficiency came from the
emailfield's visibility check (editCredentials→User::isAdmin()), which reads$user->groups. Because the getter queried a different relation, each user's groups were loaded twice.Changes
UserResourcegroupsgetter now reads the (eager-)loadedgroupsrelation and filters hidden groups in PHP, reusing the relationisAdmin()already loads.values()keeps the filtered result a JSON array rather than an object.groupsonUserResourceUpdate/Show/Index,user.groupsonPostResourceShow/Index, anduser.groups/lastPostedUser.groups/firstPost.user.groupsonDiscussionResource::Show(to matchIndex).email/isEmailConfirmedvisibility reordered to check the cheapid === idself comparison before theeditCredentialspolicy, avoiding anisAdmin()groups read for the own-profile case.Reviewer notes
Hidden-group filtering is preserved: actors without
viewHiddenGroupsstill never see hidden groups in the relationship or includes (covered by the existingShowGroupsRelationshipTest, hardened here with a hidden group sorted first + a JSON-array assertion). Behaviour for the linkage is unchanged.Fixes #4695