]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Improved relay post processing
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index df19ab8b8ffd152fea35aa1df61d1bd31e0cc028..83c068e0edca7a170be3ac9617139383b88c3af9 100644 (file)
@@ -348,12 +348,8 @@ class Processor
 
                if ($fetch_parents && empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
                        $result = self::fetchParent($activity, !empty($conversation));
-                       if (!empty($result)) {
-                               if (($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
-                                       $item['thr-parent'] = $result;
-                               }
-                       } elseif (empty($conversation)) {
-                               return [];
+                       if (!empty($result) && ($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
+                               $item['thr-parent'] = $result;
                        }
                }
 
@@ -532,39 +528,35 @@ class Processor
 
                self::addActivityId($activity['reply-to-id']);
 
-               if (!DI::config()->get('system', 'fetch_by_worker')) {
-                       $in_background = false;
+               $completion = $activity['completion-mode'] ?? Receiver::COMPLETION_NONE;
+
+               if (DI::config()->get('system', 'decoupled_receiver') && ($completion != Receiver::COMPLETION_MANUAL)) {
+                       $in_background = true;
                }
 
                $recursion_depth = $activity['recursion-depth'] ?? 0;
 
                if (!$in_background && ($recursion_depth < DI::config()->get('system', 'max_recursion_depth'))) {
-                       Logger::info('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
+                       Logger::info('Parent not found. Try to refetch it.', ['completion' => $completion, 'recursion-depth' => $recursion_depth, 'parent' => $activity['reply-to-id']]);
                        $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::info('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::info('The activity has been fetched and will hopefully be created later.', ['parent' => $result]);
+                               $post = Post::selectFirstPost(['uri'], ['uri' => [$result, $activity['reply-to-id']]]);
+                               if (!empty($post['uri'])) {
+                                       Logger::info('The activity has been fetched and created.', ['result' => $result, 'uri' => $post['uri']]);
+                                       return $post['uri'];
                                } 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 '';
                        }
+                       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 ($in_background) {
@@ -586,7 +578,7 @@ class Processor
                        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(Worker::PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
+                       $wid = Worker::add(Worker::PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_ASYNC);
                        Fetch::setWorkerId($activity['reply-to-id'], $wid);
                } else {
                        Logger::debug('Activity will already be fetched via a worker.', ['url' => $activity['reply-to-id']]);
@@ -604,16 +596,16 @@ class Processor
         */
        public static function isActivityGone(string $url): bool
        {
+               if (Network::isUrlBlocked($url)) {
+                       return true;
+               }
+
                try {
                        $curlResult = HTTPSignature::fetchRaw($url, 0);
                } catch (\Exception $exception) {
                        Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
                        return true;
-               }
-
-               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])) {
@@ -745,7 +737,7 @@ class Processor
                }
 
                if (!empty($parent['uri-id'])) {
-                       $parent;
+                       return $parent;
                }
 
                return null;
@@ -760,6 +752,7 @@ class Processor
        {
                $post = self::getUriIdForFeaturedCollection($activity);
                if (empty($post)) {
+                       Queue::remove($activity);
                        return;
                }
 
@@ -778,6 +771,7 @@ class Processor
        {
                $post = self::getUriIdForFeaturedCollection($activity);
                if (empty($post)) {
+                       Queue::remove($activity);
                        return;
                }
 
@@ -867,7 +861,7 @@ class Processor
                $content = self::addMentionLinks($content, $activity['tags']);
 
                if (!empty($activity['quote-url'])) {
-                       $id = Item::fetchByLink($activity['quote-url']);
+                       $id = Item::fetchByLink($activity['quote-url'], 0, ActivityPub\Receiver::COMPLETION_ASYNC);
                        if ($id) {
                                $shared_item = Post::selectFirst(['uri-id'], ['id' => $id]);
                                $item['quote-uri-id'] = $shared_item['uri-id'];
@@ -1007,7 +1001,7 @@ class Processor
                }
 
                $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name');
-               if (Relay::isSolicitedPost($tags, $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0)) {
+               if (Relay::isSolicitedPost($tags, $item['title'] . ' ' . ($item['content-warning'] ?? '') . ' ' . $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0)) {
                        Logger::debug('Post is accepted because of the relay settings', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]);
                        return true;
                } else {
@@ -1456,7 +1450,7 @@ class Processor
                        if (empty($post['id'])) {
                                continue;
                        }
-                       $id = Item::fetchByLink($post['id']);
+                       $id = Item::fetchByLink($post['id'], 0, ActivityPub\Receiver::COMPLETION_ASYNC);
                        if (!empty($id)) {
                                $item = Post::selectFirst(['uri-id', 'featured', 'author-id'], ['id' => $id]);
                                if (!empty($item['uri-id'])) {
@@ -1499,7 +1493,7 @@ class Processor
                        return $object;
                }
 
-               $object = ActivityPub::fetchContent($url, $uid);
+               $object = HTTPSignature::fetch($url, $uid);
                if (empty($object)) {
                        Logger::notice('Activity was not fetchable, aborting.', ['url' => $url, 'uid' => $uid]);
                        // We perform negative caching.
@@ -1526,14 +1520,43 @@ class Processor
         * @param string     $relay_actor Relay actor
         * @param int        $completion  Completion mode, see Receiver::COMPLETION_*
         * @param int        $uid         User id that is used to fetch the activity
-        * @return string fetched message URL
+        * @return string fetched message URL. An empty string indicates a temporary error, null indicates a permament error,
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL, int $uid = 0): string
+       public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL, int $uid = 0): ?string
        {
-               $object = self::fetchCachedActivity($url, $uid);
-               if (empty($object)) {
+               if (Network::isUrlBlocked($url)) {
+                       return null;
+               }
+
+               try {
+                       $curlResult = HTTPSignature::fetchRaw($url, $uid);
+               } catch (\Exception $exception) {
+                       Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
+                       return '';
+               }
+
+               if (empty($curlResult)) {
+                       return '';
+               }
+
+               $body = $curlResult->getBody();
+               if (!$curlResult->isSuccess() || empty($body)) {
+                       if (in_array($curlResult->getReturnCode(), [403, 404, 406, 410])) {
+                               return null;
+                       }
+                       return '';
+               }
+
+               $object = json_decode($body, true);
+               if (empty($object) || !is_array($object)) {
+                       $element = explode(';', $curlResult->getContentType());
+                       if (!in_array($element[0], ['application/activity+json', 'application/ld+json', 'application/json'])) {
+                               Logger::debug('Unexpected content-type', ['url' => $url, 'content-type' => $curlResult->getContentType()]);
+                               return null;
+                       }
+                       Logger::notice('Invalid JSON data', ['url' => $url, 'content-type' => $curlResult->getContentType(), 'body' => $body]);
                        return '';
                }
 
@@ -1566,28 +1589,28 @@ class Processor
                        $actor = $object_actor;
                }
 
-               if (!empty($object['published'])) {
-                       $published = $object['published'];
-               } elseif (!empty($child['published'])) {
-                       $published = $child['published'];
-               } else {
-                       $published = DateTimeFormat::utcNow();
-               }
+               $ldobject = JsonLD::compact($object);
 
-               $activity = [];
-               $activity['@context'] = $object['@context'] ?? ActivityPub::CONTEXT;
-               unset($object['@context']);
-               $activity['id'] = $object['id'];
-               $activity['to'] = $object['to'] ?? [];
-               $activity['cc'] = $object['cc'] ?? [];
-               $activity['audience'] = $object['audience'] ?? [];
-               $activity['actor'] = $actor;
-               $activity['object'] = $object;
-               $activity['published'] = $published;
-               $activity['type'] = 'Create';
+               $type      = JsonLD::fetchElement($ldobject, '@type');
+               $object_id = JsonLD::fetchElement($ldobject, 'as:object', '@id');
 
-               $ldactivity = JsonLD::compact($activity);
+               if (!in_array($type, Receiver::CONTENT_TYPES) && !empty($object_id)) {
+                       if (($type == 'as:Announce') && !empty($relay_actor) && ($completion = Receiver::COMPLETION_RELAY)) {
+                               if (Item::searchByLink($object_id)) {
+                                       return $object_id;
+                               }
+                               Logger::debug('Fetch announced activity', ['type' => $type, 'id' => $object_id, 'actor' => $relay_actor, 'signer' => $signer]);
 
+                               return self::fetchMissingActivity($object_id, $child, $relay_actor, $completion, $uid);
+                       }
+                       $activity   = $object;
+                       $ldactivity = $ldobject;
+               } else {
+                       $activity   = self::getActivityForObject($object, $actor);
+                       $ldactivity = JsonLD::compact($activity);
+                       $object_id  = $object['id'];
+               }
+       
                $ldactivity['recursion-depth'] = !empty($child['recursion-depth']) ? $child['recursion-depth'] + 1 : 0;
 
                if ($object_actor != $actor) {
@@ -1606,8 +1629,8 @@ class Processor
 
                if ($completion == Receiver::COMPLETION_RELAY) {
                        $ldactivity['from-relay'] = $ldactivity['thread-completion'];
-                       if (!self::acceptIncomingMessage($ldactivity, $object['id'])) {
-                               return '';
+                       if (in_array($type, Receiver::CONTENT_TYPES) && !self::acceptIncomingMessage($ldactivity, $object_id)) {
+                               return null;
                        }
                }
 
@@ -1630,6 +1653,31 @@ class Processor
                return $activity['id'];
        }
 
+       private static function getActivityForObject(array $object, string $actor): array
+       {
+               if (!empty($object['published'])) {
+                       $published = $object['published'];
+               } elseif (!empty($child['published'])) {
+                       $published = $child['published'];
+               } else {
+                       $published = DateTimeFormat::utcNow();
+               }
+
+               $activity = [];
+               $activity['@context'] = $object['@context'] ?? ActivityPub::CONTEXT;
+               unset($object['@context']);
+               $activity['id'] = $object['id'];
+               $activity['to'] = $object['to'] ?? [];
+               $activity['cc'] = $object['cc'] ?? [];
+               $activity['audience'] = $object['audience'] ?? [];
+               $activity['actor'] = $actor;
+               $activity['object'] = $object;
+               $activity['published'] = $published;
+               $activity['type'] = 'Create';
+
+               return $activity;
+       }
+
        /**
         * Test if incoming relay messages should be accepted
         *
@@ -1679,7 +1727,39 @@ class Processor
                        }
                }
 
-               return Relay::isSolicitedPost($messageTags, $content, $authorid, $id, Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0);
+               $languages = self::getPostLanguages($activity);
+
+               return Relay::isSolicitedPost($messageTags, $content, $authorid, $id, Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0, $languages);
+       }
+
+       /**
+        * Fetch the post language from the content
+        *
+        * @param array $activity
+        * @return array
+        */
+       private static function getPostLanguages(array $activity): array
+       {
+               $content   = JsonLD::fetchElement($activity['as:object'], 'as:content') ?? '';
+               $languages = JsonLD::fetchElementArray($activity['as:object'], 'as:content', '@language') ?? [];
+               if (empty($languages)) {
+                       return [];
+               }
+
+               $iso639 = new \Matriphe\ISO639\ISO639;
+
+               $result = [];
+               foreach ($languages as $language) {
+                       if ($language == $content) {
+                               continue;
+                       }
+                       $language = DI::l10n()->toISO6391($language);
+                       if (!in_array($language, array_column($iso639->allLanguages(), 0))) {
+                               continue;
+                       }
+                       $result[] = $language;
+               }
+               return $result;
        }
 
        /**