]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge remote-tracking branch 'upstream/2022.05-rc' into bulk-delivery
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 553993edca300717b225bcf6a232e2c9ba7f5063..baaaba0680e45100177a048ea5f70e7c4aa5ddce 100644 (file)
@@ -146,16 +146,16 @@ class Transmitter
        /**
         * Collects a list of contacts of the given owner
         *
-        * @param array     $owner     Owner array
-        * @param int|array $rel       The relevant value(s) contact.rel should match
-        * @param string    $module    The name of the relevant AP endpoint module (followers|following)
-        * @param integer   $page      Page number
-        * @param string    $requester URL of the requester
+        * @param array   $owner     Owner array
+        * @param array   $rel       The relevant value(s) contact.rel should match
+        * @param string  $module    The name of the relevant AP endpoint module (followers|following)
+        * @param integer $page      Page number
+        * @param string  $requester URL of the requester
         *
         * @return array of owners
         * @throws \Exception
         */
-       public static function getContacts($owner, $rel, $module, $page = null, string $requester = null)
+       public static function getContacts(array $owner, array $rel, string $module, int $page = null, string $requester = null)
        {
                $parameters = [
                        'rel' => $rel,
@@ -179,6 +179,10 @@ class Transmitter
                $data['type'] = 'OrderedCollection';
                $data['totalItems'] = $total;
 
+               if (!empty($page)) {
+                       $data['id'] .= '?' . http_build_query(['page' => $page]);
+               }
+
                // When we hide our friends we will only show the pure number but don't allow more.
                $show_contacts = empty($owner['hide-friends']);
 
@@ -203,7 +207,7 @@ class Transmitter
                        }
                        DBA::close($contacts);
 
-                       if (!empty($list)) {
+                       if (count($list) == 100) {
                                $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
                        }
 
@@ -226,7 +230,7 @@ class Transmitter
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function getOutbox($owner, $page = null, $requester = '')
+       public static function getOutbox(array $owner, int $page = null, string $requester = '')
        {
                $condition = ['private' => [Item::PUBLIC, Item::UNLISTED]];
 
@@ -258,6 +262,10 @@ class Transmitter
                $data['type'] = 'OrderedCollection';
                $data['totalItems'] = $count;
 
+               if (!empty($page)) {
+                       $data['id'] .= '?' . http_build_query(['page' => $page]);
+               }
+
                if (empty($page)) {
                        $data['first'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
                } else {
@@ -276,7 +284,7 @@ class Transmitter
                        }
                        DBA::close($items);
 
-                       if (!empty($list)) {
+                       if (count($list) == 20) {
                                $data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
                        }
 
@@ -288,6 +296,75 @@ class Transmitter
                return $data;
        }
 
+       /**
+        * Public posts for the given owner
+        *
+        * @param array   $owner Owner array
+        * @param integer $page  Page number
+        *
+        * @return array of posts
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function getFeatured(array $owner, int $page = null)
+       {
+               $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
+                       Contact::getIdForURL($owner['url'], 0, false), Post\Collection::FEATURED];
+
+               $condition = DBA::mergeConditions($condition,
+                       ['uid'           => $owner['uid'],
+                       'author-id'      => Contact::getIdForURL($owner['url'], 0, false),
+                       'private'        => [Item::PUBLIC, Item::UNLISTED],
+                       'gravity'        => [GRAVITY_PARENT, GRAVITY_COMMENT],
+                       'network'        => Protocol::FEDERATED,
+                       'parent-network' => Protocol::FEDERATED,
+                       'origin'         => true,
+                       'deleted'        => false,
+                       'visible'        => true]);
+
+               $count = Post::count($condition);
+
+               $data = ['@context' => ActivityPub::CONTEXT];
+               $data['id'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
+               $data['type'] = 'OrderedCollection';
+               $data['totalItems'] = $count;
+
+               if (!empty($page)) {
+                       $data['id'] .= '?' . http_build_query(['page' => $page]);
+               }
+
+               if (empty($page)) {
+                       $items = Post::select(['id'], $condition, ['limit' => 20, 'order' => ['created' => true]]);
+               } else {
+                       $data['type'] = 'OrderedCollectionPage';
+                       $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
+               }
+               $list = [];
+
+               while ($item = Post::fetch($items)) {
+                       $activity = self::createActivityFromItem($item['id'], true);
+                       $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
+
+                       // Only list "Create" activity objects here, no reshares
+                       if (!empty($activity['object']) && ($activity['type'] == 'Create')) {
+                               $list[] = $activity['object'];
+                       }
+               }
+               DBA::close($items);
+
+               if (count($list) == 20) {
+                       $data['next'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=' . ($page + 1);
+               }
+
+               if (!empty($page)) {
+                       $data['partOf'] = DI::baseUrl() . '/featured/' . $owner['nickname'];
+               }
+
+               $data['orderedItems'] = $list;
+
+               return $data;
+       }
+
        /**
         * Return the service array containing information the used software and it's url
         *
@@ -328,8 +405,9 @@ class Transmitter
                if ($uid != 0) {
                        $data['following'] = DI::baseUrl() . '/following/' . $owner['nick'];
                        $data['followers'] = DI::baseUrl() . '/followers/' . $owner['nick'];
-                       $data['inbox'] = DI::baseUrl() . '/inbox/' . $owner['nick'];
-                       $data['outbox'] = DI::baseUrl() . '/outbox/' . $owner['nick'];
+                       $data['inbox']     = DI::baseUrl() . '/inbox/' . $owner['nick'];
+                       $data['outbox']    = DI::baseUrl() . '/outbox/' . $owner['nick'];
+                       $data['featured']  = DI::baseUrl() . '/featured/' . $owner['nick'];
                } else {
                        $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
                }
@@ -1165,6 +1243,7 @@ class Transmitter
 
                if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
                        $data['object'] = $object ?? self::createNote($item);
+                       $data['published'] = DateTimeFormat::utcNow(DateTimeFormat::ATOM);
                } elseif ($data['type'] == 'Add') {
                        $data = self::createAddTag($item, $data);
                } elseif ($data['type'] == 'Announce') {