From 99e73a38a9bf79bfcca1e5bd387a74f225dce216 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Tue, 8 Dec 2020 16:59:03 +0100 Subject: [PATCH] Use new extenders (#25) --- extensions/likes/extend.php | 30 ++++++-- .../src/Listener/AddPostLikesRelationship.php | 69 ------------------- 2 files changed, 25 insertions(+), 74 deletions(-) delete mode 100755 extensions/likes/src/Listener/AddPostLikesRelationship.php diff --git a/extensions/likes/extend.php b/extensions/likes/extend.php index e4930674c..c6d1d30ff 100644 --- a/extensions/likes/extend.php +++ b/extensions/likes/extend.php @@ -7,6 +7,8 @@ * LICENSE file that was distributed with this source code. */ +use Flarum\Api\Controller; +use Flarum\Api\Serializer\BasicUserSerializer; use Flarum\Api\Serializer\PostSerializer; use Flarum\Extend; use Flarum\Likes\Event\PostWasLiked; @@ -33,11 +35,29 @@ return [ (new Extend\Notification()) ->type(PostLikedBlueprint::class, PostSerializer::class, ['alert']), - function (Dispatcher $events) { - $events->subscribe(Listener\AddPostLikesRelationship::class); - $events->subscribe(Listener\SaveLikesToDatabase::class); + (new Extend\ApiSerializer(PostSerializer::class)) + ->hasMany('likes', BasicUserSerializer::class) + ->attribute('canLike', function (PostSerializer $serializer, $model) { + return (bool) $serializer->getActor()->can('like', $model); + }), - $events->listen(PostWasLiked::class, Listener\SendNotificationWhenPostIsLiked::class); - $events->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class); + (new Extend\ApiController(Controller\ShowDiscussionController::class)) + ->addInclude('posts.likes'), + + (new Extend\ApiController(Controller\ListPostsController::class)) + ->addInclude('likes'), + (new Extend\ApiController(Controller\ShowPostController::class)) + ->addInclude('likes'), + (new Extend\ApiController(Controller\CreatePostController::class)) + ->addInclude('likes'), + (new Extend\ApiController(Controller\UpdatePostController::class)) + ->addInclude('likes'), + + (new Extend\Event()) + ->listen(PostWasLiked::class, Listener\SendNotificationWhenPostIsLiked::class) + ->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class), + + function (Dispatcher $events) { + $events->subscribe(Listener\SaveLikesToDatabase::class); }, ]; diff --git a/extensions/likes/src/Listener/AddPostLikesRelationship.php b/extensions/likes/src/Listener/AddPostLikesRelationship.php deleted file mode 100755 index bf18d7baf..000000000 --- a/extensions/likes/src/Listener/AddPostLikesRelationship.php +++ /dev/null @@ -1,69 +0,0 @@ -listen(GetApiRelationship::class, [$this, 'getApiAttributes']); - $events->listen(Serializing::class, [$this, 'prepareApiAttributes']); - $events->listen(WillGetData::class, [$this, 'includeLikes']); - } - - /** - * @param GetApiRelationship $event - * @return \Tobscure\JsonApi\Relationship|null - */ - public function getApiAttributes(GetApiRelationship $event) - { - if ($event->isRelationship(PostSerializer::class, 'likes')) { - return $event->serializer->hasMany($event->model, BasicUserSerializer::class, 'likes'); - } - } - - /** - * @param Serializing $event - */ - public function prepareApiAttributes(Serializing $event) - { - if ($event->isSerializer(PostSerializer::class)) { - $event->attributes['canLike'] = (bool) $event->actor->can('like', $event->model); - } - } - - /** - * @param WillGetData $event - */ - public function includeLikes(WillGetData $event) - { - if ($event->isController(Controller\ShowDiscussionController::class)) { - $event->addInclude('posts.likes'); - } - - if ($event->isController(Controller\ListPostsController::class) - || $event->isController(Controller\ShowPostController::class) - || $event->isController(Controller\CreatePostController::class) - || $event->isController(Controller\UpdatePostController::class)) { - $event->addInclude('likes'); - } - } -}