[2.x] fix: include user groups when listing discussions - #4928
Merged
Conversation
The discussion list eager-loaded `user.groups` but never listed it in `defaultInclude`, so the groups were loaded for the permission checks and then discarded before serialisation. Every user in the list arrived with no groups at all. That is enough on its own — a user with no groups has no badges — but it also outlives the request. The frontend keeps the group-less record in its store, and opening that user's profile finds it there and skips fetching the user again, so the profile stays badge-less until the page is reloaded. `Show` had the same gap. It already included `firstPost.user.groups`, so the post author was fine while the discussion author and last poster were not; all three were eager-loaded. No new queries: the relations were already loaded for these very users, and this only serialises what was being thrown away. Measured over a page of ten discussions with twenty distinct users, `group_user` is queried three times before and after.
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.
Fixes #0000
Changes proposed in this pull request:
Click a user's avatar in the discussion list and their profile opens with no badges. Reload the page and the badges appear.
DiscussionResource'sIndexendpoint eager-loadsuser.groupsbut never lists it indefaultInclude.eagerLoadis a database optimisation — it populates the Eloquent relation so the permission checks (isAdmin(),canEdit, and friends) don't fire a query per user. It does not put anything in the JSON:API document. So the groups were loaded, used for permissions, then discarded before serialisation, and every user in the list came back with nogroupsrelationship at all.That alone means no badges. But it also outlives the request: the frontend keeps that group-less user in its store, and
UserPage::loadUser()scans the store first — finding a user with ajoinTime(), it callsshow()and returns without fetching. SoUserResource'sShowendpoint, which does havedefaultInclude(['groups']), never runs. A hard reload empties the store, the fetch happens, and the badges come back — which is why it looks like a caching problem rather than a payload one.Showhad the same gap. It already includedfirstPost.user.groups, so the post author serialised correctly while the discussion author and last poster didn't, despite all three being eager-loaded. #4695 fixed the eager-loading for all three but only added the include for one; this finishes it.Every include added here already had a matching
eagerLoadon the same endpoint.Reviewers should focus on:
group_useris queried 3 times before and 3 times after. The new test asserts this, and I checked it isn't vacuous: removing theeagerLoadwhile keeping the includes makes it fail.groupsresources themselves (23 users → 5 group resources), so gzip takes most of that back. Still, it is a payload increase on the discussion list and worth a deliberate yes rather than being waved through.UserResource'sgroupsrelationship getter, which checksviewHiddenGroups— there's a test pinning it, but worth a second pair of eyes given this widens where groups are serialised.Screenshot
No visual change to the discussion list itself. The difference is on the profile you land on after clicking an avatar: badges present immediately instead of after a reload.
Necessity
Confirmed
yarn testinjs/). — no frontend changescomposer test).New
ListGroupsQueryCountTest, modelled on the existingShowGroupsQueryCountTestfrom #4695: ten discussions with twenty distinct users, so a per-user query shows up as a multiple of the page size rather than hiding in a constant. Covers query count, groups actually being present, and hidden groups staying hidden. 57 discussion API tests green, including the pre-existing query-count tests. PHPStan clean.Required changes: