From 888efd677abd2c88c2379cfc7d376355883ffb7b Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 11 Nov 2023 16:01:14 +0100 Subject: [PATCH] perf: eager load file counts --- extend.php | 21 +++++++++++++++++++ src/Extenders/AddForumAttributes.php | 3 +++ src/Extenders/AddUserAttributes.php | 4 ++-- src/Extenders/LoadFilesRelationship.php | 28 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/Extenders/LoadFilesRelationship.php diff --git a/extend.php b/extend.php index 83f6f682..9214e602 100644 --- a/extend.php +++ b/extend.php @@ -13,6 +13,10 @@ namespace FoF\Upload; use Blomstra\Gdpr\Extend\UserData; +use Flarum\Api\Controller\ListDiscussionsController; +use Flarum\Api\Controller\ListPostsController; +use Flarum\Api\Controller\ShowForumController; +use Flarum\Api\Controller\ShowUserController; use Flarum\Api\Serializer\CurrentUserSerializer; use Flarum\Api\Serializer\ForumSerializer; use Flarum\Api\Serializer\UserSerializer; @@ -20,10 +24,12 @@ use Flarum\Post\Event\Posted; use Flarum\Post\Event\Revised; use Flarum\Settings\Event\Deserializing; +use Flarum\User\User; use FoF\Upload\Events\File\WillBeUploaded; use FoF\Upload\Exceptions\ExceptionHandler; use FoF\Upload\Exceptions\InvalidUploadException; use FoF\Upload\Extend\SvgSanitizer; +use FoF\Upload\Extenders\LoadFilesRelationship; return [ (new Extend\Routes('api')) @@ -54,6 +60,21 @@ new Extenders\AddPostDownloadTags(), new Extenders\CreateStorageFolder('tmp'), + (new Extend\Model(User::class)) + ->hasMany('foffiles', File::class, 'actor_id') + ->relationship('foffilesCurrent', function (User $model) { + return $model->foffiles()->where('hide_from_media_manager', false); + }), + + (new Extend\ApiController(ShowUserController::class)) + ->prepareDataForSerialization([LoadFilesRelationship::class, 'countRelations']), + (new Extend\ApiController(ShowForumController::class)) + ->prepareDataForSerialization([LoadFilesRelationship::class, 'countRelations']), + (new Extend\ApiController(ListDiscussionsController::class)) + ->prepareDataForSerialization([LoadFilesRelationship::class, 'countRelations']), + (new Extend\ApiController(ListPostsController::class)) + ->prepareDataForSerialization([LoadFilesRelationship::class, 'countRelations']), + (new Extend\ApiSerializer(ForumSerializer::class)) ->attributes(Extenders\AddForumAttributes::class), diff --git a/src/Extenders/AddForumAttributes.php b/src/Extenders/AddForumAttributes.php index 5fbbe2e5..345d599a 100644 --- a/src/Extenders/AddForumAttributes.php +++ b/src/Extenders/AddForumAttributes.php @@ -36,6 +36,9 @@ public function __invoke(ForumSerializer $serializer) $attributes['fof-upload.canDownload'] = $serializer->getActor()->can('fof-upload.download'); $attributes['fof-upload.composerButtonVisiblity'] = $this->settings->get('fof-upload.composerButtonVisiblity', 'both'); + $serializer->getActor()->load('foffiles'); + $serializer->getActor()->load('foffilesCurrent'); + return $attributes; } } diff --git a/src/Extenders/AddUserAttributes.php b/src/Extenders/AddUserAttributes.php index 3226ec53..6dfce660 100644 --- a/src/Extenders/AddUserAttributes.php +++ b/src/Extenders/AddUserAttributes.php @@ -20,8 +20,8 @@ class AddUserAttributes { public function __invoke(UserSerializer $serializer, User $user, array $attributes): array { - $attributes['fof-upload-uploadCountCurrent'] = File::where('actor_id', $user->id)->where('hide_from_media_manager', false)->count(); - $attributes['fof-upload-uploadCountAll'] = File::where('actor_id', $user->id)->count(); + $attributes['fof-upload-uploadCountCurrent'] = $user->foffiles_current_count; + $attributes['fof-upload-uploadCountAll'] = $user->foffiles_count; return $attributes; } diff --git a/src/Extenders/LoadFilesRelationship.php b/src/Extenders/LoadFilesRelationship.php new file mode 100644 index 00000000..c0e2b96c --- /dev/null +++ b/src/Extenders/LoadFilesRelationship.php @@ -0,0 +1,28 @@ +newCollection($data->pluck('user')); + } + + if ($loadable) { + $loadable->loadCount('foffiles'); + $loadable->loadCount('foffilesCurrent'); + } + } +}