Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagerload some needed relations in ListDiscussionsController #2639

Merged
merged 5 commits into from
Mar 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Api/Controller/ListDiscussionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,20 @@ protected function data(ServerRequestInterface $request, Document $document)

Discussion::setStateUser($actor);

// Eager load groups for use in the policies (isAdmin check)
if (in_array('mostRelevantPost.user', $include)) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd add a comment saying this is done for eager loading

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we want to explain why we need to eager load it or just state that we're eager loading it ?

Observation: this is currently only necessary because the suspend extension's UserPolicy calls isAdmin on the $user object which loads all groups, in which case we should be eager loading the relation in the extension and not here, but I'm seeing in https://github.com/flarum/core/pull/2620/files#diff-5f5aff0db193a1b7b5bdff796982aefe84572973059c31c65f33233e7fafd597R34-R36 that we might end up doing it in core as well, so it will belong here after all (if that pr isn't changed).

Copy link
Member

Choose a reason for hiding this comment

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

Should the comment reflect that? Suggestion:

// Eager load groups for use in the policies (isAdmin check)

$include[] = 'mostRelevantPost.user.groups';

// If the first level of the relationship wasn't explicitly included,
// add it so the code below can look for it
if (! in_array('mostRelevantPost', $include)) {
$include[] = 'mostRelevantPost';
}
}

$results = $results->getResults()->load($include);

if ($relations = array_intersect($include, ['firstPost', 'lastPost'])) {
if ($relations = array_intersect($include, ['firstPost', 'lastPost', 'mostRelevantPost'])) {
foreach ($results as $discussion) {
foreach ($relations as $relation) {
if ($discussion->$relation) {
Expand Down