1
0
mirror of https://github.com/flarum/core.git synced 2025-08-19 23:01:56 +02:00

Use new extenders (#25)

This commit is contained in:
Sami Mazouz
2020-12-08 16:59:03 +01:00
committed by GitHub
parent 78288cfbae
commit 99e73a38a9
2 changed files with 25 additions and 74 deletions

View File

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