From af9e5a9cc9d7cf3d5e7c77865cd3873b6dc59e55 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Mon, 23 Aug 2021 20:33:42 +0100 Subject: [PATCH] Eager load more necessary related mentions (#72) * perf: Eager load related mentions from the extender These missing relations caused more queries to be called, the more mentions posts have the more queries produced. * perf: Eager load models in show endpoints Can't use the extender for that --- extensions/mentions/extend.php | 24 ++++++++++++++++--- .../mentions/src/FilterVisiblePosts.php | 9 ++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/extensions/mentions/extend.php b/extensions/mentions/extend.php index c4f388bdf..178330e33 100644 --- a/extensions/mentions/extend.php +++ b/extensions/mentions/extend.php @@ -11,6 +11,7 @@ namespace Flarum\Mentions; use Flarum\Api\Controller; use Flarum\Api\Serializer\BasicPostSerializer; +use Flarum\Api\Serializer\BasicUserSerializer; use Flarum\Api\Serializer\PostSerializer; use Flarum\Extend; use Flarum\Mentions\Notification\PostMentionedBlueprint; @@ -56,20 +57,37 @@ return [ (new Extend\ApiSerializer(BasicPostSerializer::class)) ->hasMany('mentionedBy', BasicPostSerializer::class) ->hasMany('mentionsPosts', BasicPostSerializer::class) - ->hasMany('mentionsUsers', BasicPostSerializer::class), + ->hasMany('mentionsUsers', BasicUserSerializer::class), (new Extend\ApiController(Controller\ShowDiscussionController::class)) - ->addInclude(['posts.mentionedBy', 'posts.mentionedBy.user', 'posts.mentionedBy.discussion']), + ->addInclude(['posts.mentionedBy', 'posts.mentionedBy.user', 'posts.mentionedBy.discussion']) + ->load([ + 'posts.mentionsUsers', 'posts.mentionsPosts', 'posts.mentionsPosts.user', 'posts.mentionedBy', + 'posts.mentionedBy.mentionsPosts', 'posts.mentionedBy.mentionsPosts.user', 'posts.mentionedBy.mentionsUsers', + ]), + + (new Extend\ApiController(Controller\ListDiscussionsController::class)) + ->load([ + 'firstPost.mentionsUsers', 'firstPost.mentionsPosts', 'firstPost.mentionsPosts.user', + 'lastPost.mentionsUsers', 'lastPost.mentionsPosts', 'lastPost.mentionsPosts.user' + ]), (new Extend\ApiController(Controller\ShowPostController::class)) ->addInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion']), (new Extend\ApiController(Controller\ListPostsController::class)) - ->addInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion']), + ->addInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion']) + ->load([ + 'mentionsUsers', 'mentionsPosts', 'mentionsPosts.user', 'mentionedBy', + 'mentionedBy.mentionsPosts', 'mentionedBy.mentionsPosts.user', 'mentionedBy.mentionsUsers', + ]), (new Extend\ApiController(Controller\CreatePostController::class)) ->addInclude(['mentionsPosts', 'mentionsPosts.mentionedBy']), + (new Extend\ApiController(Controller\UpdatePostController::class)) + ->addInclude(['mentionsPosts', 'mentionsPosts.mentionedBy']), + (new Extend\ApiController(Controller\AbstractSerializeController::class)) ->prepareDataForSerialization(FilterVisiblePosts::class), diff --git a/extensions/mentions/src/FilterVisiblePosts.php b/extensions/mentions/src/FilterVisiblePosts.php index 1a86208cb..256025ec7 100755 --- a/extensions/mentions/src/FilterVisiblePosts.php +++ b/extensions/mentions/src/FilterVisiblePosts.php @@ -44,6 +44,8 @@ class FilterVisiblePosts */ public function __invoke(Controller\AbstractSerializeController $controller, $data, ServerRequestInterface $request) { + $relations = []; + // Firstly we gather a list of posts contained within the API document. // This will vary according to the API endpoint that is being accessed. if ($controller instanceof Controller\ShowDiscussionController) { @@ -51,6 +53,11 @@ class FilterVisiblePosts } elseif ($controller instanceof Controller\ShowPostController || $controller instanceof Controller\CreatePostController || $controller instanceof Controller\UpdatePostController) { + $relations = [ + 'mentionsUsers', 'mentionsPosts', 'mentionsPosts.user', 'mentionedBy', + 'mentionedBy.mentionsPosts', 'mentionedBy.mentionsPosts.user', 'mentionedBy.mentionsUsers' + ]; + $posts = [$data]; } elseif ($controller instanceof Controller\ListPostsController) { $posts = $data; @@ -67,7 +74,7 @@ class FilterVisiblePosts // Load all of the users that these posts mention. This way the data // will be ready to go when we need to sub in current usernames // during the rendering process. - $posts->loadMissing(['mentionsUsers', 'mentionsPosts.user', 'mentionedBy']); + $posts->loadMissing($relations); // Construct a list of the IDs of all of the posts that these posts // have been mentioned in. We can then filter this list of IDs to