Skip to content

Commit

Permalink
perf: eager load file counts
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Nov 11, 2023
1 parent f25f4ba commit 888efd6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
21 changes: 21 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
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;
use Flarum\Extend;
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'))
Expand Down Expand Up @@ -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),

Expand Down
3 changes: 3 additions & 0 deletions src/Extenders/AddForumAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Extenders/AddUserAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions src/Extenders/LoadFilesRelationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace FoF\Upload\Extenders;

use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Controller\ListPostsController;
use Flarum\User\User;

class LoadFilesRelationship
{
public static function countRelations($controller, $data): void
{
$loadable = null;

if ($data instanceof User) {
$loadable = $data;
} elseif (is_array($data) && isset($data['actor'])) {
$loadable = $data['actor'];
} elseif ($controller instanceof ListPostsController || $controller instanceof ListDiscussionsController) {
$loadable = (new User)->newCollection($data->pluck('user'));
}

if ($loadable) {
$loadable->loadCount('foffiles');
$loadable->loadCount('foffilesCurrent');
}
}
}

0 comments on commit 888efd6

Please sign in to comment.