]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Improve SQL-Queries / fixed "fetch_parents" setting
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index d9dd7ee5b321993c99ad2249bb31375f1a8d04f2..f142b7e91d148070b0234ef5de97c2923900c028 100644 (file)
@@ -24,6 +24,7 @@ namespace Friendica\Protocol\ActivityPub;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Content\Text\Markdown;
+use Friendica\Core\Cache\Enum\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
@@ -45,7 +46,9 @@ use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\HTTPSignature;
 use Friendica\Util\JsonLD;
+use Friendica\Util\Network;
 use Friendica\Util\Strings;
 use Friendica\Worker\Delivery;
 
@@ -54,6 +57,34 @@ use Friendica\Worker\Delivery;
  */
 class Processor
 {
+       const CACHEKEY_FETCH_ACTIVITY = 'processor:fetchMissingActivity:';
+       const CACHEKEY_JUST_FETCHED   = 'processor:isJustFetched:';
+
+       /**
+        * Add an object id to the list of processed ids
+        *
+        * @param string $id
+        *
+        * @return void
+        */
+       private static function addActivityId(string $id)
+       {
+               DBA::delete('processed-activity', ["`received` < ?", DateTimeFormat::utc('now - 5 minutes')]);
+               DBA::insert('processed-activity', ['object-id' => $id, 'received' => DateTimeFormat::utcNow()]);
+       }
+
+       /**
+        * Checks if the given object id has just been processed
+        *
+        * @param string $id
+        *
+        * @return boolean
+        */
+       private static function isProcessed(string $id): bool
+       {
+               return DBA::exists('processed-activity', ['object-id' => $id]);
+       }
+
        /**
         * Extracts the tag character (#, @, !) from mention links
         *
@@ -189,16 +220,18 @@ class Processor
        /**
         * Updates a message
         *
-        * @param array $activity Activity array
+        * @param array      $activity   Activity array
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function updateItem(array $activity)
        {
-               $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity', 'post-type'], ['uri' => $activity['id']]);
+               $item = Post::selectFirst(['uri', 'uri-id', 'thr-parent', 'gravity', 'post-type', 'private'], ['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($activity, false);
                        if (empty($item)) {
+                               Queue::remove($activity);
                                return;
                        }
 
@@ -210,17 +243,19 @@ class Processor
                $item['edited'] = DateTimeFormat::utc($activity['updated']);
 
                $item = self::processContent($activity, $item);
-
-               self::storeAttachments($activity, $item);
-               self::storeQuestion($activity, $item);
-
                if (empty($item)) {
+                       Queue::remove($activity);
                        return;
                }
 
+               self::storeAttachments($activity, $item);
+               self::storeQuestion($activity, $item);
+
                Post\History::add($item['uri-id'], $item);
                Item::update($item, ['uri' => $activity['id']]);
 
+               Queue::remove($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,13 +293,23 @@ class Processor
        /**
         * Prepares data for a message
         *
-        * @param array $activity Activity array
+        * @param array $activity      Activity array
+        * @param bool  $fetch_parents
+        *
         * @return array Internal item
+        *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function createItem(array $activity): array
+       public static function createItem(array $activity, bool $fetch_parents): array
        {
+               if (self::isProcessed($activity['id']) && !Post::exists(['uri' => $activity['id']])) {
+                       Logger::info('Id is already processed', ['id' => $activity['id']]);
+                       return [];
+               }
+
+               self::addActivityId($activity['id']);
+
                $item = [];
                $item['verb'] = Activity::POST;
                $item['thr-parent'] = $activity['reply-to-id'];
@@ -277,16 +322,51 @@ class Processor
                        $item['object-type'] = Activity\ObjectType::COMMENT;
                }
 
-               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);
+               if (!empty($activity['conversation'])) {
+                       $item['conversation'] = $activity['conversation'];
+               } elseif (!empty($activity['context'])) {
+                       $item['conversation'] = $activity['context'];
+               }
+
+               if (!empty($item['conversation'])) {
+                       $conversation = Post::selectFirstThread(['uri'], ['conversation' => $item['conversation']]);
+                       if (!empty($conversation)) {
+                               Logger::debug('Got conversation', ['conversation' => $item['conversation'], 'parent' => $conversation]);
+                               $item['parent-uri'] = $conversation['uri'];
+                       }
+               } else {
+                       $conversation = [];
+               }
+
+               Logger::debug('Create Item', ['id' => $activity['id'], 'conversation' => $item['conversation'] ?? '']);
+               if (empty($activity['author']) && empty($activity['actor'])) {
+                       Logger::notice('Missing author and actor. We quit here.', ['activity' => $activity]);
+                       Queue::remove($activity);
+                       return [];
+               }
+
+               if (!in_array(0, $activity['receiver']) || !DI::config()->get('system', 'fetch_parents')) {
+                       $fetch_parents = false;
+               }
+
+               if ($fetch_parents && empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
+                       $result = self::fetchParent($activity);
+                       if (!empty($result)) {
+                               if (($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
+                                       $item['thr-parent'] = $result;
+                               }
+                       } elseif (empty($conversation)) {
+                               return [];
+                       }
                }
 
                $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
 
-               /// @todo What to do with $activity['context']?
-               if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
+               if (empty($conversation) && empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
                        Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
+                       if (!$fetch_parents) {
+                               Queue::remove($activity);
+                       }
                        return [];
                }
 
@@ -306,13 +386,12 @@ class Processor
 
                if (!empty($activity['raw'])) {
                        $item['source'] = $activity['raw'];
-                       $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
-                       $item['conversation-href'] = $activity['context'] ?? '';
-                       $item['conversation-uri'] = $activity['conversation'] ?? '';
+               }
 
-                       if (isset($activity['push'])) {
-                               $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
-                       }
+               $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
+
+               if (isset($activity['push'])) {
+                       $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
                }
 
                if (!empty($activity['from-relay'])) {
@@ -381,6 +460,7 @@ class Processor
                $item = self::processContent($activity, $item);
                if (empty($item)) {
                        Logger::info('Message was not processed');
+                       Queue::remove($activity);
                        return [];
                }
 
@@ -405,6 +485,109 @@ class Processor
                return $item;
        }
 
+       /**
+        * Fetch and process parent posts for the given activity
+        *
+        * @param array $activity
+        *
+        * @return string
+        */
+       private static function fetchParent(array $activity): string
+       {
+               $recursion_depth = $activity['recursion-depth'] ?? 0;
+
+               if ($recursion_depth < DI::config()->get('system', 'max_recursion_depth')) {
+                       Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+                       $result = self::fetchMissingActivity($activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
+                       if (empty($result) && self::isActivityGone($activity['reply-to-id'])) {
+                               Logger::notice('The activity is gone, the queue entry will be deleted', ['parent' => $activity['reply-to-id']]);
+                               if (!empty($activity['entry-id'])) {
+                                       Queue::deleteById($activity['entry-id']);
+                               }
+                               return '';
+                       } elseif (!empty($result)) {
+                               $exists = Post::exists(['uri' => [$result, $activity['reply-to-id']]]);
+                               if ($exists) {
+                                       Logger::notice('The activity has been fetched and created.', ['parent' => $result]);
+                                       return $result;
+                               } elseif (DI::config()->get('system', 'fetch_by_worker') || DI::config()->get('system', 'decoupled_receiver')) {
+                                       Logger::notice('The activity has been fetched and will hopefully be created later.', ['parent' => $result]);
+                               } else {
+                                       Logger::notice('The activity exists but has not been created, the queue entry will be deleted.', ['parent' => $result]);
+                                       if (!empty($activity['entry-id'])) {
+                                               Queue::deleteById($activity['entry-id']);
+                                       }
+                               }
+                               return '';
+                       }
+                       if (empty($result) && !DI::config()->get('system', 'fetch_by_worker')) {
+                               return '';
+                       }
+               } elseif (self::isActivityGone($activity['reply-to-id'])) {
+                       Logger::notice('The activity is gone. We will not spawn a worker. The queue entry will be deleted', ['parent' => $activity['reply-to-id']]);
+                       if (!empty($activity['entry-id'])) {
+                               Queue::deleteById($activity['entry-id']);
+                       }
+                       return '';
+               } else {
+                       Logger::notice('Recursion level is too high.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+               }
+
+               if (Queue::hasWorker($activity['worker-id'] ?? 0)) {
+                       Logger::notice('There is already a worker task to fetch the post.', ['id' => $activity['id'], 'parent' => $activity['reply-to-id']]);
+                       return '';
+               }
+
+               if (!Fetch::hasWorker($activity['reply-to-id'])) {
+                       Logger::notice('Fetching is done by worker.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+                       Fetch::add($activity['reply-to-id']);
+                       $activity['recursion-depth'] = 0;
+                       $wid = Worker::add(PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
+                       Fetch::setWorkerId($activity['reply-to-id'], $wid);
+               } else {
+                       Logger::debug('Activity will already be fetched via a worker.', ['url' => $activity['reply-to-id']]);
+               }
+
+               return '';
+       }
+
+       /**
+        * Check if a given activity is no longer available
+        *
+        * @param string $url
+        *
+        * @return boolean
+        */
+       public static function isActivityGone(string $url): bool
+       {
+               $curlResult = HTTPSignature::fetchRaw($url, 0);
+
+               if (Network::isUrlBlocked($url)) {
+                       return true;
+               }
+
+               // @todo To ensure that the remote system is working correctly, we can check if the "Content-Type" contains JSON
+               if (in_array($curlResult->getReturnCode(), [401, 404])) {
+                       return true;
+               }
+
+               if ($curlResult->isSuccess()) {
+                       $object = json_decode($curlResult->getBody(), true);
+                       if (!empty($object)) {
+                               $activity = JsonLD::compact($object);
+                               if (JsonLD::fetchElement($activity, '@type') == 'as:Tombstone') {
+                                       return true;
+                               }
+                       }
+               } elseif ($curlResult->getReturnCode() == 0) {
+                       $host = parse_url($url, PHP_URL_HOST);
+                       if (!(filter_var($host, FILTER_VALIDATE_IP) || @dns_get_record($host . '.', DNS_A + DNS_AAAA))) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
        /**
         * Delete items
         *
@@ -418,6 +601,7 @@ class Processor
 
                Logger::info('Deleting item', ['object' => $activity['object_id'], 'owner'  => $owner]);
                Item::markForDeletion(['uri' => $activity['object_id'], 'owner-id' => $owner]);
+               Queue::remove($activity);
        }
 
        /**
@@ -453,14 +637,15 @@ class Processor
        /**
         * Prepare the item array for an activity
         *
-        * @param array  $activity Activity array
-        * @param string $verb     Activity verb
+        * @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)
        {
-               $item = self::createItem($activity);
+               $activity['reply-to-id'] = $activity['object_id'];
+               $item = self::createItem($activity, false);
                if (empty($item)) {
                        return;
                }
@@ -502,16 +687,13 @@ class Processor
                        }
                }
 
-               if ($activity['target_id'] != $actor['featured']) {
-                       return null;
-               }
-
-               $id = Contact::getIdForURL($activity['actor']);
-               if (empty($id)) {
-                       return null;
+               $parent = Post::selectFirst(['uri-id'], ['uri' => $activity['object_id']]);
+               if (empty($parent['uri-id'])) {
+                       if (self::fetchMissingActivity($activity['object_id'], $activity, '', Receiver::COMPLETION_AUTO)) {
+                               $parent = Post::selectFirst(['uri-id'], ['uri' => $activity['object_id']]);
+                       }
                }
 
-               $parent = Post::selectFirst(['uri-id'], ['uri' => $activity['object_id'], 'author-id' => $id]);
                if (!empty($parent['uri-id'])) {
                        return $parent['uri-id'];
                }
@@ -534,6 +716,7 @@ class Processor
                Logger::debug('Add post to featured collection', ['uri-id' => $uriid]);
 
                Post\Collection::add($uriid, Post\Collection::FEATURED);
+               Queue::remove($activity);
        }
 
        /**
@@ -551,6 +734,7 @@ class Processor
                Logger::debug('Remove post from featured collection', ['uri-id' => $uriid]);
 
                Post\Collection::remove($uriid, Post\Collection::FEATURED);
+               Queue::remove($activity);
        }
 
        /**
@@ -637,14 +821,14 @@ class Processor
                        $item['raw-body'] = $content;
                        $item['body'] = Item::improveSharedDataInBody($item);
                } else {
-                       if (empty($activity['directmessage']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
-                               $item_private = !in_array(0, $activity['item_receiver']);
-                               $parent = Post::selectFirst(['id', 'uri-id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
+                       $parent_uri = $item['parent-uri'] ?? $item['thr-parent'];
+                       if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
+                               $parent = Post::selectFirst(['id', 'uri-id', 'private', 'author-link', 'alias'], ['uri' => $parent_uri]);
                                if (!DBA::isResult($parent)) {
-                                       Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
+                                       Logger::warning('Unknown parent item.', ['uri' => $parent_uri]);
                                        return false;
                                }
-                               if ($item_private && ($parent['private'] != Item::PRIVATE)) {
+                               if (!empty($activity['type']) && in_array($activity['type'], Receiver::CONTENT_TYPES) && ($item['private'] == Item::PRIVATE) && ($parent['private'] != Item::PRIVATE)) {
                                        Logger::warning('Item is private but the parent is not. Dropping.', ['item-uri' => $item['uri'], 'thr-parent' => $item['thr-parent']]);
                                        return false;
                                }
@@ -769,10 +953,14 @@ class Processor
                }
 
                $stored = false;
+               $success = false;
                ksort($activity['receiver']);
 
                if (!self::isSolicitedMessage($activity, $item)) {
                        DBA::delete('item-uri', ['id' => $item['uri-id']]);
+                       if (!empty($activity['entry-id'])) {
+                               Queue::deleteById($activity['entry-id']);
+                       }
                        return;
                }
 
@@ -810,10 +998,16 @@ class Processor
                                        $item['post-reason'] = Item::PR_NONE;
                        }
 
-                       if (!empty($activity['from-relay'])) {
-                               $item['post-reason'] = Item::PR_RELAY;
-                       } elseif (!empty($activity['thread-completion'])) {
-                               $item['post-reason'] = Item::PR_FETCHED;
+                       $item['post-reason'] = Item::getPostReason($item);
+
+                       if (in_array($item['post-reason'], [Item::PR_GLOBAL, Item::PR_NONE])) {
+                               if (!empty($activity['from-relay'])) {
+                                       $item['post-reason'] = Item::PR_RELAY;
+                               } elseif (!empty($activity['thread-completion'])) {
+                                       $item['post-reason'] = Item::PR_FETCHED;
+                               } elseif (!empty($activity['push'])) {
+                                       $item['post-reason'] = Item::PR_PUSHED;
+                               }
                        }
 
                        if ($item['isForum'] ?? false) {
@@ -831,41 +1025,31 @@ class Processor
                                continue;
                        }
 
-                       if (!($item['isForum'] ?? false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT) && !Contact::isSharingByURL($activity['author'], $receiver)) {
-                               if ($item['post-reason'] == Item::PR_BCC) {
-                                       Logger::info('Top level post via BCC from a non sharer, ignoring', ['uid' => $receiver, 'contact' => $item['contact-id']]);
-                                       continue;
-                               }
+                       if (($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT) && !in_array($item['post-reason'], [Item::PR_FOLLOWER, Item::PR_TAG, item::PR_TO, Item::PR_CC])) {
+                               if (!($item['isForum'] ?? false)) {
+                                       if ($item['post-reason'] == Item::PR_BCC) {
+                                               Logger::info('Top level post via BCC from a non sharer, ignoring', ['uid' => $receiver, 'contact' => $item['contact-id'], 'url' => $item['uri']]);
+                                               continue;
+                                       }
 
-                               if (
-                                       !empty($activity['thread-children-type'])
-                                       && in_array($activity['thread-children-type'], Receiver::ACTIVITY_TYPES)
-                                       && DI::pConfig()->get($receiver, 'system', 'accept_only_sharer') != Item::COMPLETION_LIKE
-                               ) {
-                                       Logger::info('Top level post from thread completion from a non sharer had been initiated via an activity, ignoring',
-                                               ['type' => $activity['thread-children-type'], 'user' => $item['uid'], 'causer' => $item['causer-link'], 'author' => $activity['author'], 'url' => $item['uri']]);
-                                       continue;
+                                       if ((DI::pConfig()->get($receiver, 'system', 'accept_only_sharer') != Item::COMPLETION_LIKE)
+                                               && in_array($activity['thread-children-type'] ?? '', Receiver::ACTIVITY_TYPES)) {
+                                               Logger::info('Top level post from thread completion from a non sharer had been initiated via an activity, ignoring',
+                                                       ['type' => $activity['thread-children-type'], 'user' => $item['uid'], 'causer' => $item['causer-link'], 'author' => $activity['author'], 'url' => $item['uri']]);
+                                               continue;
+                                       }
                                }
-                       }
-
-                       $is_forum = false;
 
-                       if ($receiver != 0) {
+                               $is_forum = false;
                                $user = User::getById($receiver, ['account-type']);
                                if (!empty($user['account-type'])) {
                                        $is_forum = ($user['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
                                }
-                       }
-
-                       if (!$is_forum && DI::pConfig()->get($receiver, 'system', 'accept_only_sharer') == Item::COMPLETION_NONE && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) {
-                               $skip = !Contact::isSharingByURL($activity['author'], $receiver);
 
-                               if ($skip && (($activity['type'] == 'as:Announce') || ($item['isForum'] ?? false))) {
-                                       $skip = !Contact::isSharingByURL($activity['actor'], $receiver);
-                               }
-
-                               if ($skip) {
-                                       Logger::info('Skipping post', ['uid' => $receiver, 'url' => $item['uri']]);
+                               if ((DI::pConfig()->get($receiver, 'system', 'accept_only_sharer') == Item::COMPLETION_NONE)
+                                       && ((!$is_forum && !($item['isForum'] ?? false) && ($activity['type'] != 'as:Announce'))
+                                       || !Contact::isSharingByURL($activity['actor'], $receiver))) {
+                                       Logger::info('Actor is a non sharer, is no forum or it is no announce', ['uid' => $receiver, 'actor' => $activity['actor'], 'url' => $item['uri'], 'type' => $activity['type']]);
                                        continue;
                                }
 
@@ -881,8 +1065,13 @@ class Processor
                        $item_id = Item::insert($item);
                        if ($item_id) {
                                Logger::info('Item insertion successful', ['user' => $item['uid'], 'item_id' => $item_id]);
+                               $success = true;
                        } else {
-                               Logger::notice('Item insertion aborted', ['user' => $item['uid']]);
+                               Logger::notice('Item insertion aborted', ['uri' => $item['uri'], 'uid' => $item['uid']]);
+                               if (($item['uid'] == 0) && (count($activity['receiver']) > 1)) {
+                                       Logger::info('Public item was aborted. We skip for all users.', ['uri' => $item['uri']]);
+                                       break;
+                               }
                        }
 
                        if ($item['uid'] == 0) {
@@ -890,8 +1079,14 @@ class Processor
                        }
                }
 
+               Queue::remove($activity);
+
+               if ($success && Queue::hasChildren($item['uri']) && Post::exists(['uri' => $item['uri']])) {
+                       Queue::processReplyByUri($item['uri']);
+               }
+
                // 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') {
@@ -1103,49 +1298,78 @@ class Processor
                Logger::info('Fetched featured posts', ['new' => $new, 'old' => $old, 'contact' => $url]);
        }
 
+       public static function fetchCachedActivity(string $url, int $uid): array
+       {
+               $cachekey = self::CACHEKEY_FETCH_ACTIVITY . $uid . ':' . $url;
+               $object = DI::cache()->get($cachekey);
+
+               if (!is_null($object)) {
+                       if (!empty($object)) {
+                               Logger::debug('Fetch from cache', ['url' => $url, 'uid' => $uid]);
+                       } else {
+                               Logger::debug('Fetch from negative cache', ['url' => $url, 'uid' => $uid]);
+                       }
+                       return $object;
+               }
+
+               $object = ActivityPub::fetchContent($url, $uid);
+               if (empty($object)) {
+                       Logger::notice('Activity was not fetchable, aborting.', ['url' => $url, 'uid' => $uid]);
+                       // We perform negative caching.
+                       DI::cache()->set($cachekey, [], Duration::FIVE_MINUTES);
+                       return [];
+               }
+
+               if (empty($object['id'])) {
+                       Logger::notice('Activity has got not id, aborting. ', ['url' => $url, 'object' => $object]);
+                       return [];
+               }
+               DI::cache()->set($cachekey, $object, Duration::FIVE_MINUTES);
+
+               Logger::debug('Activity was fetched successfully', ['url' => $url, 'uid' => $uid]);
+
+               return $object;
+       }
+
        /**
         * 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 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
        {
-               if (!empty($child['receiver'])) {
-                       $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']);
-               } else {
-                       $uid = 0;
-               }
-
-               $object = ActivityPub::fetchContent($url, $uid);
+               $object = self::fetchCachedActivity($url, 0);
                if (empty($object)) {
-                       Logger::notice('Activity was not fetchable, aborting.', ['url' => $url]);
                        return '';
                }
 
-               if (empty($object['id'])) {
-                       Logger::notice('Activity has got not id, aborting. ', ['url' => $url, 'object' => $object]);
-                       return '';
+               $signer = [];
+
+               if (!empty($object['attributedTo'])) {
+                       $attributed_to = $object['attributedTo'];
+                       if (is_array($attributed_to)) {
+                               $compacted = JsonLD::compact($object);
+                               $attributed_to = JsonLD::fetchElement($compacted, 'as:attributedTo', '@id');
+                       }
+                       $signer[] = $attributed_to;
                }
 
                if (!empty($object['actor'])) {
                        $object_actor = $object['actor'];
-               } elseif (!empty($object['attributedTo'])) {
-                       $object_actor = $object['attributedTo'];
-                       if (is_array($object_actor)) {
-                               $compacted = JsonLD::compact($object);
-                               $object_actor = JsonLD::fetchElement($compacted, 'as:attributedTo', '@id');
-                       }
+               } elseif (!empty($attributed_to)) {
+                       $object_actor = $attributed_to;
                } else {
                        // Shouldn't happen
                        $object_actor = '';
                }
 
-               $signer = [$object_actor];
+               $signer[] = $object_actor;
 
                if (!empty($child['author'])) {
                        $actor = $child['author'];
@@ -1175,6 +1399,8 @@ class Processor
 
                $ldactivity = JsonLD::compact($activity);
 
+               $ldactivity['recursion-depth'] = !empty($child['recursion-depth']) ? $child['recursion-depth'] + 1 : 0;
+
                if (!empty($relay_actor)) {
                        $ldactivity['thread-completion'] = $ldactivity['from-relay'] = Contact::getIdForURL($relay_actor);
                        $ldactivity['completion-mode']   = Receiver::COMPLETION_RELAY;
@@ -1186,17 +1412,25 @@ class Processor
                        $ldactivity['completion-mode']   = $completion;
                }
 
-               if (!empty($child['type'])) {
+               if (!empty($child['thread-children-type'])) {
+                       $ldactivity['thread-children-type'] = $child['thread-children-type'];
+               } elseif (!empty($child['type'])) {
                        $ldactivity['thread-children-type'] = $child['type'];
+               } else {
+                       $ldactivity['thread-children-type'] = 'as:Create';
                }
 
                if (!empty($relay_actor) && !self::acceptIncomingMessage($ldactivity, $object['id'])) {
                        return '';
                }
 
-               ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), $uid, true, false, $signer);
-
-               Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]);
+               if (($completion == Receiver::COMPLETION_RELAY) && Queue::exists($url, 'as:Create')) {
+                       Logger::notice('Activity has already been queued.', ['url' => $url, 'object' => $activity['id']]);
+               } elseif (ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), 0, true, false, $signer, '', $completion)) {
+                       Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'entry' => $child['entry-id'] ?? 0, 'completion' => $completion, 'object' => $activity['id']]);
+               } else {
+                       Logger::notice('Activity had been fetched and will be processed later.', ['url' => $url, 'entry' => $child['entry-id'] ?? 0, 'completion' => $completion, 'object' => $activity['id']]);
+               }
 
                return $activity['id'];
        }
@@ -1253,6 +1487,7 @@ class Processor
        {
                $uid = User::getIdForURL($activity['object_id']);
                if (empty($uid)) {
+                       Queue::remove($activity);
                        return;
                }
 
@@ -1292,8 +1527,8 @@ class Processor
                if (empty($contact)) {
                        Contact::update(['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
                }
-
                Logger::notice('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
+               Queue::remove($activity);
        }
 
        /**
@@ -1337,6 +1572,7 @@ class Processor
 
                Logger::info('Updating profile', ['object' => $activity['object_id']]);
                Contact::updateFromProbeByURL($activity['object_id']);
+               Queue::remove($activity);
        }
 
        /**
@@ -1365,6 +1601,7 @@ class Processor
                DBA::close($contacts);
 
                Logger::info('Deleted contact', ['object' => $activity['object_id']]);
+               Queue::remove($activity);
        }
 
        /**
@@ -1389,6 +1626,7 @@ class Processor
                Contact\User::setIsBlocked($cid, $uid, true);
 
                Logger::info('Contact blocked user', ['contact' => $cid, 'user' => $uid]);
+               Queue::remove($activity);
        }
 
        /**
@@ -1413,6 +1651,7 @@ class Processor
                Contact\User::setIsBlocked($cid, $uid, false);
 
                Logger::info('Contact unblocked user', ['contact' => $cid, 'user' => $uid]);
+               Queue::remove($activity);
        }
 
        /**
@@ -1447,6 +1686,7 @@ class Processor
                $condition = ['id' => $cid];
                Contact::update($fields, $condition);
                Logger::info('Accept contact request', ['contact' => $cid, 'user' => $uid]);
+               Queue::remove($activity);
        }
 
        /**
@@ -1480,6 +1720,7 @@ class Processor
                } else {
                        Logger::info('Rejected contact request', ['contact' => $cid, 'user' => $uid]);
                }
+               Queue::remove($activity);
        }
 
        /**
@@ -1505,6 +1746,7 @@ class Processor
                }
 
                Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
+               Queue::remove($activity);
        }
 
        /**
@@ -1541,12 +1783,14 @@ class Processor
 
                Contact::removeFollower($contact);
                Logger::info('Undo following request', ['contact' => $cid, 'user' => $uid]);
+               Queue::remove($activity);
        }
 
        /**
         * Switches a contact to AP if needed
         *
         * @param integer $cid Contact ID
+        * @return void
         * @throws \Exception
         */
        private static function switchContact(int $cid)