mirror of
https://github.com/flarum/core.git
synced 2025-08-03 23:17:43 +02:00
perf: Allow loading relations in other discussion endpoints (#3191)
This commit is contained in:
@@ -157,7 +157,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||||||
*
|
*
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
protected function getRelationsToLoad(): array
|
protected function getRelationsToLoad(Collection $models): array
|
||||||
{
|
{
|
||||||
$addedRelations = [];
|
$addedRelations = [];
|
||||||
|
|
||||||
@@ -175,7 +175,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||||||
*
|
*
|
||||||
* @return array<string, callable>
|
* @return array<string, callable>
|
||||||
*/
|
*/
|
||||||
protected function getRelationCallablesToLoad(): array
|
protected function getRelationCallablesToLoad(Collection $models): array
|
||||||
{
|
{
|
||||||
$addedRelationCallables = [];
|
$addedRelationCallables = [];
|
||||||
|
|
||||||
@@ -193,8 +193,8 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||||||
*/
|
*/
|
||||||
protected function loadRelations(Collection $models, array $relations, ServerRequestInterface $request = null): void
|
protected function loadRelations(Collection $models, array $relations, ServerRequestInterface $request = null): void
|
||||||
{
|
{
|
||||||
$addedRelations = $this->getRelationsToLoad();
|
$addedRelations = $this->getRelationsToLoad($models);
|
||||||
$addedRelationCallables = $this->getRelationCallablesToLoad();
|
$addedRelationCallables = $this->getRelationCallablesToLoad($models);
|
||||||
|
|
||||||
foreach ($addedRelationCallables as $name => $relation) {
|
foreach ($addedRelationCallables as $name => $relation) {
|
||||||
$addedRelations[] = $name;
|
$addedRelations[] = $name;
|
||||||
|
@@ -14,6 +14,7 @@ use Flarum\Discussion\Command\ReadDiscussion;
|
|||||||
use Flarum\Discussion\Command\StartDiscussion;
|
use Flarum\Discussion\Command\StartDiscussion;
|
||||||
use Flarum\Http\RequestUtil;
|
use Flarum\Http\RequestUtil;
|
||||||
use Illuminate\Contracts\Bus\Dispatcher;
|
use Illuminate\Contracts\Bus\Dispatcher;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Tobscure\JsonApi\Document;
|
use Tobscure\JsonApi\Document;
|
||||||
@@ -70,6 +71,8 @@ class CreateDiscussionController extends AbstractCreateController
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->loadRelations(new Collection([$discussion]), $this->extractInclude($request), $request);
|
||||||
|
|
||||||
return $discussion;
|
return $discussion;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,6 +16,7 @@ use Flarum\Http\RequestUtil;
|
|||||||
use Flarum\Http\SlugManager;
|
use Flarum\Http\SlugManager;
|
||||||
use Flarum\Post\PostRepository;
|
use Flarum\Post\PostRepository;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
@@ -98,9 +99,9 @@ class ShowDiscussionController extends AbstractShowController
|
|||||||
$this->includePosts($discussion, $request, $postRelationships);
|
$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');
|
return ! Str::startsWith($relationship, 'posts');
|
||||||
}));
|
}), $request);
|
||||||
|
|
||||||
return $discussion;
|
return $discussion;
|
||||||
}
|
}
|
||||||
@@ -198,10 +199,29 @@ class ShowDiscussionController extends AbstractShowController
|
|||||||
return $posts->all();
|
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);
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user