]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Remove entries from queue / relay detection
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index d9dd7ee5b321993c99ad2249bb31375f1a8d04f2..e6ff43f06acd807e53c1a260e21a5c26931d0a45 100644 (file)
@@ -189,15 +189,17 @@ class Processor
        /**
         * Updates a message
         *
-        * @param array $activity Activity array
+        * @param FetchQueue $fetchQueue
+        * @param array      $activity   Activity array
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
-       public static function updateItem(array $activity)
+       public static function updateItem(FetchQueue $fetchQueue, array $activity)
        {
                $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity', 'post-type'], ['uri' => $activity['id']]);
                if (!DBA::isResult($item)) {
                        Logger::warning('No existing item, item will be created', ['uri' => $activity['id']]);
-                       $item = self::createItem($activity);
+                       $item = self::createItem($fetchQueue, $activity);
                        if (empty($item)) {
                                return;
                        }
@@ -221,6 +223,8 @@ class Processor
                Post\History::add($item['uri-id'], $item);
                Item::update($item, ['uri' => $activity['id']]);
 
+               Receiver::removeFromQueue($activity);
+
                if ($activity['object_type'] == 'as:Event') {
                        $posts = Post::select(['event-id', 'uid'], ["`uri` = ? AND `event-id` > ?", $activity['id'], 0]);
                        while ($post = DBA::fetch($posts)) {
@@ -258,12 +262,13 @@ class Processor
        /**
         * Prepares data for a message
         *
-        * @param array $activity Activity array
+        * @param FetchQueue $fetchQueue
+        * @param array      $activity   Activity array
         * @return array Internal item
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function createItem(array $activity): array
+       public static function createItem(FetchQueue $fetchQueue, array $activity): array
        {
                $item = [];
                $item['verb'] = Activity::POST;
@@ -279,7 +284,12 @@ class Processor
 
                if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
                        Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id']]);
-                       self::fetchMissingActivity($activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
+                       /**
+                        * Instead of calling recursively self::fetchMissingActivity which can hit PHP's default function nesting
+                        * limit of 256 recursive calls, we push the parent activity fetch parameters in this queue. The initial
+                        * caller is responsible for processing the remaining queue once the original activity has been processed.
+                        */
+                       $fetchQueue->push(new FetchQueueItem($activity['reply-to-id'], $activity));
                }
 
                $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
@@ -418,6 +428,7 @@ class Processor
 
                Logger::info('Deleting item', ['object' => $activity['object_id'], 'owner'  => $owner]);
                Item::markForDeletion(['uri' => $activity['object_id'], 'owner-id' => $owner]);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -453,14 +464,15 @@ class Processor
        /**
         * Prepare the item array for an activity
         *
-        * @param array  $activity Activity array
-        * @param string $verb     Activity verb
+        * @param FetchQueue $fetchQueue
+        * @param array      $activity   Activity array
+        * @param string     $verb       Activity verb
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function createActivity(array $activity, string $verb)
+       public static function createActivity(FetchQueue $fetchQueue, array $activity, string $verb)
        {
-               $item = self::createItem($activity);
+               $item = self::createItem($fetchQueue, $activity);
                if (empty($item)) {
                        return;
                }
@@ -534,6 +546,7 @@ class Processor
                Logger::debug('Add post to featured collection', ['uri-id' => $uriid]);
 
                Post\Collection::add($uriid, Post\Collection::FEATURED);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -551,6 +564,7 @@ class Processor
                Logger::debug('Remove post from featured collection', ['uri-id' => $uriid]);
 
                Post\Collection::remove($uriid, Post\Collection::FEATURED);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -881,6 +895,7 @@ class Processor
                        $item_id = Item::insert($item);
                        if ($item_id) {
                                Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
+                               Receiver::removeFromQueue($activity);
                        } else {
                                Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
                        }
@@ -891,7 +906,7 @@ class Processor
                }
 
                // Store send a follow request for every reshare - but only when the item had been stored
-               if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && ($item['author-link'] != $item['owner-link'])) {
+               if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && !empty($item['author-link']) && ($item['author-link'] != $item['owner-link'])) {
                        $author = APContact::getByURL($item['owner-link'], false);
                        // We send automatic follow requests for reshared messages. (We don't need though for forum posts)
                        if ($author['type'] != 'Group') {
@@ -1106,14 +1121,16 @@ class Processor
        /**
         * Fetches missing posts
         *
-        * @param string $url         message URL
-        * @param array  $child       activity array with the child of this message
-        * @param string $relay_actor Relay actor
-        * @param int    $completion  Completion mode, see Receiver::COMPLETION_*
+        * @param FetchQueue $fetchQueue
+        * @param string     $url         message URL
+        * @param array      $child       activity array with the child of this message
+        * @param string     $relay_actor Relay actor
+        * @param int        $completion  Completion mode, see Receiver::COMPLETION_*
         * @return string fetched message URL
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
-       public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL): string
+       public static function fetchMissingActivity(FetchQueue $fetchQueue, string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL): string
        {
                if (!empty($child['receiver'])) {
                        $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
@@ -1194,7 +1211,7 @@ class Processor
                        return '';
                }
 
-               ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), $uid, true, false, $signer);
+               ActivityPub\Receiver::processActivity($fetchQueue, $ldactivity, json_encode($activity), $uid, true, false, $signer);
 
                Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
 
@@ -1337,6 +1354,7 @@ class Processor
 
                Logger::info('Updating profile', ['object' => $activity['object_id']]);
                Contact::updateFromProbeByURL($activity['object_id']);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -1365,6 +1383,7 @@ class Processor
                DBA::close($contacts);
 
                Logger::info('Deleted contact', ['object' => $activity['object_id']]);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -1447,6 +1466,7 @@ class Processor
                $condition = ['id' => $cid];
                Contact::update($fields, $condition);
                Logger::info('Accept contact request', ['contact' => $cid, 'user' => $uid]);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -1480,6 +1500,7 @@ class Processor
                } else {
                        Logger::info('Rejected contact request', ['contact' => $cid, 'user' => $uid]);
                }
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -1505,6 +1526,7 @@ class Processor
                }
 
                Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
@@ -1541,12 +1563,14 @@ class Processor
 
                Contact::removeFollower($contact);
                Logger::info('Undo following request', ['contact' => $cid, 'user' => $uid]);
+               Receiver::removeFromQueue($activity);
        }
 
        /**
         * Switches a contact to AP if needed
         *
         * @param integer $cid Contact ID
+        * @return void
         * @throws \Exception
         */
        private static function switchContact(int $cid)