From 4b8ffc874ad25ef5a101aa18cf2464d7f814e466 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 7 Jul 2023 15:02:34 +0200 Subject: [PATCH] add pager to followers endpoint --- includes/rest/class-followers.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index 38b4746..edd1445 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -58,6 +58,8 @@ class Followers { return $user; } + $page = $request->get_param( 'page', 1 ); + /* * Action triggerd prior to the ActivityPub profile being created and sent to the client */ @@ -72,15 +74,22 @@ class Followers { $json->actor = $user->get_id(); $json->type = 'OrderedCollectionPage'; - $json->partOf = get_rest_url_by_path( sprintf( 'users/%d/followers', $user->get__id() ) ); // phpcs:ignore - $json->first = $json->partOf; // phpcs:ignore $json->totalItems = FollowerCollection::count_followers( $user->get__id() ); // phpcs:ignore + $json->partOf = get_rest_url_by_path( sprintf( 'users/%d/followers', $user->get__id() ) ); // phpcs:ignore + + $json->first = \add_query_arg( 'page', 1, $json->partOf ); // phpcs:ignore + $json->last = \add_query_arg( 'page', \ceil ( $json->totalItems / 20 ), $json->partOf ); // phpcs:ignore + + if ( $page && ( ( \ceil ( $json->totalItems / 20 ) ) >= $page ) ) { // phpcs:ignore + $json->next = \add_query_arg( 'page', $page + 1, $json->partOf ); // phpcs:ignore + } + // phpcs:ignore $json->orderedItems = array_map( function( $item ) { return $item->get_url(); }, - FollowerCollection::get_followers( $user->get__id() ) + FollowerCollection::get_followers( $user->get__id(), 20, $page ) ); $response = new WP_REST_Response( $json, 200 ); @@ -99,6 +108,7 @@ class Followers { $params['page'] = array( 'type' => 'integer', + 'default' => 1, ); $params['user_id'] = array(