Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: eager load file counts #358

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
5 changes: 2 additions & 3 deletions src/Extenders/AddUserAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@

use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\User;
use FoF\Upload\File;

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
38 changes: 38 additions & 0 deletions src/Extenders/LoadFilesRelationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of fof/upload.
*
* Copyright (c) FriendsOfFlarum.
* Copyright (c) Flagrow.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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');
}
}
}
Loading