]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Improved relay post processing
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index 7ba5266b46574a156f107a0c473775585d5ed3e0..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;
                        }
                }
 
@@ -471,7 +467,7 @@ class Processor
                $item['uri'] = $activity['id'];
 
                if (empty($activity['published']) || empty($activity['updated'])) {
-                       DI::logger()->notice('published or updated keys are empty for activity', ['activity' => $activity, 'callstack' => System::callstack(10)]);
+                       DI::logger()->notice('published or updated keys are empty for activity', ['activity' => $activity]);
                }
 
                $item['created'] = DateTimeFormat::utc($activity['published'] ?? 'now');
@@ -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 {
@@ -1113,8 +1107,7 @@ class Processor
                                $item['contact-id'] = Contact::getIdForURL($activity['author']);
                        }
 
-                       if (!empty($activity['directmessage'])) {
-                               self::postMail($activity, $item);
+                       if (!empty($activity['directmessage']) && self::postMail($item)) {
                                continue;
                        }
 
@@ -1346,18 +1339,22 @@ class Processor
        /**
         * Creates an mail post
         *
-        * @param array $activity Activity data
-        * @param array $item     item array
+        * @param array $item item array
         * @return int|bool New mail table row id or false on error
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function postMail(array $activity, array $item)
+       private static function postMail(array $item): bool
        {
                if (($item['gravity'] != Item::GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
                        Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]);
                        return false;
                }
 
+               if (!Contact::isFollower($item['contact-id'], $item['uid']) && !Contact::isSharing($item['contact-id'], $item['uid'])) {
+                       Logger::info('Contact is not a sharer or follower, mail will be discarded.', ['item' => $item]);
+                       return false;
+               }
+
                Logger::info('Direct Message', $item);
 
                $msg = [];
@@ -1453,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'])) {
@@ -1496,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.
@@ -1523,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 '';
                }
 
@@ -1563,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) {
@@ -1603,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;
                        }
                }
 
@@ -1627,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
         *
@@ -1651,20 +1702,64 @@ class Processor
                $attributed_to = JsonLD::fetchElement($activity['as:object'], 'as:attributedTo', '@id');
                $authorid = Contact::getIdForURL($attributed_to);
 
-               $body = HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value') ?? '');
+               $content = JsonLD::fetchElement($activity['as:object'], 'as:name', '@value') ?? '';
+               $content .= ' ' . JsonLD::fetchElement($activity['as:object'], 'as:summary', '@value') ?? '';
+               $content .= ' ' . HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value') ?? '');
+
+               $attachments = JsonLD::fetchElementArray($activity['as:object'], 'as:attachment') ?? [];
+               foreach ($attachments as $media) {
+                       if (!empty($media['as:summary'])) {
+                               $content .= ' ' . JsonLD::fetchElement($media, 'as:summary', '@value');
+                       }
+                       if (!empty($media['as:name'])) {
+                               $content .= ' ' . JsonLD::fetchElement($media, 'as:name', '@value');
+                       }
+               }
 
                $messageTags = [];
                $tags = Receiver::processTags(JsonLD::fetchElementArray($activity['as:object'], 'as:tag') ?? []);
                if (!empty($tags)) {
                        foreach ($tags as $tag) {
-                               if ($tag['type'] != 'Hashtag') {
+                               if (($tag['type'] != 'Hashtag') && !strpos($tag['type'], ':Hashtag')) {
                                        continue;
                                }
                                $messageTags[] = ltrim(mb_strtolower($tag['name']), '#');
                        }
                }
 
-               return Relay::isSolicitedPost($messageTags, $body, $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;
        }
 
        /**
@@ -1893,8 +1988,8 @@ class Processor
         */
        public static function ReportAccount(array $activity)
        {
-               $account_id = Contact::getIdForURL($activity['object_id']);
-               if (empty($account_id)) {
+               $account = Contact::getByURL($activity['object_id'], null, ['id', 'gsid']);
+               if (empty($account)) {
                        Logger::info('Unknown account', ['activity' => $activity]);
                        Queue::remove($activity);
                        return;
@@ -1915,10 +2010,10 @@ class Processor
                        }
                }
 
-               $report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, '', false, $uri_ids);
+               $report = DI::reportFactory()->createFromReportsRequest(System::getRules(true), $reporter_id, $account['id'], $account['gsid'], $activity['content'], 'other', false, $uri_ids);
                DI::report()->save($report);
 
-               Logger::info('Stored report', ['reporter' => $reporter_id, 'account_id' => $account_id, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]);
+               Logger::info('Stored report', ['reporter' => $reporter_id, 'account' => $account, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]);
        }
 
        /**