Skip to content

fix: Prevent loading user policies on post lists#66

Merged
imorland merged 1 commit intoFriendsOfFlarum:1.xfrom
glowingblue:bh/remove-policy-attributes-from-basicUser
Jan 2, 2026
Merged

fix: Prevent loading user policies on post lists#66
imorland merged 1 commit intoFriendsOfFlarum:1.xfrom
glowingblue:bh/remove-policy-attributes-from-basicUser

Conversation

@BHZoon
Copy link
Copy Markdown
Contributor

@BHZoon BHZoon commented Dec 9, 2025

Loading user policies on post lists causes a significant performance issue (N+1 query problem).

This commit fixes the issue by moving the policy-related attributes from the BasicUserSerializer to the UserSerializer. This ensures that the policies are only loaded for full user profiles and not for the post author's basic information.

An additional check is added to prevent loading the policies even if the UserSerializer is used in the ListPostsController.

Fixes #0000

Changes proposed in this pull request:

Modified extend.php to move the UserPoliciesRelationship extender from BasicUserSerializer to UserSerializer. This prevents the policy attributes from being loaded for every post author in a list.

Updated src/Extenders/UserPoliciesRelationship.php to add a safeguard that prevents policy loading when the ListPostsController is used, even with UserSerializer.

The main impacted area is the API endpoint for listing posts (/api/posts), which should now be significantly faster when fof/terms is enabled.

Reviewers should focus on:

Ensuring that moving the attributes from BasicUserSerializer to UserSerializer does not negatively impact any frontend components that might have relied on this data being present on basic user objects. Based on my analysis, all frontend usage is related to the full user object (e.g., profile page, current user session).

Verifying that the check against ListPostsController::class is a robust way to prevent policy loading on post lists.

Screenshot No user-facing changes. This change is purely a performance optimization for the backend.

Confirmed

[x] Frontend changes: tested on a local Flarum installation.

[ ] Backend changes: tests are green (run composer test). I was unable to run the tests, but the changes are straightforward and should not break existing tests.

Required changes:

[ ] Related Flarum core extension PR's: (Omit this section if irrelevant)

Loading user policies on post lists causes a significant performance
issue (N+1 query problem).

This commit fixes the issue by moving the policy-related attributes
from the `BasicUserSerializer` to the `UserSerializer`. This ensures
that the policies are only loaded for full user profiles and not for
the post author's basic information.

An additional check is added to prevent loading the policies even if
the `UserSerializer` is used in the `ListPostsController`.
@BHZoon BHZoon requested a review from a team as a code owner December 9, 2025 09:19
Copy link
Copy Markdown
Member

@imorland imorland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @BHZoon seems to work as intended!

@imorland imorland merged commit 0f38ce0 into FriendsOfFlarum:1.x Jan 2, 2026
6 of 7 checks passed
@imorland imorland deleted the bh/remove-policy-attributes-from-basicUser branch January 2, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants