From eb6744f5fdf8c2f6e4f55497b1ab9d91d22c557b Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sun, 7 Mar 2021 22:32:23 +0100 Subject: [PATCH] Eagerload some needed relations in ListDiscussionsController (#2639) --- .../Api/Controller/ListDiscussionsController.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Api/Controller/ListDiscussionsController.php b/framework/core/src/Api/Controller/ListDiscussionsController.php index 83b016698..495b14357 100644 --- a/framework/core/src/Api/Controller/ListDiscussionsController.php +++ b/framework/core/src/Api/Controller/ListDiscussionsController.php @@ -110,9 +110,20 @@ class ListDiscussionsController extends AbstractListController Discussion::setStateUser($actor); + // Eager load groups for use in the policies (isAdmin check) + if (in_array('mostRelevantPost.user', $include)) { + $include[] = 'mostRelevantPost.user.groups'; + + // If the first level of the relationship wasn't explicitly included, + // add it so the code below can look for it + if (! in_array('mostRelevantPost', $include)) { + $include[] = 'mostRelevantPost'; + } + } + $results = $results->getResults()->load($include); - if ($relations = array_intersect($include, ['firstPost', 'lastPost'])) { + if ($relations = array_intersect($include, ['firstPost', 'lastPost', 'mostRelevantPost'])) { foreach ($results as $discussion) { foreach ($relations as $relation) { if ($discussion->$relation) {