]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Fixes and type-hints:
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index e3df1a7a32fc0af404803e0ec40245faf99acce4..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
         *
@@ -151,12 +154,21 @@ class Transmitter
         * @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(array $owner, array $rel, string $module, int $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'],
@@ -192,6 +204,10 @@ class Transmitter
                }
 
                if (!$show_contacts) {
+                       if (!empty($cachekey)) {
+                               DI::cache()->set($cachekey, $data, Duration::DAY);
+                       }
+
                        return $data;
                }
 
@@ -216,6 +232,10 @@ class Transmitter
                        $data['orderedItems'] = $list;
                }
 
+               if (!empty($cachekey)) {
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
+               }
+
                return $data;
        }
 
@@ -225,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(array $owner, int $page = null, string $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)) {
@@ -293,27 +322,42 @@ 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 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)
+       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` = ?)",
-                       Contact::getIdForURL($owner['url'], 0, false), Post\Collection::FEATURED];
+                       $owner_cid, Post\Collection::FEATURED];
 
                $condition = DBA::mergeConditions($condition,
                        ['uid'           => $owner['uid'],
-                       'author-id'      => Contact::getIdForURL($owner['url'], 0, false),
+                       'author-id'      => $owner_cid,
                        'private'        => [Item::PUBLIC, Item::UNLISTED],
                        'gravity'        => [GRAVITY_PARENT, GRAVITY_COMMENT],
                        'network'        => Protocol::FEDERATED,
@@ -334,30 +378,36 @@ class Transmitter
                }
 
                if (empty($page)) {
-                       $data['first'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=1';
+                       $items = Post::select(['id'], $condition, ['limit' => 20, 'order' => ['created' => true]]);
                } else {
                        $data['type'] = 'OrderedCollectionPage';
-                       $list = [];
-
                        $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
-                       while ($item = Post::fetch($items)) {
-                               $activity = self::createActivityFromItem($item['id'], true);
-                               $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
+               }
+               $list = [];
 
-                               // Only list "Create" activity objects here, no reshares
-                               if (!empty($activity['object']) && ($activity['type'] == 'Create')) {
-                                       $list[] = $activity['object'];
-                               }
-                       }
-                       DBA::close($items);
+               while ($item = Post::fetch($items)) {
+                       $activity = self::createActivityFromItem($item['id'], true);
+                       $activity['type'] = $activity['type'] == 'Update' ? 'Create' : $activity['type'];
 
-                       if (count($list) == 20) {
-                               $data['next'] = DI::baseUrl() . '/featured/' . $owner['nickname'] . '?page=' . ($page + 1);
+                       // 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;
+               $data['orderedItems'] = $list;
+
+               if (!empty($cachekey)) {
+                       DI::cache()->set($cachekey, $data, Duration::DAY);
                }
 
                return $data;