From 256e821289bc1a5d43016408753ca9d469f5ecf4 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Tue, 8 Dec 2020 17:15:52 +0100 Subject: [PATCH] Use new extenders (#58) --- extensions/mentions/extend.php | 46 +++++++---- .../src/{Listener => }/FilterVisiblePosts.php | 28 +++---- .../AddPostMentionedByRelationship.php | 77 ------------------- 3 files changed, 46 insertions(+), 105 deletions(-) rename extensions/mentions/src/{Listener => }/FilterVisiblePosts.php (73%) delete mode 100755 extensions/mentions/src/Listener/AddPostMentionedByRelationship.php diff --git a/extensions/mentions/extend.php b/extensions/mentions/extend.php index 7863c7b42..24667bd49 100644 --- a/extensions/mentions/extend.php +++ b/extensions/mentions/extend.php @@ -7,12 +7,14 @@ * LICENSE file that was distributed with this source code. */ -use Flarum\Api\Event\WillSerializeData; +use Flarum\Api\Controller; +use Flarum\Api\Serializer\BasicPostSerializer; use Flarum\Api\Serializer\PostSerializer; use Flarum\Event\ConfigurePostsQuery; use Flarum\Extend; use Flarum\Formatter\Event\Rendering; use Flarum\Mentions\ConfigureMentions; +use Flarum\Mentions\FilterVisiblePosts; use Flarum\Mentions\Listener; use Flarum\Mentions\Notification\PostMentionedBlueprint; use Flarum\Mentions\Notification\UserMentionedBlueprint; @@ -47,21 +49,35 @@ return [ ->type(PostMentionedBlueprint::class, PostSerializer::class, ['alert']) ->type(UserMentionedBlueprint::class, PostSerializer::class, ['alert']), + (new Extend\ApiSerializer(BasicPostSerializer::class)) + ->hasMany('mentionedBy', BasicPostSerializer::class) + ->hasMany('mentionsPosts', BasicPostSerializer::class) + ->hasMany('mentionsUsers', BasicPostSerializer::class), + + (new Extend\ApiController(Controller\ShowDiscussionController::class)) + ->addInclude(['posts.mentionedBy', 'posts.mentionedBy.user', 'posts.mentionedBy.discussion']), + + (new Extend\ApiController(Controller\ShowPostController::class)) + ->addInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion']), + + (new Extend\ApiController(Controller\ListPostsController::class)) + ->addInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion']), + + (new Extend\ApiController(Controller\CreatePostController::class)) + ->addInclude(['mentionsPosts', 'mentionsPosts.mentionedBy']), + + (new Extend\ApiController(Controller\AbstractSerializeController::class)) + ->prepareDataForSerialization(FilterVisiblePosts::class), + + (new Extend\Event()) + ->listen(Posted::class, Listener\UpdateMentionsMetadataWhenVisible::class) + ->listen(Restored::class, Listener\UpdateMentionsMetadataWhenVisible::class) + ->listen(Revised::class, Listener\UpdateMentionsMetadataWhenVisible::class) + ->listen(Hidden::class, Listener\UpdateMentionsMetadataWhenInvisible::class) + ->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class) + ->listen(ConfigurePostsQuery::class, Listener\AddFilterByMentions::class), + function (Dispatcher $events) { - $events->listen(WillSerializeData::class, Listener\FilterVisiblePosts::class); - $events->subscribe(Listener\AddPostMentionedByRelationship::class); - - $events->listen( - [Posted::class, Restored::class, Revised::class], - Listener\UpdateMentionsMetadataWhenVisible::class - ); - $events->listen( - [Deleted::class, Hidden::class], - Listener\UpdateMentionsMetadataWhenInvisible::class - ); - - $events->listen(ConfigurePostsQuery::class, Listener\AddFilterByMentions::class); - $events->listen(Rendering::class, Listener\FormatPostMentions::class); $events->listen(Rendering::class, Listener\FormatUserMentions::class); }, diff --git a/extensions/mentions/src/Listener/FilterVisiblePosts.php b/extensions/mentions/src/FilterVisiblePosts.php similarity index 73% rename from extensions/mentions/src/Listener/FilterVisiblePosts.php rename to extensions/mentions/src/FilterVisiblePosts.php index c8f63eec2..df72ddbda 100755 --- a/extensions/mentions/src/Listener/FilterVisiblePosts.php +++ b/extensions/mentions/src/FilterVisiblePosts.php @@ -7,13 +7,13 @@ * LICENSE file that was distributed with this source code. */ -namespace Flarum\Mentions\Listener; +namespace Flarum\Mentions; use Flarum\Api\Controller; -use Flarum\Api\Event\WillSerializeData; use Flarum\Post\CommentPost; use Flarum\Post\PostRepository; use Illuminate\Database\Eloquent\Collection; +use Psr\Http\Message\ServerRequestInterface; class FilterVisiblePosts { @@ -38,24 +38,26 @@ class FilterVisiblePosts * additional posts so that the user can't see any posts which they don't * have access to. * - * @param WillSerializeData $event + * @param Controller\AbstractSerializeController $controller + * @param mixed $data */ - public function handle(WillSerializeData $event) + public function __invoke(Controller\AbstractSerializeController $controller, $data, ServerRequestInterface $request) { // Firstly we gather a list of posts contained within the API document. // This will vary according to the API endpoint that is being accessed. - if ($event->isController(Controller\ShowDiscussionController::class)) { - $posts = $event->data->posts; - } elseif ($event->isController(Controller\ShowPostController::class) - || $event->isController(Controller\CreatePostController::class) - || $event->isController(Controller\UpdatePostController::class)) { - $posts = [$event->data]; - } elseif ($event->isController(Controller\ListPostsController::class)) { - $posts = $event->data; + if ($controller instanceof Controller\ShowDiscussionController) { + $posts = $data->posts; + } elseif ($controller instanceof Controller\ShowPostController + || $controller instanceof Controller\CreatePostController + || $controller instanceof Controller\UpdatePostController) { + $posts = [$data]; + } elseif ($controller instanceof Controller\ListPostsController) { + $posts = $data; } if (isset($posts)) { $posts = new Collection($posts); + $actor = $request->getAttribute('actor'); $posts = $posts->filter(function ($post) { return $post instanceof CommentPost; @@ -75,7 +77,7 @@ class FilterVisiblePosts $ids = array_merge($ids, $post->mentionedBy->pluck('id')->all()); } - $ids = $this->posts->filterVisibleIds($ids, $event->actor); + $ids = $this->posts->filterVisibleIds($ids, $actor); // Finally, go back through each of the posts and filter out any // of the posts in the relationship data that we now know are diff --git a/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php b/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php deleted file mode 100755 index 7ad4cc50a..000000000 --- a/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php +++ /dev/null @@ -1,77 +0,0 @@ -listen(GetApiRelationship::class, [$this, 'getApiRelationship']); - $events->listen(WillGetData::class, [$this, 'includeRelationships']); - } - - /** - * @param GetApiRelationship $event - * @return \Tobscure\JsonApi\Relationship|null - */ - public function getApiRelationship(GetApiRelationship $event) - { - if ($event->isRelationship(BasicPostSerializer::class, 'mentionedBy')) { - return $event->serializer->hasMany($event->model, BasicPostSerializer::class, 'mentionedBy'); - } - - if ($event->isRelationship(BasicPostSerializer::class, 'mentionsPosts')) { - return $event->serializer->hasMany($event->model, BasicPostSerializer::class, 'mentionsPosts'); - } - - if ($event->isRelationship(BasicPostSerializer::class, 'mentionsUsers')) { - return $event->serializer->hasMany($event->model, BasicPostSerializer::class, 'mentionsUsers'); - } - } - - /** - * @param WillGetData $event - */ - public function includeRelationships(WillGetData $event) - { - if ($event->isController(Controller\ShowDiscussionController::class)) { - $event->addInclude([ - 'posts.mentionedBy', - 'posts.mentionedBy.user', - 'posts.mentionedBy.discussion' - ]); - } - - if ($event->isController(Controller\ShowPostController::class) - || $event->isController(Controller\ListPostsController::class)) { - $event->addInclude([ - 'mentionedBy', - 'mentionedBy.user', - 'mentionedBy.discussion' - ]); - } - - if ($event->isController(Controller\CreatePostController::class)) { - $event->addInclude([ - 'mentionsPosts', - 'mentionsPosts.mentionedBy' - ]); - } - } -}