mirror of
https://github.com/flarum/core.git
synced 2025-10-24 05:06:05 +02:00
Convert the rest of the API to new action architecture
Also make some tweaks: - Merge SerializeAction::$include and SerializeAction::$includeAvailable into a keyed boolean array - Set defaults for SerializeAction::$limit and $limitMax - Rename SerializeAction::$sortAvailable to $sortFields
This commit is contained in:
@@ -32,14 +32,21 @@ class CreateAction extends BaseCreateAction
|
||||
public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer';
|
||||
|
||||
/**
|
||||
* The relations that are included by default.
|
||||
* The relationships that are available to be included, and which ones are
|
||||
* included by default.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $include = ['posts', 'startUser', 'lastUser', 'startPost', 'lastPost'];
|
||||
public static $include = [
|
||||
'posts' => true,
|
||||
'startUser' => true,
|
||||
'lastUser' => true,
|
||||
'startPost' => true,
|
||||
'lastPost' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Initialize the action.
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
|
||||
* @param \Flarum\Core\Models\Forum $forum
|
||||
|
||||
@@ -16,7 +16,7 @@ class DeleteAction extends BaseDeleteAction
|
||||
protected $bus;
|
||||
|
||||
/**
|
||||
* Initialize the action.
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
|
||||
*/
|
||||
@@ -26,9 +26,10 @@ class DeleteAction extends BaseDeleteAction
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a discussion according to input from the API request.
|
||||
* Delete a discussion.
|
||||
*
|
||||
* @param \Flarum\Api\Request $request
|
||||
* @param \Illuminate\Http\Response $response
|
||||
* @return void
|
||||
*/
|
||||
protected function delete(Request $request, Response $response)
|
||||
|
||||
@@ -23,46 +23,25 @@ class IndexAction extends SerializeCollectionAction
|
||||
public static $serializer = 'Flarum\Api\Serializers\DiscussionSerializer';
|
||||
|
||||
/**
|
||||
* The relations that are available to be included.
|
||||
* The relationships that are available to be included, and which ones are
|
||||
* included by default.
|
||||
*
|
||||
* @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;
|
||||
public static $include = [
|
||||
'startUser' => true,
|
||||
'lastUser' => true,
|
||||
'startPost' => false,
|
||||
'lastPost' => false,
|
||||
'relevantPosts' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* 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'];
|
||||
public static $sortFields = ['lastTime', 'commentsCount', 'startTime'];
|
||||
|
||||
/**
|
||||
* Instantiate the action.
|
||||
|
||||
@@ -12,16 +12,12 @@ class ShowAction extends SerializeResourceAction
|
||||
use GetsPosts;
|
||||
|
||||
/**
|
||||
* The discussion repository.
|
||||
*
|
||||
* @var DiscussionRepository
|
||||
* @var \Flarum\Core\Repositories\DiscussionRepositoryInterface
|
||||
*/
|
||||
protected $discussions;
|
||||
|
||||
/**
|
||||
* The post repository.
|
||||
*
|
||||
* @var PostRepository
|
||||
* @var \Flarum\Core\Repositories\PostRepositoryInterface
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
@@ -33,23 +29,21 @@ class ShowAction extends SerializeResourceAction
|
||||
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.
|
||||
* The relationships that are available to be included, and which ones are
|
||||
* included by default.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $include = [
|
||||
'startPost', 'lastPost', 'posts',
|
||||
'posts.user', 'posts.user.groups', 'posts.editUser', 'posts.hideUser'
|
||||
'startUser' => false,
|
||||
'lastUser' => false,
|
||||
'startPost' => true,
|
||||
'lastPost' => true,
|
||||
'posts' => true,
|
||||
'posts.user' => true,
|
||||
'posts.user.groups' => true,
|
||||
'posts.editUser' => true,
|
||||
'posts.hideUser' => true
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -64,33 +58,20 @@ class ShowAction extends SerializeResourceAction
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $sortAvailable = ['time'];
|
||||
public static $sortFields = ['time'];
|
||||
|
||||
/**
|
||||
* The default field to sort by.
|
||||
* The default sort field and order to user.
|
||||
*
|
||||
* @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
|
||||
* @param \Flarum\Core\Repositories\DiscussionRepositoryInterface $discussions
|
||||
* @param \Flarum\Core\Repositories\PostRepositoryInterface $posts
|
||||
*/
|
||||
public function __construct(DiscussionRepositoryInterface $discussions, PostRepositoryInterface $posts)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
use Flarum\Core\Commands\EditDiscussionCommand;
|
||||
use Flarum\Core\Commands\ReadDiscussionCommand;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Flarum\Api\Actions\SerializeResourceAction;
|
||||
use Flarum\Api\Actions\Posts\GetsPosts;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
@@ -12,8 +11,6 @@ use Illuminate\Contracts\Bus\Dispatcher;
|
||||
class UpdateAction extends SerializeResourceAction
|
||||
{
|
||||
/**
|
||||
* The command bus.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Bus\Dispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
@@ -30,10 +27,13 @@ class UpdateAction extends SerializeResourceAction
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $include = ['addedPosts', 'addedPosts.user'];
|
||||
public static $include = [
|
||||
'addedPosts' => true,
|
||||
'addedPosts.user' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Initialize the action.
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
|
||||
*/
|
||||
@@ -46,7 +46,8 @@ class UpdateAction extends SerializeResourceAction
|
||||
* 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
|
||||
* @param \Flarum\Api\JsonApiRequest $request
|
||||
* @param \Flarum\Api\JsonApiResponse $response
|
||||
* @return \Flarum\Core\Models\Discussion
|
||||
*/
|
||||
protected function data(JsonApiRequest $request, JsonApiResponse $response)
|
||||
@@ -54,18 +55,12 @@ class UpdateAction extends SerializeResourceAction
|
||||
$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($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.
|
||||
if ($readNumber = $request->get('data.readNumber')) {
|
||||
$state = $this->bus->dispatch(
|
||||
new ReadDiscussionCommand($discussionId, $user, $readNumber)
|
||||
|
||||
Reference in New Issue
Block a user