mirror of
https://github.com/flarum/core.git
synced 2025-10-12 15:34:26 +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:
@@ -1,19 +1,17 @@
|
||||
<?php namespace Flarum\Api\Actions\Posts;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\JsonApiRequest;
|
||||
|
||||
trait GetsPosts
|
||||
{
|
||||
protected function getPosts(ApiParams $params, $where)
|
||||
protected function getPosts(JsonApiRequest $request, array $where)
|
||||
{
|
||||
$sort = $params->sort(['time']);
|
||||
$count = $params->count(20, 50);
|
||||
$user = $this->actor->getUser();
|
||||
$user = $request->actor->getUser();
|
||||
|
||||
if (isset($where['discussion_id']) && ($near = $params->get('near')) > 1) {
|
||||
if (isset($where['discussion_id']) && ($near = $request->get('near')) > 1) {
|
||||
$start = $this->posts->getIndexForNumber($where['discussion_id'], $near, $user);
|
||||
$start = max(0, $start - $count / 2);
|
||||
$start = max(0, $request->offset - $request->limit / 2);
|
||||
} else {
|
||||
$start = 0;
|
||||
}
|
||||
@@ -21,10 +19,9 @@ trait GetsPosts
|
||||
return $this->posts->findWhere(
|
||||
$where,
|
||||
$user,
|
||||
$sort['field'],
|
||||
$sort['order'] ?: 'asc',
|
||||
$count,
|
||||
$start
|
||||
$request->sort,
|
||||
$request->limit,
|
||||
$request->offset
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user