Use new extenders (#102)

This commit is contained in:
Sami Mazouz 2020-12-08 20:20:50 +01:00 committed by GitHub
parent 2fd76a8d75
commit 384f17ea94
5 changed files with 68 additions and 178 deletions

View File

@ -7,14 +7,18 @@
* LICENSE file that was distributed with this source code.
*/
use Flarum\Api\Event\Serializing;
use Flarum\Api\Controller as FlarumController;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving;
use Flarum\Extend;
use Flarum\Tags\Access;
use Flarum\Tags\Api\Controller;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Content;
use Flarum\Tags\Listener;
use Flarum\Tags\LoadForumTagsRelationship;
use Flarum\Tags\Tag;
use Illuminate\Contracts\Events\Dispatcher;
@ -39,17 +43,40 @@ return [
(new Extend\Model(Discussion::class))
->belongsToMany('tags', Tag::class, 'discussion_tag'),
(new Extend\ApiSerializer(ForumSerializer::class))
->hasMany('tags', TagSerializer::class),
(new Extend\ApiSerializer(DiscussionSerializer::class))
->hasMany('tags', TagSerializer::class)
->attribute('canTag', function (DiscussionSerializer $serializer, $model) {
return $serializer->getActor()->can('tag', $model);
}),
(new Extend\ApiController(FlarumController\ListDiscussionsController::class))
->addInclude(['tags', 'tags.state']),
(new Extend\ApiController(FlarumController\ShowDiscussionController::class))
->addInclude(['tags', 'tags.state']),
(new Extend\ApiController(FlarumController\CreateDiscussionController::class))
->addInclude(['tags', 'tags.state']),
(new Extend\ApiController(FlarumController\ShowForumController::class))
->addInclude(['tags', 'tags.lastPostedDiscussion', 'tags.parent'])
->prepareDataForSerialization(LoadForumTagsRelationship::class),
(new Extend\Settings())
->serializeToForum('minPrimaryTags', 'flarum-tags.min_primary_tags')
->serializeToForum('maxPrimaryTags', 'flarum-tags.max_primary_tags')
->serializeToForum('minSecondaryTags', 'flarum-tags.min_secondary_tags')
->serializeToForum('maxSecondaryTags', 'flarum-tags.max_secondary_tags'),
new Extend\Locales(__DIR__.'/locale'),
(new Extend\View)
->namespace('tags', __DIR__.'/views'),
function (Dispatcher $events) {
$events->subscribe(Listener\AddDiscussionTagsRelationship::class);
$events->subscribe(Listener\AddForumTagsRelationship::class);
$events->listen(Serializing::class, Listener\PrepareForumTagsApiAttributes::class);
$events->subscribe(Listener\CreatePostWhenTagsAreChanged::class);
$events->subscribe(Listener\FilterDiscussionListByTags::class);
$events->subscribe(Listener\FilterPostsQueryByTag::class);

View File

@ -1,63 +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\Tags\Listener;
use Flarum\Api\Controller;
use Flarum\Api\Event\Serializing;
use Flarum\Api\Event\WillGetData;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Event\GetApiRelationship;
use Illuminate\Contracts\Events\Dispatcher;
class AddDiscussionTagsRelationship
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GetApiRelationship::class, [$this, 'getApiRelationship']);
$events->listen(WillGetData::class, [$this, 'includeTagsRelationship']);
$events->listen(Serializing::class, [$this, 'prepareApiAttributes']);
}
/**
* @param GetApiRelationship $event
* @return \Tobscure\JsonApi\Relationship|null
*/
public function getApiRelationship(GetApiRelationship $event)
{
if ($event->isRelationship(DiscussionSerializer::class, 'tags')) {
return $event->serializer->hasMany($event->model, 'Flarum\Tags\Api\Serializer\TagSerializer', 'tags');
}
}
/**
* @param WillGetData $event
*/
public function includeTagsRelationship(WillGetData $event)
{
if ($event->isController(Controller\ListDiscussionsController::class)
|| $event->isController(Controller\ShowDiscussionController::class)
|| $event->isController(Controller\CreateDiscussionController::class)) {
$event->addInclude(['tags', 'tags.state']);
}
}
/**
* @param Serializing $event
*/
public function prepareApiAttributes(Serializing $event)
{
if ($event->isSerializer(DiscussionSerializer::class)) {
$event->attributes['canTag'] = $event->actor->can('tag', $event->model);
}
}
}

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\Tags\Listener;
use Flarum\Api\Controller\ShowForumController;
use Flarum\Api\Event\WillGetData;
use Flarum\Api\Event\WillSerializeData;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Event\GetApiRelationship;
use Flarum\Tags\Tag;
use Illuminate\Contracts\Events\Dispatcher;
class AddForumTagsRelationship
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GetApiRelationship::class, [$this, 'getApiRelationship']);
$events->listen(WillSerializeData::class, [$this, 'loadTagsRelationship']);
$events->listen(WillGetData::class, [$this, 'includeTagsRelationship']);
}
/**
* @param GetApiRelationship $event
* @return \Tobscure\JsonApi\Relationship|null
*/
public function getApiRelationship(GetApiRelationship $event)
{
if ($event->isRelationship(ForumSerializer::class, 'tags')) {
return $event->serializer->hasMany($event->model, 'Flarum\Tags\Api\Serializer\TagSerializer', 'tags');
}
}
/**
* @param WillSerializeData $event
*/
public function loadTagsRelationship(WillSerializeData $event)
{
// Expose the complete tag list to clients by adding it as a
// relationship to the /api endpoint. Since the Forum model
// doesn't actually have a tags relationship, we will manually load and
// assign the tags data to it using an event listener.
if ($event->isController(ShowForumController::class)) {
$event->data['tags'] = Tag::whereVisibleTo($event->actor)
->withStateFor($event->actor)
->with(['parent', 'lastPostedDiscussion'])
->get();
}
}
/**
* @param WillGetData $event
*/
public function includeTagsRelationship(WillGetData $event)
{
if ($event->isController(ShowForumController::class)) {
$event->addInclude(['tags', 'tags.lastPostedDiscussion', 'tags.parent']);
}
}
}

View File

@ -1,40 +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\Tags\Listener;
use Flarum\Api\Event\Serializing;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
class PrepareForumTagsApiAttributes
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function handle(Serializing $event)
{
if ($event->isSerializer(ForumSerializer::class)) {
$event->attributes['minPrimaryTags'] = $this->settings->get('flarum-tags.min_primary_tags');
$event->attributes['maxPrimaryTags'] = $this->settings->get('flarum-tags.max_primary_tags');
$event->attributes['minSecondaryTags'] = $this->settings->get('flarum-tags.min_secondary_tags');
$event->attributes['maxSecondaryTags'] = $this->settings->get('flarum-tags.max_secondary_tags');
}
}
}

View File

@ -0,0 +1,35 @@
<?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\Tags;
use Flarum\Api\Controller\ShowForumController;
use Psr\Http\Message\ServerRequestInterface;
class LoadForumTagsRelationship
{
/**
* @param ShowForumController $controller
* @param $data
* @param ServerRequestInterface $request
*/
public function __invoke(ShowForumController $controller, &$data, ServerRequestInterface $request)
{
$actor = $request->getAttribute('actor');
// Expose the complete tag list to clients by adding it as a
// relationship to the /api endpoint. Since the Forum model
// doesn't actually have a tags relationship, we will manually load and
// assign the tags data to it using an event listener.
$data['tags'] = Tag::whereVisibleTo($actor)
->withStateFor($actor)
->with(['parent', 'lastPostedDiscussion'])
->get();
}
}