From 474631dff26343f1c021864bb8c8fd8d348e5234 Mon Sep 17 00:00:00 2001 From: Sascha Nitsch Date: Wed, 11 Jun 2025 03:15:55 +0200 Subject: [PATCH] extended factory and constructing from json renaming to not mix total count with paging --- php/federator/api/fedusers/followers.php | 2 +- php/federator/api/fedusers/following.php | 2 +- .../data/activitypub/common/collection.php | 22 ++++++++++- .../data/activitypub/common/followers.php | 2 +- .../data/activitypub/common/following.php | 2 +- .../activitypub/common/orderedcollection.php | 39 +++++++++++++------ .../common/orderedcollectionpage.php | 24 +++++++++++- .../data/activitypub/common/outbox.php | 3 +- php/federator/data/activitypub/factory.php | 8 +++- 9 files changed, 82 insertions(+), 22 deletions(-) diff --git a/php/federator/api/fedusers/followers.php b/php/federator/api/fedusers/followers.php index 26fdbfb..818d9c8 100644 --- a/php/federator/api/fedusers/followers.php +++ b/php/federator/api/fedusers/followers.php @@ -87,7 +87,7 @@ class Followers implements \Federator\Api\FedUsers\FedUsersInterface // Pagination navigation $lastPage = max(0, ceil($totalItems / $pageSize) - 1); - if ($page === "" || $followers->count() == 0) { + if ($page === "" || $followers->getCount() == 0) { $followers->setFirst($baseUrl . '?page=0'); $followers->setLast($baseUrl . '?page=' . $lastPage); } diff --git a/php/federator/api/fedusers/following.php b/php/federator/api/fedusers/following.php index 2fe4ff8..f4007f9 100644 --- a/php/federator/api/fedusers/following.php +++ b/php/federator/api/fedusers/following.php @@ -87,7 +87,7 @@ class Following implements \Federator\Api\FedUsers\FedUsersInterface // Pagination navigation $lastPage = max(0, ceil($totalItems / $pageSize) - 1); - if ($page === "" || $following->count() == 0) { + if ($page === "" || $following->getCount() == 0) { $following->setFirst($baseUrl . '?page=0'); $following->setLast($baseUrl . '?page=' . $lastPage); } diff --git a/php/federator/data/activitypub/common/collection.php b/php/federator/data/activitypub/common/collection.php index d925f05..14b3a7c 100644 --- a/php/federator/data/activitypub/common/collection.php +++ b/php/federator/data/activitypub/common/collection.php @@ -48,7 +48,20 @@ class Collection extends APObject */ public function fromJson($json) { - return parent::fromJson($json); + $success = parent::fromJson($json); + if (!$success) { + return false; + } + if (array_key_exists('totalItems', $json)) { + $this->totalItems = $json['totalItems']; + } + if (array_key_exists('first', $json)) { + $this->first = $json['first']; + } + if (array_key_exists('last', $json)) { + $this->last = $json['last']; + } + return true; } /** @@ -61,11 +74,16 @@ class Collection extends APObject $this->totalItems = $totalItems; } - public function count(): int + public function getTotalItems(): int { return $this->totalItems; } + public function getFirst(): string + { + return $this->first; + } + public function setFirst(string $url): void { $this->first = $url; diff --git a/php/federator/data/activitypub/common/followers.php b/php/federator/data/activitypub/common/followers.php index dfa7e31..a2f3116 100644 --- a/php/federator/data/activitypub/common/followers.php +++ b/php/federator/data/activitypub/common/followers.php @@ -25,7 +25,7 @@ class Followers extends OrderedCollectionPage public function setItems(&$items) { // Optionally: type check that all $items are Activity objects - $this->items = $items; + $this->orderedItems = $items; $this->totalItems = sizeof($items); } diff --git a/php/federator/data/activitypub/common/following.php b/php/federator/data/activitypub/common/following.php index 6358ed8..ea57e9c 100644 --- a/php/federator/data/activitypub/common/following.php +++ b/php/federator/data/activitypub/common/following.php @@ -25,7 +25,7 @@ class Following extends OrderedCollectionPage public function setItems(&$items) { // Optionally: type check that all $items are Activity objects - $this->items = $items; + $this->orderedItems = $items; $this->totalItems = sizeof($items); } diff --git a/php/federator/data/activitypub/common/orderedcollection.php b/php/federator/data/activitypub/common/orderedcollection.php index 1207c40..0897fc9 100644 --- a/php/federator/data/activitypub/common/orderedcollection.php +++ b/php/federator/data/activitypub/common/orderedcollection.php @@ -15,7 +15,7 @@ class OrderedCollection extends Collection * * @var APObject[]|string[] */ - protected $items = []; + protected $orderedItems = []; public function __construct() { @@ -32,8 +32,8 @@ class OrderedCollection extends Collection { $return = parent::toObject(); $return['type'] = 'OrderedCollection'; - if ($this->totalItems > 0) { - foreach ($this->items as $item) { + if (sizeof($this->orderedItems) > 0) { + foreach ($this->orderedItems as $item) { if (is_string($item)) { $return['orderedItems'][] = $item; } elseif (is_object($item)) { @@ -52,7 +52,20 @@ class OrderedCollection extends Collection */ public function fromJson($json) { - return parent::fromJson($json); + $success = parent::fromJson($json); + if (!$success) { + return false; + } + if (array_key_exists('orderedItems', $json)) { + foreach ($json['orderedItems'] as $item) { + $obj = \Federator\Data\ActivityPub\Factory::newActivityFromJson($item); + if ($obj !== false) { + $this->orderedItems[] = $obj; + } + } + } + + return true; } /** @@ -61,8 +74,7 @@ class OrderedCollection extends Collection */ public function append(&$item): void { - $this->items[] = $item; - $this->totalItems = sizeof($this->items); + $this->orderedItems[] = $item; } /** @@ -73,18 +85,22 @@ class OrderedCollection extends Collection public function get(int $index) { if ($index >= 0) { - if ($index >= $this->totalItems) { + if ($index >= sizeof($this->orderedItems)) { return false; } - return $this->items[$index]; + return $this->orderedItems[$index]; } else { - if ($this->totalItems + $index < 0) { + if (sizeof($this->orderedItems) + $index < 0) { return false; } - return $this->items[$this->totalItems + $index]; + return $this->orderedItems[sizeof($this->orderedItems) + $index]; } } + public function getCount(): int + { + return sizeof($this->orderedItems); + } /** * set items * @@ -93,7 +109,6 @@ class OrderedCollection extends Collection */ public function setItems(&$items) { - $this->items = $items; - $this->totalItems = sizeof($items); + $this->orderedItems = $items; } } diff --git a/php/federator/data/activitypub/common/orderedcollectionpage.php b/php/federator/data/activitypub/common/orderedcollectionpage.php index 21838cc..3bfbe86 100644 --- a/php/federator/data/activitypub/common/orderedcollectionpage.php +++ b/php/federator/data/activitypub/common/orderedcollectionpage.php @@ -51,7 +51,29 @@ class OrderedCollectionPage extends OrderedCollection */ public function fromJson($json) { - return parent::fromJson($json); + $success = parent::fromJson($json); + if (!$success) { + return false; + } + if (array_key_exists('next', $json)) { + $this->next = $json['next']; + } + if (array_key_exists('prev', $json)) { + $this->prev = $json['prev']; + } + if (array_key_exists('partOf', $json)) { + $this->partOf = $json['partOf']; + } + return true; + } + + /** + * get next url + * @return string next URL + */ + public function getNext() + { + return $this->next; } /** diff --git a/php/federator/data/activitypub/common/outbox.php b/php/federator/data/activitypub/common/outbox.php index 9c668c0..6c51622 100644 --- a/php/federator/data/activitypub/common/outbox.php +++ b/php/federator/data/activitypub/common/outbox.php @@ -35,8 +35,7 @@ class Outbox extends OrderedCollectionPage public function setItems(&$items) { // Optionally: type check that all $items are Activity objects - $this->items = $items; - $this->totalItems = sizeof($items); + $this->orderedItems = $items; } /** diff --git a/php/federator/data/activitypub/factory.php b/php/federator/data/activitypub/factory.php index 3fbf3c1..4fdab3f 100644 --- a/php/federator/data/activitypub/factory.php +++ b/php/federator/data/activitypub/factory.php @@ -16,7 +16,7 @@ class Factory /** * create object tree from json - * @param array $json input json + * @param array|mixed $json input json * @return Common\APObject|null object or false on error */ public static function newFromJson($json, string $jsonstring) @@ -67,6 +67,12 @@ class Factory case 'Inbox': $return = new Common\Inbox(); break; + case 'OrderedCollection': + $return = new Common\OrderedCollection(); + break; + case 'OrderedCollectionPage': + $return = new Common\OrderedCollectionPage(); + break; case 'Tombstone': $return = new Common\APObject("Tombstone"); break;