mirror of
https://github.com/flarum/core.git
synced 2025-08-14 04:14:06 +02:00
Use new extenders (#58)
This commit is contained in:
@@ -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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user