1
0
mirror of https://github.com/flarum/core.git synced 2025-08-15 04:44:08 +02:00

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
This commit is contained in:
Sami Mazouz
2021-08-23 20:33:42 +01:00
committed by GitHub
parent 5217b8b6be
commit af9e5a9cc9
2 changed files with 29 additions and 4 deletions

View File

@@ -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