From f7777ff9e396ab6bb12bc258bc062a66538c865b Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Mon, 13 Dec 2021 11:34:51 +0100 Subject: [PATCH] perf: Fix eager loading of tag state relation in other endpoints (#151) --- extensions/tags/extend.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/extensions/tags/extend.php b/extensions/tags/extend.php index 269643fea..42512d1f6 100644 --- a/extensions/tags/extend.php +++ b/extensions/tags/extend.php @@ -31,6 +31,12 @@ use Flarum\Tags\Query\TagFilterGambit; use Flarum\Tags\Tag; use Psr\Http\Message\ServerRequestInterface; +$eagerLoadTagState = function ($query, ?ServerRequestInterface $request, array $relations) { + if ($request && in_array('tags.state', $relations, true)) { + $query->withStateFor(RequestUtil::getActor($request)); + } +}; + return [ (new Extend\Frontend('forum')) ->js(__DIR__.'/js/dist/forum.js') @@ -73,17 +79,15 @@ return [ (new Extend\ApiController(FlarumController\ListDiscussionsController::class)) ->addInclude(['tags', 'tags.state', 'tags.parent']) - ->loadWhere('tags', function ($query, ?ServerRequestInterface $request, array $relations) { - if ($request && in_array('tags.state', $relations, true)) { - $query->withStateFor(RequestUtil::getActor($request)); - } - }), + ->loadWhere('tags', $eagerLoadTagState), (new Extend\ApiController(FlarumController\ShowDiscussionController::class)) - ->addInclude(['tags', 'tags.state', 'tags.parent']), + ->addInclude(['tags', 'tags.state', 'tags.parent']) + ->loadWhere('tags', $eagerLoadTagState), (new Extend\ApiController(FlarumController\CreateDiscussionController::class)) - ->addInclude(['tags', 'tags.state', 'tags.parent']), + ->addInclude(['tags', 'tags.state', 'tags.parent']) + ->loadWhere('tags', $eagerLoadTagState), (new Extend\ApiController(FlarumController\ShowForumController::class)) ->addInclude(['tags', 'tags.parent'])