mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 09:25:28 +08:00
Use new extenders (#25)
This commit is contained in:
parent
78288cfbae
commit
99e73a38a9
|
@ -7,6 +7,8 @@
|
||||||
* LICENSE file that was distributed with this source code.
|
* 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\Api\Serializer\PostSerializer;
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
use Flarum\Likes\Event\PostWasLiked;
|
use Flarum\Likes\Event\PostWasLiked;
|
||||||
|
@ -33,11 +35,29 @@ return [
|
||||||
(new Extend\Notification())
|
(new Extend\Notification())
|
||||||
->type(PostLikedBlueprint::class, PostSerializer::class, ['alert']),
|
->type(PostLikedBlueprint::class, PostSerializer::class, ['alert']),
|
||||||
|
|
||||||
function (Dispatcher $events) {
|
(new Extend\ApiSerializer(PostSerializer::class))
|
||||||
$events->subscribe(Listener\AddPostLikesRelationship::class);
|
->hasMany('likes', BasicUserSerializer::class)
|
||||||
$events->subscribe(Listener\SaveLikesToDatabase::class);
|
->attribute('canLike', function (PostSerializer $serializer, $model) {
|
||||||
|
return (bool) $serializer->getActor()->can('like', $model);
|
||||||
|
}),
|
||||||
|
|
||||||
$events->listen(PostWasLiked::class, Listener\SendNotificationWhenPostIsLiked::class);
|
(new Extend\ApiController(Controller\ShowDiscussionController::class))
|
||||||
$events->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::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);
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file is part of Flarum.
|
|
||||||
*
|
|
||||||
* For detailed copyright and license information, please view the
|
|
||||||
* LICENSE file that was distributed with this source code.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Flarum\Likes\Listener;
|
|
||||||
|
|
||||||
use Flarum\Api\Controller;
|
|
||||||
use Flarum\Api\Event\Serializing;
|
|
||||||
use Flarum\Api\Event\WillGetData;
|
|
||||||
use Flarum\Api\Serializer\BasicUserSerializer;
|
|
||||||
use Flarum\Api\Serializer\PostSerializer;
|
|
||||||
use Flarum\Event\GetApiRelationship;
|
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
|
||||||
|
|
||||||
class AddPostLikesRelationship
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param Dispatcher $events
|
|
||||||
*/
|
|
||||||
public function subscribe(Dispatcher $events)
|
|
||||||
{
|
|
||||||
$events->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');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user