]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Fixes and type-hints:
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 2cab827bb1afd937468687e1e542c0bf9988f897..023bd5c83cce179c8c6cd16d2f9408b099f41f72 100644 (file)
@@ -44,7 +44,6 @@ use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\HTTPSignature;
-use Friendica\Util\JsonLD;
 use Friendica\Util\LDSignature;
 use Friendica\Util\Map;
 use Friendica\Util\Network;
@@ -59,6 +58,10 @@ use Friendica\Util\XML;
  */
 class Transmitter
 {
+       const CACHEKEY_FEATURED = 'transmitter:getFeatured:';
+       const CACHEKEY_CONTACTS = 'transmitter:getContacts:';
+       const CACHEKEY_OUTBOX   = 'transmitter:getOutbox:';
+
        /**
         * Add relay servers to the list of inboxes
         *
@@ -146,17 +149,26 @@ 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
+        * @param boolean $nocache   Wether to bypass caching
         *
         * @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, $nocache = false)
        {
+               if (empty($page)) {
+                       $cachekey = self::CACHEKEY_CONTACTS . $module . ':'. $owner['uid'];
+                       $result = DI::cache()->get($cachekey);
+                       if (!$nocache && !is_null($result)) {
+                               return $result;
+                       }
+               }
+
                $parameters = [
                        'rel' => $rel,
                        'uid' => $owner['uid'],
@@ -179,6 +191,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']);
 
@@ -188,6 +204,10 @@ class Transmitter
                }
 
                if (!$show_contacts) {
+                       if (!empty($cachekey)) {
+                               DI::cache()->set($cachekey, $data, Duration::DAY);
+                       }
+
                        return $data;
                }
 
@@ -203,7 +223,7 @@ class Transmitter
                        }
                        DBA::close($contacts);
 
-                       if (!empty($list)) {
+                       if (count($list) == 100) {
                                $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
                        }
 
@@ -212,6 +232,10 @@ class Transmitter
                        $data['orderedItems'] = $list;
                }
 
+               if (!empty($cachekey)) {
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
+               }
+
                return $data;
        }
 
@@ -221,13 +245,22 @@ class Transmitter
         * @param array   $owner     Owner array
         * @param integer $page      Page number
         * @param string  $requester URL of requesting account
+        * @param boolean $nocache   Wether to bypass caching
         *
         * @return array of posts
         * @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 = '', $nocache = false)
        {
+               if (empty($page)) {
+                       $cachekey = self::CACHEKEY_OUTBOX . $owner['uid'];
+                       $result = DI::cache()->get($cachekey);
+                       if (!$nocache && !is_null($result)) {
+                               return $result;
+                       }
+               }
+
                $condition = ['private' => [Item::PUBLIC, Item::UNLISTED]];
 
                if (!empty($requester)) {
@@ -258,6 +291,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 +313,7 @@ class Transmitter
                        }
                        DBA::close($items);
 
-                       if (!empty($list)) {
+                       if (count($list) == 20) {
                                $data['next'] = DI::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
                        }
 
@@ -285,6 +322,94 @@ class Transmitter
                        $data['orderedItems'] = $list;
                }
 
+               if (!empty($cachekey)) {
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
+               }
+
+               return $data;
+       }
+
+       /**
+        * Public posts for the given owner
+        *
+        * @param array   $owner   Owner array
+        * @param integer $page    Page number
+        * @param boolean $nocache Wether to bypass caching
+        *
+        * @return array of posts
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function getFeatured(array $owner, int $page = null, $nocache = false)
+       {
+               if (empty($page)) {
+                       $cachekey = self::CACHEKEY_FEATURED . $owner['uid'];
+                       $result = DI::cache()->get($cachekey);
+                       if (!$nocache && !is_null($result)) {
+                               return $result;
+                       }
+               }
+
+               $owner_cid = Contact::getIdForURL($owner['url'], 0, false);
+
+               $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
+                       $owner_cid, Post\Collection::FEATURED];
+
+               $condition = DBA::mergeConditions($condition,
+                       ['uid'           => $owner['uid'],
+                       'author-id'      => $owner_cid,
+                       '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;
+
+               if (!empty($cachekey)) {
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
+               }
+
                return $data;
        }
 
@@ -328,8 +453,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 +1291,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') {
@@ -1467,10 +1594,28 @@ class Transmitter
                        return [];
                }
 
+               // We are treating posts differently when they are directed to a community.
+               // This is done to better support Lemmy. Most of the changes should work with other systems as well.
+               // But to not risk compatibility issues we currently perform the changes only for communities.
+               if ($item['gravity'] == GRAVITY_PARENT) {
+                       $isCommunityPost = !empty(Tag::getByURIId($item['uri-id'], [Tag::EXCLUSIVE_MENTION]));
+                       $links = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
+                       if ($isCommunityPost && (count($links) == 1)) {
+                               $link = $links[0]['url'];
+                       }
+               } else {
+                       $isCommunityPost = false;
+               }
+
                if ($item['event-type'] == 'event') {
                        $type = 'Event';
                } elseif (!empty($item['title'])) {
-                       $type = 'Article';
+                       if (!$isCommunityPost || empty($link)) {
+                               $type = 'Article';
+                       } else {
+                               // "Page" is used by Lemmy for posts that contain an external link
+                               $type = 'Page';
+                       }
                } else {
                        $type = 'Note';
                }
@@ -1502,7 +1647,7 @@ class Transmitter
                        $data['updated'] = DateTimeFormat::utc($item['edited'] . '+00:00', DateTimeFormat::ATOM);
                }
 
-               $data['url'] = $item['plink'];
+               $data['url'] = $link ?? $item['plink'];
                $data['attributedTo'] = $item['author-link'];
                $data['sensitive'] = self::isSensitive($item['uri-id']);
                $data['context'] = self::fetchContextURLForItem($item);
@@ -1539,6 +1684,19 @@ class Transmitter
                if ($type == 'Event') {
                        $data = array_merge($data, self::createEvent($item));
                } else {
+                       if ($isCommunityPost) {
+                               // For community posts we remove the visible "!user@domain.tld".
+                               // This improves the look at systems like Lemmy.
+                               // Also in the future we should control the community delivery via other methods.
+                               $body = preg_replace("/!\[url\=[^\[\]]*\][^\[\]]*\[\/url\]/ism", '', $body);
+                       }
+
+                       if ($type == 'Page') {
+                               // When we transmit "Page" posts we have to remove the attachment.
+                               // The attachment contains the link that we already transmit in the "url" field.
+                               $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
+                       }
+
                        $body = BBCode::setMentionsToNicknames($body);
 
                        $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);