diff options
| author | winter | 2025-02-14 20:25:42 +0000 |
|---|---|---|
| committer | winter | 2025-02-14 20:25:42 +0000 |
| commit | 8a8e07c6239e5f33c79ddf63a3e4ac3065086037 (patch) | |
| tree | e8899555bd49f1f8c0577a84c596bea923aa3b8c /routes/actor.php | |
| parent | ded22bb2a0c9eec4d1c0f65c6271cbf202ed9f09 (diff) | |
proper pagination and privacy checks on basicFeed/fullFeed
Diffstat (limited to 'routes/actor.php')
| -rw-r--r-- | routes/actor.php | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/routes/actor.php b/routes/actor.php index 125c733..c228f45 100644 --- a/routes/actor.php +++ b/routes/actor.php @@ -5,6 +5,7 @@ use Digitigrade\HttpResponseStatus\TemporaryRedirect; use Digitigrade\HttpResponseStatus\Unauthorized; use Digitigrade\Model\Actor; use Digitigrade\Model\Note; +use Digitigrade\Model\NotePrivacyScope; use Digitigrade\Router; Router::getInstance()->mount('/actor/:id', function (array $args) { @@ -23,14 +24,21 @@ Router::getInstance()->mount('/actor/:id', function (array $args) { Router::getInstance()->mount('/actor/:id/basicFeed', function (array $args) { $actor = Actor::find($args['id']); - $notes = Note::findAllWithAuthor($actor); - // TODO: implement pagination + $page = +($_GET['page'] ?? 1); + $pageSize = 100; + $notes = array_filter( + Note::findAllWithAuthor($actor, $pageSize, ($page - 1) * $pageSize), + fn(Note $n) => $n->privacy->scope == NotePrivacyScope::PUBLIC + ); + $notes = array_values($notes); // to fix any holes in the array + $totalNotes = Note::countWithAuthor($actor); + $totalPages = ceil($totalNotes / $pageSize); json_response([ - 'page' => 1, - 'totalPages' => 1, - 'totalItems' => count($notes), - 'nextPage' => null, - 'previousPage' => null, + 'page' => $page, + 'totalPages' => $totalPages, + 'totalItems' => $totalNotes, + 'nextPage' => ($page < $totalPages) ? path_to_uri("/actor/$actor->id/basicFeed?page=" . ($page + 1)) : null, + 'previousPage' => ($page > 1) ? path_to_uri("/actor/$actor->id/basicFeed?page=" . ($page - 1)) : null, 'items' => array_map(function (Note $note) { return $note->uri; }, $notes) @@ -39,14 +47,21 @@ Router::getInstance()->mount('/actor/:id/basicFeed', function (array $args) { Router::getInstance()->mount('/actor/:id/fullFeed', function (array $args) { $actor = Actor::find($args['id']); - $notes = Note::findAllWithAuthor($actor); - // TODO: implement pagination here as well + $page = +($_GET['page'] ?? 1); + $pageSize = 10; + $notes = array_filter( + Note::findAllWithAuthor($actor, $pageSize, ($page - 1) * $pageSize), + fn(Note $n) => $n->privacy->scope == NotePrivacyScope::PUBLIC + ); + $notes = array_values($notes); + $totalNotes = Note::countWithAuthor($actor); + $totalPages = ceil($totalNotes / $pageSize); json_response([ - 'page' => 1, - 'totalPages' => 1, - 'totalItems' => count($notes), - 'nextPage' => null, - 'previousPage' => null, + 'page' => $page, + 'totalPages' => $totalPages, + 'totalItems' => $totalNotes, + 'nextPage' => ($page < $totalPages) ? path_to_uri("/actor/$actor->id/fullFeed?page=" . ($page + 1)) : null, + 'previousPage' => ($page > 1) ? path_to_uri("/actor/$actor->id/fullFeed?page=" . ($page - 1)) : null, 'items' => $notes ]); }); |
