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

perf: Allow loading relations in other discussion endpoints #3191

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/Api/Controller/AbstractSerializeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ abstract protected function createElement($data, SerializerInterface $serializer
*
* @return string[]
*/
protected function getRelationsToLoad(): array
protected function getRelationsToLoad(Collection $models): array
{
$addedRelations = [];

Expand All @@ -175,7 +175,7 @@ protected function getRelationsToLoad(): array
*
* @return array<string, callable>
*/
protected function getRelationCallablesToLoad(): array
protected function getRelationCallablesToLoad(Collection $models): array
{
$addedRelationCallables = [];

Expand All @@ -193,8 +193,8 @@ protected function getRelationCallablesToLoad(): array
*/
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;
Expand Down
3 changes: 3 additions & 0 deletions src/Api/Controller/CreateDiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -70,6 +71,8 @@ protected function data(ServerRequestInterface $request, Document $document)
);
}

$this->loadRelations(new Collection([$discussion]), $this->extractInclude($request), $request);

return $discussion;
}
}
28 changes: 24 additions & 4 deletions src/Api/Controller/ShowDiscussionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -98,9 +99,9 @@ protected function data(ServerRequestInterface $request, Document $document)
$this->includePosts($discussion, $request, $postRelationships);
}

$discussion->load(array_filter($include, function ($relationship) {
$this->loadRelations(new Collection([$discussion]), array_filter($include, function ($relationship) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure that I like creating the collection here, it might be better to modify loadRelations to accept either a single model or a collection of models, as they both have the same methods, though I dislike that as well, I wish there was a common interface for this, there is a trait, but I don't think you can use traits for typing.

Copy link
Member

Choose a reason for hiding this comment

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

Let's keep it like this for now; it's not perfect, but it's good enough.

re: typing, we can figure out the PHPStan implementation later.

return ! Str::startsWith($relationship, 'posts');
}));
}), $request);

return $discussion;
}
Expand Down Expand Up @@ -198,10 +199,29 @@ private function loadPosts($discussion, $actor, $offset, $limit, array $include)
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)));
}
}