aboutsummaryrefslogtreecommitdiffhomepage
path: root/routes/actor.php
diff options
context:
space:
mode:
Diffstat (limited to 'routes/actor.php')
-rw-r--r--routes/actor.php43
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
]);
});