fix: regressions

This commit is contained in:
Sami Mazouz 2024-02-26 13:47:47 +01:00
parent 8bcc2ffb40
commit f6cd055dbe
No known key found for this signature in database
8 changed files with 61 additions and 44 deletions

View File

@ -90,7 +90,7 @@ trait ExtractsListingParams
return [ return [
'filter' => RequestUtil::extractFilter($context->request), 'filter' => RequestUtil::extractFilter($context->request),
'sort' => RequestUtil::extractSort($context->request, $this->defaultSort, $this->getAvailableSorts($context)), 'sort' => RequestUtil::extractSort($context->request, $this->defaultSort, $this->getAvailableSorts($context)),
'limit' => $limit = (RequestUtil::extractLimit($context->request, $this->limit, $this->maxLimit) ?? -1), 'limit' => $limit = (RequestUtil::extractLimit($context->request, $this->limit, $this->maxLimit) ?? null),
'offset' => RequestUtil::extractOffset($context->request, $limit), 'offset' => RequestUtil::extractOffset($context->request, $limit),
]; ];
} }

View File

@ -193,13 +193,20 @@ class DiscussionResource extends AbstractDatabaseResource
->includable() ->includable()
->type('posts'), ->type('posts'),
Schema\Relationship\ToMany::make('posts') Schema\Relationship\ToMany::make('posts')
->withLinkage() ->withLinkage(function (Context $context) {
return $context->collection instanceof self && $context->endpoint instanceof Endpoint\Show;
})
->includable() ->includable()
->get(function (Discussion $discussion, Context $context) { ->get(function (Discussion $discussion, Context $context) {
if ($context->collection instanceof self && $context->endpoint instanceof Endpoint\Show) { $showingDiscussion = $context->collection instanceof self && $context->endpoint instanceof Endpoint\Show;
if (! $showingDiscussion) {
return fn () => $discussion->posts->all();
}
$actor = $context->getActor(); $actor = $context->getActor();
$limit = $context->endpoint->extractLimitValue($context, $context->endpoint->defaultExtracts($context)); $limit = PostResource::$defaultLimit;
if (($near = Arr::get($context->request->getQueryParams(), 'page.near')) > 1) { if (($near = Arr::get($context->request->getQueryParams(), 'page.near')) > 1) {
$offset = $this->posts->getIndexForNumber($discussion->id, $near, $actor); $offset = $this->posts->getIndexForNumber($discussion->id, $near, $actor);
@ -226,9 +233,6 @@ class DiscussionResource extends AbstractDatabaseResource
array_splice($allPosts, $offset, $limit, $loadedPosts); array_splice($allPosts, $offset, $limit, $loadedPosts);
return $allPosts; return $allPosts;
}
return [];
}), }),
Schema\Relationship\ToOne::make('mostRelevantPost') Schema\Relationship\ToOne::make('mostRelevantPost')
->visible(fn (Discussion $model, Context $context) => $context->endpoint instanceof Endpoint\Index) ->visible(fn (Discussion $model, Context $context) => $context->endpoint instanceof Endpoint\Index)

View File

@ -10,6 +10,7 @@ use Flarum\Bus\Dispatcher;
use Flarum\Notification\Command\ReadNotification; use Flarum\Notification\Command\ReadNotification;
use Flarum\Notification\Notification; use Flarum\Notification\Notification;
use Flarum\Notification\NotificationRepository; use Flarum\Notification\NotificationRepository;
use Illuminate\Database\Eloquent\Builder;
use Tobyz\JsonApiServer\Pagination\Pagination; use Tobyz\JsonApiServer\Pagination\Pagination;
class NotificationResource extends AbstractDatabaseResource class NotificationResource extends AbstractDatabaseResource
@ -32,7 +33,7 @@ class NotificationResource extends AbstractDatabaseResource
public function query(\Tobyz\JsonApiServer\Context $context): object public function query(\Tobyz\JsonApiServer\Context $context): object
{ {
if ($context->endpoint instanceof Endpoint\Index) { if ($context->collection instanceof self && $context->endpoint instanceof Endpoint\Index) {
/** @var Pagination $pagination */ /** @var Pagination $pagination */
$pagination = ($context->endpoint->paginationResolver)($context); $pagination = ($context->endpoint->paginationResolver)($context);

View File

@ -23,6 +23,8 @@ use Tobyz\JsonApiServer\Exception\BadRequestException;
class PostResource extends AbstractDatabaseResource class PostResource extends AbstractDatabaseResource
{ {
public static int $defaultLimit = 20;
public function __construct( public function __construct(
protected PostRepository $posts, protected PostRepository $posts,
protected TranslatorInterface $translator, protected TranslatorInterface $translator,
@ -137,7 +139,7 @@ class PostResource extends AbstractDatabaseResource
'hiddenUser', 'hiddenUser',
'discussion' 'discussion'
]) ])
->paginate(), ->paginate(static::$defaultLimit),
]; ];
} }

View File

@ -37,7 +37,13 @@ class RequestUtil
public static function extractSort(Request $request, ?string $default, array $available = []): ?array public static function extractSort(Request $request, ?string $default, array $available = []): ?array
{ {
if (! ($input = $request->getQueryParams()['sort'] ?? $default)) { $input = $request->getQueryParams()['sort'] ?? null;
if (is_null($input) || ! filled($input)) {
$input = $default;
}
if (! $input) {
return null; return null;
} }
@ -71,7 +77,11 @@ class RequestUtil
public static function extractLimit(Request $request, ?int $defaultLimit = null, ?int $max = null): ?int public static function extractLimit(Request $request, ?int $defaultLimit = null, ?int $max = null): ?int
{ {
$limit = (int) ($request->getQueryParams()['page']['limit'] ?? $defaultLimit); $limit = $request->getQueryParams()['page']['limit'] ?? '';
if (is_null($limit) || ! filled($limit)) {
$limit = $defaultLimit;
}
if (! $limit) { if (! $limit) {
return null; return null;

View File

@ -39,7 +39,7 @@ abstract class AbstractSearcher implements SearcherInterface
$this->applySort($search, $criteria->sort, $criteria->sortIsDefault); $this->applySort($search, $criteria->sort, $criteria->sortIsDefault);
$this->applyOffset($search, $criteria->offset); $this->applyOffset($search, $criteria->offset);
$this->applyLimit($search, $criteria->limit + 1); $this->applyLimit($search, $criteria->limit ? $criteria->limit + 1 : null);
foreach ($this->mutators as $mutator) { foreach ($this->mutators as $mutator) {
$mutator($search, $criteria); $mutator($search, $criteria);
@ -102,7 +102,7 @@ abstract class AbstractSearcher implements SearcherInterface
protected function applyLimit(DatabaseSearchState $state, ?int $limit): void protected function applyLimit(DatabaseSearchState $state, ?int $limit): void
{ {
if ($limit > 0) { if ($limit && $limit > 0) {
$state->getQuery()->take($limit); $state->getQuery()->take($limit);
} }
} }

View File

@ -53,7 +53,7 @@ class ShowTest extends TestCase
]) ])
); );
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode(), $response->getBody()->getContents());
} }
/** /**
@ -71,7 +71,7 @@ class ShowTest extends TestCase
]) ])
); );
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode(), $response->getBody()->getContents());
} }
/** /**
@ -113,7 +113,7 @@ class ShowTest extends TestCase
$json = json_decode($response->getBody()->getContents(), true); $json = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(2, Arr::get($json, 'data.relationships.posts.data.0.id')); $this->assertEquals(2, Arr::get($json, 'data.relationships.posts.data.0.id'), $response->getBody()->getContents());
} }
/** /**
@ -125,7 +125,7 @@ class ShowTest extends TestCase
$this->request('GET', '/api/discussions/2') $this->request('GET', '/api/discussions/2')
); );
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode(), $response->getBody()->getContents());
} }
/** /**

View File

@ -78,9 +78,9 @@ class ApiControllerTest extends TestCase
]) ])
); );
$payload = json_decode($response->getBody()->getContents(), true); $payload = json_decode($body = $response->getBody()->getContents(), true);
$this->assertEquals('dataSerializationPrepCustomTitle', $payload['data']['attributes']['title']); $this->assertEquals('dataSerializationPrepCustomTitle', $payload['data']['attributes']['title'], $body);
} }
/** /**