diff --git a/src/Objects/CollectionsService.php b/src/Objects/CollectionsService.php index e8c3543..af32c5e 100644 --- a/src/Objects/CollectionsService.php +++ b/src/Objects/CollectionsService.php @@ -104,8 +104,9 @@ class CollectionsService $itemsKey => $pageItems, 'partOf' => $collection['id'], ); - if ( $collectionItems->getFieldValue( $idx ) ) { - $page['next'] = $collection['id'] . "?offset=$idx"; + $nextIdx = $this->hasNextItem( $request, $collectionItems, $idx ); + if ( $nextIdx ) { + $page['next'] = $collection['id'] . "?offset=$nextIdx"; } if ( $isOrdered ) { $page['startIndex'] = $offset; @@ -113,6 +114,20 @@ class CollectionsService return $page; } + private function hasNextItem( $request, $collectionItems, $idx ) + { + $next = $collectionItems->getFieldValue( $idx ); + while ( $next ) { + if ( is_string( $next ) || + $this->authService->requestAuthorizedToView( $request, $next ) ) { + return $idx; + } + $idx++; + $next = $collectionsItems->getFieldValue( $idx ); + } + return false; + } + private function isOrdered( ActivityPubObject $collection ) { if ( $collection->hasField( 'type' ) &&