mirror of
https://github.com/flarum/core.git
synced 2025-10-23 12:46:09 +02:00
Update API action architecture
- An API action handles a Flarum\Api\Request, which is a simple object containing an array of params, the actor, and optionally an HTTP request object - Most API actions use SerializeAction as a base, which parses request input according to the JSON-API spec (creates a JsonApiRequest object), runs the child class method to get data, then serializes it and assigns it to a JsonApiResponse (standard HTTP response with a Tobscure\JsonApi\Document as content) - The JSON-API request input parsing is subject to restrictions as defined by public static properties on the action (i.e. extensible) - Also the actor is given to the serializer instance now, instead of being a static property
This commit is contained in:
@@ -2,40 +2,78 @@
|
||||
|
||||
use Flarum\Core\Commands\StartDiscussionCommand;
|
||||
use Flarum\Core\Commands\ReadDiscussionCommand;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
use Flarum\Core\Models\Forum;
|
||||
use Flarum\Api\Actions\CreateAction as BaseCreateAction;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
use Flarum\Api\JsonApiResponse;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
|
||||
class CreateAction extends BaseAction
|
||||
class CreateAction extends BaseCreateAction
|
||||
{
|
||||
/**
|
||||
* Start a new discussion.
|
||||
* The command bus.
|
||||
*
|
||||
* @return Response
|
||||
* @var \Illuminate\Contracts\Bus\Dispatcher
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
// By default, the only required attributes of a discussion are the
|
||||
// title and the content. We'll extract these from the rbaseequest data
|
||||
// and pass them through to the StartDiscussionCommand.
|
||||
$title = $params->get('data.title');
|
||||
$content = $params->get('data.content');
|
||||
$user = $this->actor->getUser();
|
||||
protected $bus;
|
||||
|
||||
$command = new StartDiscussionCommand($title, $content, $user, app('flarum.forum'));
|
||||
$discussion = $this->dispatch($command, $params);
|
||||
/**
|
||||
* The default forum instance.
|
||||
*
|
||||
* @var \Flarum\Core\Models\Forum
|
||||
*/
|
||||
protected $forum;
|
||||
|
||||
/**
|
||||
* The name of the serializer class to output results with.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer';
|
||||
|
||||
/**
|
||||
* The relations that are included by default.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $include = ['posts', 'startUser', 'lastUser', 'startPost', 'lastPost'];
|
||||
|
||||
/**
|
||||
* Initialize the action.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
|
||||
* @param \Flarum\Core\Models\Forum $forum
|
||||
*/
|
||||
public function __construct(Dispatcher $bus, Forum $forum)
|
||||
{
|
||||
$this->bus = $bus;
|
||||
$this->forum = $forum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a discussion according to input from the API request.
|
||||
*
|
||||
* @param \Flarum\Api\JsonApiRequest $request
|
||||
* @param \Flarum\Api\JsonApiResponse $response
|
||||
* @return \Flarum\Core\Models\Discussion
|
||||
*/
|
||||
protected function create(JsonApiRequest $request, JsonApiResponse $response)
|
||||
{
|
||||
$user = $request->actor->getUser();
|
||||
|
||||
$discussion = $this->bus->dispatch(
|
||||
new StartDiscussionCommand($user, $this->forum, $request->get('data'))
|
||||
);
|
||||
|
||||
// After creating the discussion, we assume that the user has seen all
|
||||
// of the posts in the discussion; thus, we will mark the discussion
|
||||
// as read if they are logged in.
|
||||
if ($user->exists) {
|
||||
$command = new ReadDiscussionCommand($discussion->id, $user, 1);
|
||||
$this->dispatch($command, $params);
|
||||
$this->bus->dispatch(
|
||||
new ReadDiscussionCommand($discussion->id, $user, 1)
|
||||
);
|
||||
}
|
||||
|
||||
$serializer = new DiscussionSerializer(['posts', 'startUser', 'lastUser', 'startPost', 'lastPost']);
|
||||
$document = $this->document()->setData($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
return $discussion;
|
||||
}
|
||||
}
|
||||
|
@@ -1,23 +1,40 @@
|
||||
<?php namespace Flarum\Api\Actions\Discussions;
|
||||
|
||||
use Flarum\Core\Commands\DeleteDiscussionCommand;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Actions\DeleteAction as BaseDeleteAction;
|
||||
use Flarum\Api\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
|
||||
class DeleteAction extends BaseAction
|
||||
class DeleteAction extends BaseDeleteAction
|
||||
{
|
||||
/**
|
||||
* Delete a discussion.
|
||||
* The command bus.
|
||||
*
|
||||
* @return Response
|
||||
* @var \Illuminate\Contracts\Bus\Dispatcher
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
protected $bus;
|
||||
|
||||
/**
|
||||
* Initialize the action.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
|
||||
*/
|
||||
public function __construct(Dispatcher $bus)
|
||||
{
|
||||
$discussionId = $params->get('id');
|
||||
$this->bus = $bus;
|
||||
}
|
||||
|
||||
$command = new DeleteDiscussionCommand($discussionId, $this->actor->getUser());
|
||||
$this->dispatch($command, $params);
|
||||
|
||||
return $this->respondWithoutContent();
|
||||
/**
|
||||
* Delete a discussion according to input from the API request.
|
||||
*
|
||||
* @param \Flarum\Api\Request $request
|
||||
* @return void
|
||||
*/
|
||||
protected function delete(Request $request, Response $response)
|
||||
{
|
||||
$this->bus->dispatch(
|
||||
new DeleteDiscussionCommand($request->get('id'), $request->actor->getUser())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -2,12 +2,11 @@
|
||||
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearchCriteria;
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearcher;
|
||||
use Flarum\Support\Actor;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
use Flarum\Api\Actions\SerializeCollectionAction;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
use Flarum\Api\JsonApiResponse;
|
||||
|
||||
class IndexAction extends BaseAction
|
||||
class IndexAction extends SerializeCollectionAction
|
||||
{
|
||||
/**
|
||||
* The discussion searcher.
|
||||
@@ -16,62 +15,90 @@ class IndexAction extends BaseAction
|
||||
*/
|
||||
protected $searcher;
|
||||
|
||||
/**
|
||||
* The name of the serializer class to output results with.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer';
|
||||
|
||||
/**
|
||||
* The relations that are available to be included.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $includeAvailable = ['startUser', 'lastUser', 'startPost', 'lastPost', 'relevantPosts'];
|
||||
|
||||
/**
|
||||
* The relations that are included by default.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $include = ['startUser', 'lastUser'];
|
||||
|
||||
/**
|
||||
* The maximum number of records that can be requested.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
public static $limitMax = 50;
|
||||
|
||||
/**
|
||||
* The number of records included by default.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
public static $limit = 20;
|
||||
|
||||
/**
|
||||
* The fields that are available to be sorted by.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $sortAvailable = ['lastTime', 'commentsCount', 'startTime'];
|
||||
|
||||
/**
|
||||
* The default field to sort by.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $sort = ['lastTime' => 'desc'];
|
||||
|
||||
/**
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param \Flarum\Core\Search\Discussions\DiscussionSearcher $searcher
|
||||
*/
|
||||
public function __construct(Actor $actor, DiscussionSearcher $searcher)
|
||||
public function __construct(DiscussionSearcher $searcher)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->searcher = $searcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a list of discussions.
|
||||
* Get the discussion results, ready to be serialized and assigned to the
|
||||
* document response.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param \Flarum\Api\JsonApiRequest $request
|
||||
* @param \Flarum\Api\JsonApiResponse $response
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
protected function data(JsonApiRequest $request, JsonApiResponse $response)
|
||||
{
|
||||
$query = $params->get('q');
|
||||
$start = $params->start();
|
||||
$include = $params->included(['startUser', 'lastUser', 'startPost', 'lastPost', 'relevantPosts']);
|
||||
$count = $params->count(20, 50);
|
||||
$sort = $params->sort(['', 'lastPost', 'replies', 'created']);
|
||||
$criteria = new DiscussionSearchCriteria(
|
||||
$request->actor->getUser(),
|
||||
$request->get('q'),
|
||||
$request->sort
|
||||
);
|
||||
|
||||
// Set up the discussion searcher with our search criteria, and get the
|
||||
// requested range of results with the necessary relations loaded.
|
||||
$criteria = new DiscussionSearchCriteria($this->actor->getUser(), $query, $sort['field'], $sort['order']);
|
||||
$load = array_merge($include, ['state']);
|
||||
|
||||
$results = $this->searcher->search($criteria, $count, $start, $load);
|
||||
|
||||
$document = $this->document();
|
||||
$load = array_merge($request->include, ['state']);
|
||||
$results = $this->searcher->search($criteria, $request->limit, $request->offset, $load);
|
||||
|
||||
if (($total = $results->getTotal()) !== null) {
|
||||
$document->addMeta('total', $total);
|
||||
$response->content->addMeta('total', $total);
|
||||
}
|
||||
|
||||
// If there are more results, then we need to construct a URL to the
|
||||
// next results page and add that to the metadata. We do this by
|
||||
// compacting all of the valid query parameters which have been
|
||||
// specified.
|
||||
if ($results->areMoreResults()) {
|
||||
$start += $count;
|
||||
$sort = $sort['string'];
|
||||
$input = array_filter(compact('query', 'sort', 'start', 'count')) + ['include' => implode(',', $include)];
|
||||
$moreUrl = $this->buildUrl('discussions.index', [], $input);
|
||||
} else {
|
||||
$moreUrl = '';
|
||||
}
|
||||
$document->addMeta('moreUrl', $moreUrl);
|
||||
// $response->content->addMeta('moreUrl', $moreUrl);
|
||||
|
||||
// Finally, we can set up the discussion serializer and use it to create
|
||||
// a collection of discussion results.
|
||||
$serializer = new DiscussionSerializer($include);
|
||||
$document->setData($serializer->collection($results->getDiscussions()));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
return $results->getDiscussions();
|
||||
}
|
||||
}
|
||||
|
@@ -1,14 +1,13 @@
|
||||
<?php namespace Flarum\Api\Actions\Discussions;
|
||||
|
||||
use Flarum\Support\Actor;
|
||||
use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionRepository;
|
||||
use Flarum\Core\Repositories\PostRepositoryInterface as PostRepository;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Core\Repositories\DiscussionRepositoryInterface;
|
||||
use Flarum\Core\Repositories\PostRepositoryInterface;
|
||||
use Flarum\Api\Actions\SerializeResourceAction;
|
||||
use Flarum\Api\Actions\Posts\GetsPosts;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
use Flarum\Api\JsonApiResponse;
|
||||
|
||||
class ShowAction extends BaseAction
|
||||
class ShowAction extends SerializeResourceAction
|
||||
{
|
||||
use GetsPosts;
|
||||
|
||||
@@ -26,48 +25,104 @@ class ShowAction extends BaseAction
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* The name of the serializer class to output results with.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer';
|
||||
|
||||
/**
|
||||
* The relations that are available to be included.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $includeAvailable = [
|
||||
'startUser', 'lastUser', 'startPost', 'lastPost', 'posts',
|
||||
'posts.user', 'posts.user.groups', 'posts.editUser', 'posts.hideUser'
|
||||
];
|
||||
|
||||
/**
|
||||
* The relations that are included by default.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $include = [
|
||||
'startPost', 'lastPost', 'posts',
|
||||
'posts.user', 'posts.user.groups', 'posts.editUser', 'posts.hideUser'
|
||||
];
|
||||
|
||||
/**
|
||||
* The relations that are linked by default.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $link = ['posts'];
|
||||
|
||||
/**
|
||||
* The fields that are available to be sorted by.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $sortAvailable = ['time'];
|
||||
|
||||
/**
|
||||
* The default field to sort by.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $sort = ['time' => 'asc'];
|
||||
|
||||
/**
|
||||
* The maximum number of records that can be requested.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
public static $limitMax = 50;
|
||||
|
||||
/**
|
||||
* The number of records included by default.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
public static $limit = 20;
|
||||
|
||||
/**
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param PostRepository $posts
|
||||
*/
|
||||
public function __construct(Actor $actor, DiscussionRepository $discussions, PostRepository $posts)
|
||||
public function __construct(DiscussionRepositoryInterface $discussions, PostRepositoryInterface $posts)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->discussions = $discussions;
|
||||
$this->posts = $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a single discussion.
|
||||
* Get a single discussion, ready to be serialized and assigned to the
|
||||
* JsonApi response.
|
||||
*
|
||||
* @return Response
|
||||
* @param \Flarum\Api\JsonApiRequest $request
|
||||
* @param \Flarum\Api\JsonApiResponse $response
|
||||
* @return \Flarum\Core\Models\Discussion
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
protected function data(JsonApiRequest $request, JsonApiResponse $response)
|
||||
{
|
||||
$include = $params->included(['startPost', 'lastPost', 'posts']);
|
||||
$user = $request->actor->getUser();
|
||||
|
||||
$user = $this->actor->getUser();
|
||||
$discussion = $this->discussions->findOrFail($request->get('id'), $user);
|
||||
|
||||
$discussion = $this->discussions->findOrFail($params->get('id'), $user);
|
||||
$discussion->posts_ids = $discussion->posts()->whereCan($user, 'view')->get(['id'])->fetch('id')->all();
|
||||
|
||||
if (in_array('posts', $include)) {
|
||||
$relations = ['user', 'user.groups', 'editUser', 'hideUser'];
|
||||
$discussion->posts = $this->getPosts($params, ['discussion_id' => $discussion->id])->load($relations);
|
||||
if (in_array('posts', $request->include)) {
|
||||
$length = strlen($prefix = 'posts.');
|
||||
$relations = array_filter(array_map(function ($relationship) use ($prefix, $length) {
|
||||
return substr($relationship, 0, $length) === $prefix ? substr($relationship, $length) : false;
|
||||
}, $request->include));
|
||||
|
||||
$include = array_merge($include, array_map(function ($relation) {
|
||||
return 'posts.'.$relation;
|
||||
}, $relations));
|
||||
$discussion->posts = $this->getPosts($request, ['discussion_id' => $discussion->id])->load($relations);
|
||||
}
|
||||
|
||||
// Set up the discussion serializer, which we will use to create the
|
||||
// document's primary resource. As well as including the requested
|
||||
// relations, we will specify that we want the 'posts' relation to be
|
||||
// linked so that a list of post IDs will show up in the response.
|
||||
$serializer = new DiscussionSerializer($include, ['posts']);
|
||||
$document = $this->document()->setData($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
return $discussion;
|
||||
}
|
||||
}
|
||||
|
@@ -3,57 +3,77 @@
|
||||
use Flarum\Core\Commands\EditDiscussionCommand;
|
||||
use Flarum\Core\Commands\ReadDiscussionCommand;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
use Flarum\Api\Actions\SerializeResourceAction;
|
||||
use Flarum\Api\Actions\Posts\GetsPosts;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
use Flarum\Api\JsonApiResponse;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
|
||||
class UpdateAction extends BaseAction
|
||||
class UpdateAction extends SerializeResourceAction
|
||||
{
|
||||
/**
|
||||
* Edit a discussion. Allows renaming the discussion, and updating its read
|
||||
* state with regards to the current user.
|
||||
* The command bus.
|
||||
*
|
||||
* @return Response
|
||||
* @var \Illuminate\Contracts\Bus\Dispatcher
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
protected $bus;
|
||||
|
||||
/**
|
||||
* The name of the serializer class to output results with.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer';
|
||||
|
||||
/**
|
||||
* The relations that are included by default.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $include = ['addedPosts', 'addedPosts.user'];
|
||||
|
||||
/**
|
||||
* Initialize the action.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
|
||||
*/
|
||||
public function __construct(Dispatcher $bus)
|
||||
{
|
||||
$discussionId = $params->get('id');
|
||||
$user = $this->actor->getUser();
|
||||
$this->bus = $bus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a discussion according to input from the API request, and return
|
||||
* it ready to be serialized and assigned to the JsonApi response.
|
||||
*
|
||||
* @param \Flarum\Api\Request $request
|
||||
* @return \Flarum\Core\Models\Discussion
|
||||
*/
|
||||
protected function data(JsonApiRequest $request, JsonApiResponse $response)
|
||||
{
|
||||
$user = $request->actor->getUser();
|
||||
$discussionId = $request->get('id');
|
||||
|
||||
// First, we will run the EditDiscussionCommand. This will update the
|
||||
// discussion's direct properties; by default, this is just the title.
|
||||
// As usual, however, we will fire an event to allow plugins to update
|
||||
// additional properties.
|
||||
if ($data = array_except($params->get('data'), ['readNumber'])) {
|
||||
try {
|
||||
$command = new EditDiscussionCommand($discussionId, $user);
|
||||
$this->hydrate($command, $params->get('data'));
|
||||
$discussion = $this->dispatch($command, $params);
|
||||
} catch (PermissionDeniedException $e) {
|
||||
// Temporary fix. See @todo below
|
||||
$discussion = \Flarum\Core\Models\Discussion::find($discussionId);
|
||||
}
|
||||
if ($data = array_except($request->get('data'), ['readNumber'])) {
|
||||
$discussion = $this->bus->dispatch(
|
||||
new EditDiscussionCommand($discussionId, $user, $data)
|
||||
);
|
||||
}
|
||||
|
||||
// Next, if a read number was specified in the request, we will run the
|
||||
// ReadDiscussionCommand.
|
||||
//
|
||||
// @todo Currently, if the user doesn't have permission to edit a
|
||||
// discussion, they're unable to update their readNumber because a
|
||||
// PermissionDeniedException is thrown by the
|
||||
// EditDiscussionCommand above. So this needs to be extracted into
|
||||
// its own endpoint.
|
||||
if ($readNumber = $params->get('data.readNumber')) {
|
||||
$command = new ReadDiscussionCommand($discussionId, $user, $readNumber);
|
||||
$discussion = $this->dispatch($command, $params)->discussion;
|
||||
if ($readNumber = $request->get('data.readNumber')) {
|
||||
$state = $this->bus->dispatch(
|
||||
new ReadDiscussionCommand($discussionId, $user, $readNumber)
|
||||
);
|
||||
|
||||
$discussion = $state->discussion;
|
||||
}
|
||||
|
||||
// Presumably, the discussion was updated successfully. (One of the command
|
||||
// handlers would have thrown an exception if not.) We set this
|
||||
// discussion as our document's primary element.
|
||||
$serializer = new DiscussionSerializer(['addedPosts', 'addedPosts.user']);
|
||||
$document = $this->document()->setData($serializer->resource($discussion));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
return $discussion;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user