perf: Allow loading relations in other discussion endpoints (#3191)

This commit is contained in:
Sami Mazouz 2021-12-13 11:34:26 +01:00 committed by GitHub
parent dc48e2327b
commit c7791b63f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 8 deletions

View File

@ -157,7 +157,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
*
* @return string[]
*/
protected function getRelationsToLoad(): array
protected function getRelationsToLoad(Collection $models): array
{
$addedRelations = [];
@ -175,7 +175,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
*
* @return array<string, callable>
*/
protected function getRelationCallablesToLoad(): array
protected function getRelationCallablesToLoad(Collection $models): array
{
$addedRelationCallables = [];
@ -193,8 +193,8 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
*/
protected function loadRelations(Collection $models, array $relations, ServerRequestInterface $request = null): void
{
$addedRelations = $this->getRelationsToLoad();
$addedRelationCallables = $this->getRelationCallablesToLoad();
$addedRelations = $this->getRelationsToLoad($models);
$addedRelationCallables = $this->getRelationCallablesToLoad($models);
foreach ($addedRelationCallables as $name => $relation) {
$addedRelations[] = $name;

View File

@ -14,6 +14,7 @@ use Flarum\Discussion\Command\ReadDiscussion;
use Flarum\Discussion\Command\StartDiscussion;
use Flarum\Http\RequestUtil;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
@ -70,6 +71,8 @@ class CreateDiscussionController extends AbstractCreateController
);
}
$this->loadRelations(new Collection([$discussion]), $this->extractInclude($request), $request);
return $discussion;
}
}

View File

@ -16,6 +16,7 @@ use Flarum\Http\RequestUtil;
use Flarum\Http\SlugManager;
use Flarum\Post\PostRepository;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Psr\Http\Message\ServerRequestInterface;
@ -98,9 +99,9 @@ class ShowDiscussionController extends AbstractShowController
$this->includePosts($discussion, $request, $postRelationships);
}
$discussion->load(array_filter($include, function ($relationship) {
$this->loadRelations(new Collection([$discussion]), array_filter($include, function ($relationship) {
return ! Str::startsWith($relationship, 'posts');
}));
}), $request);
return $discussion;
}
@ -198,10 +199,29 @@ class ShowDiscussionController extends AbstractShowController
return $posts->all();
}
protected function getRelationsToLoad(): array
protected function getRelationsToLoad(Collection $models): array
{
$addedRelations = parent::getRelationsToLoad();
$addedRelations = parent::getRelationsToLoad($models);
if ($models->first() instanceof Discussion) {
return $addedRelations;
}
return $this->getPostRelationships($addedRelations);
}
protected function getRelationCallablesToLoad(Collection $models): array
{
$addedCallableRelations = parent::getRelationCallablesToLoad($models);
if ($models->first() instanceof Discussion) {
return $addedCallableRelations;
}
$postCallableRelationships = $this->getPostRelationships(array_keys($addedCallableRelations));
return array_intersect_key($addedCallableRelations, array_flip(array_map(function ($relation) {
return "posts.$relation";
}, $postCallableRelationships)));
}
}