mirror of
https://github.com/flarum/framework.git
synced 2025-02-01 04:09:45 +08:00
Use new extenders (#58)
This commit is contained in:
parent
ab27fd452d
commit
256e821289
|
@ -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);
|
||||
},
|
||||
|
|
|
@ -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
|
|
@ -1,77 +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\Mentions\Listener;
|
||||
|
||||
use Flarum\Api\Controller;
|
||||
use Flarum\Api\Event\WillGetData;
|
||||
use Flarum\Api\Serializer\BasicPostSerializer;
|
||||
use Flarum\Event\GetApiRelationship;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddPostMentionedByRelationship
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user