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..e5023252 100644 --- a/src/Extenders/AddUserAttributes.php +++ b/src/Extenders/AddUserAttributes.php @@ -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; } diff --git a/src/Extenders/LoadFilesRelationship.php b/src/Extenders/LoadFilesRelationship.php new file mode 100644 index 00000000..4434df0b --- /dev/null +++ b/src/Extenders/LoadFilesRelationship.php @@ -0,0 +1,38 @@ +newCollection($data->pluck('user')); + } + + if ($loadable) { + $loadable->loadCount('foffiles'); + $loadable->loadCount('foffilesCurrent'); + } + } +}