]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Improved relay post processing
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index f9957da8af3026611c0b135c6b6d2fff5844d4c5..83c068e0edca7a170be3ac9617139383b88c3af9 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -93,7 +93,9 @@ class Processor
         */
        public static function normalizeMentionLinks(string $body): string
        {
-               return preg_replace('%\[url=([^\[\]]*)]([#@!])(.*?)\[/url]%ism', '$2[url=$1]$3[/url]', $body);
+               $body = preg_replace('%\[url=([^\[\]]*)]([#@!])(.*?)\[/url]%ism', '$2[url=$1]$3[/url]', $body);
+               $body = preg_replace('%([#@!])\[zrl=([^\[\]]*)](.*?)\[/zrl]%ism', '$1[url=$2]$3[/url]', $body);
+               return $body;
        }
 
        /**
@@ -242,6 +244,7 @@ class Processor
                $item['changed'] = DateTimeFormat::utcNow();
                $item['edited'] = DateTimeFormat::utc($activity['updated']);
 
+               Post\Media::deleteByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE, Post\Media::HTML]);
                $item = self::processContent($activity, $item);
                if (empty($item)) {
                        Queue::remove($activity);
@@ -345,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;
                        }
                }
 
@@ -412,13 +411,13 @@ class Processor
                        $item['post-type'] = Item::PT_NOTE;
                }
 
-               $item['isForum'] = false;
+               $item['isGroup'] = false;
 
                if (!empty($activity['thread-completion'])) {
                        if ($activity['thread-completion'] != $item['owner-id']) {
                                $actor = Contact::getById($activity['thread-completion'], ['url']);
                                $item['causer-link'] = $actor['url'];
-                               $item['causer-id'] = $activity['thread-completion'];
+                               $item['causer-id']   = $activity['thread-completion'];
                                Logger::info('Use inherited actor as causer.', ['id' => $item['owner-id'], 'activity' => $activity['thread-completion'], 'owner' => $item['owner-link'], 'actor' => $actor['url']]);
                        } else {
                                // Store the original actor in the "causer" fields to enable the check for ignored or blocked contacts
@@ -428,16 +427,47 @@ class Processor
                        }
 
                        $item['owner-link'] = $item['author-link'];
-                       $item['owner-id'] = $item['author-id'];
+                       $item['owner-id']   = $item['author-id'];
+               }
+
+               if (!$item['isGroup'] && !empty($activity['receiver_urls']['as:audience'])) {
+                       foreach ($activity['receiver_urls']['as:audience'] as $audience) {
+                               $actor = APContact::getByURL($audience, false);
+                               if (($actor['type'] ?? 'Person') == 'Group') {
+                                       Logger::debug('Group post detected via audience.', ['audience' => $audience, 'actor' => $activity['actor'], 'author' => $activity['author']]);
+                                       $item['isGroup']    = true;
+                                       $item['group-link'] = $item['owner-link'] = $audience;
+                                       $item['owner-id']   = Contact::getIdForURL($audience);
+                                       break;
+                               }
+                       }
                } else {
-                       $actor = APContact::getByURL($item['owner-link'], false);
-                       $item['isForum'] = ($actor['type'] ?? 'Person') == 'Group';
+                       $owner = APContact::getByURL($item['owner-link'], false);
+               }
+
+               if (!$item['isGroup'] && (($owner['type'] ?? 'Person') == 'Group')) {
+                       Logger::debug('Group post detected via owner.', ['actor' => $activity['actor'], 'author' => $activity['author']]);
+                       $item['isGroup']    = true;
+                       $item['group-link'] = $item['owner-link'];
+               } elseif (!empty($item['causer-link'])) {
+                       $causer = APContact::getByURL($item['causer-link'], false);
+               }
+
+               if (!$item['isGroup'] && (($causer['type'] ?? 'Person') == 'Group')) {
+                       Logger::debug('Group post detected via causer.', ['actor' => $activity['actor'], 'author' => $activity['author'], 'causer' => $item['causer-link']]);
+                       $item['isGroup']    = true;
+                       $item['group-link'] = $item['causer-link'];
+               }
+
+               if (!empty($item['group-link']) && empty($item['causer-link'])) {
+                       $item['causer-link'] = $item['group-link'];
+                       $item['causer-id']   = Contact::getIdForURL($item['causer-link']);
                }
 
                $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');
@@ -498,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::notice('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::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]);
+                               $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) {
@@ -552,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']]);
@@ -570,12 +596,17 @@ class Processor
         */
        public static function isActivityGone(string $url): bool
        {
-               $curlResult = HTTPSignature::fetchRaw($url, 0);
-
                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;
+               }       
+
                // @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;
@@ -706,7 +737,7 @@ class Processor
                }
 
                if (!empty($parent['uri-id'])) {
-                       $parent;
+                       return $parent;
                }
 
                return null;
@@ -721,6 +752,7 @@ class Processor
        {
                $post = self::getUriIdForFeaturedCollection($activity);
                if (empty($post)) {
+                       Queue::remove($activity);
                        return;
                }
 
@@ -739,6 +771,7 @@ class Processor
        {
                $post = self::getUriIdForFeaturedCollection($activity);
                if (empty($post)) {
+                       Queue::remove($activity);
                        return;
                }
 
@@ -828,10 +861,13 @@ 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'];
+                       } elseif ($uri_id = ItemURI::getIdByURI($activity['quote-url'], false)) {
+                               Logger::info('Quote was not fetched but the uri-id existed', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'quote' => $activity['quote-url'], 'uri-id' => $uri_id]);
+                               $item['quote-uri-id'] = $uri_id;
                        } else {
                                Logger::info('Quote was not fetched', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'quote' => $activity['quote-url']]);
                        }
@@ -855,17 +891,25 @@ class Processor
                                        Logger::warning('Unknown parent item.', ['uri' => $parent_uri]);
                                        return false;
                                }
-                               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;
-                               }
-
                                $content = self::removeImplicitMentionsFromBody($content, $parent);
                        }
                        $item['content-warning'] = HTML::toBBCode($activity['summary'] ?? '');
                        $item['raw-body'] = $item['body'] = $content;
                }
 
+               if (!empty($item['author-id']) && ($item['author-id'] == $item['owner-id'])) {
+                       foreach (Tag::getFromBody($item['body'], Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]) as $tag) {
+                               $actor = APContact::getByURL($tag[2], false);
+                               if (($actor['type'] ?? 'Person') == 'Group') {
+                                       Logger::debug('Group post detected via exclusive mention.', ['mention' => $actor['url'], 'actor' => $activity['actor'], 'author' => $activity['author']]);
+                                       $item['isGroup']    = true;
+                                       $item['group-link'] = $item['owner-link'] = $actor['url'];
+                                       $item['owner-id']   = Contact::getIdForURL($actor['url']);
+                                       break;
+                               }
+                       }
+               }
+
                self::storeFromBody($item);
                self::storeTags($item['uri-id'], $activity['tags']);
 
@@ -943,7 +987,7 @@ class Processor
                        return true;
                }
 
-               if (in_array($activity['completion-mode'] ?? Receiver::COMPLETION_NONE, [Receiver::COMPLETION_MANUAL, Receiver::COMPLETION_ANNOUCE])) {
+               if (in_array($activity['completion-mode'] ?? Receiver::COMPLETION_NONE, [Receiver::COMPLETION_MANUAL, Receiver::COMPLETION_ANNOUNCE])) {
                        // Manual completions and completions caused by reshares are allowed without any further checks.
                        Logger::debug('Message is in completion mode - accepted', ['mode' => $activity['completion-mode'], 'uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]);
                        return true;
@@ -957,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 {
@@ -1020,6 +1064,9 @@ class Processor
                                case Receiver::TARGET_BCC:
                                        $item['post-reason'] = Item::PR_BCC;
                                        break;
+                               case Receiver::TARGET_AUDIENCE:
+                                       $item['post-reason'] = Item::PR_AUDIENCE;
+                                       break;
                                case Receiver::TARGET_FOLLOWER:
                                        $item['post-reason'] = Item::PR_FOLLOWER;
                                        break;
@@ -1050,23 +1097,22 @@ class Processor
                                $item['causer-id'] = ($item['gravity'] == Item::GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
                        }
 
-                       if ($item['isForum'] ?? false) {
-                               $item['contact-id'] = Contact::getIdForURL($activity['actor'], $receiver);
+                       if ($item['isGroup']) {
+                               $item['contact-id'] = Contact::getIdForURL($item['group-link'], $receiver);
                        } else {
-                               $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver);
+                               $item['contact-id'] = Contact::getIdForURL($item['author-link'], $receiver);
                        }
 
                        if (($receiver != 0) && empty($item['contact-id'])) {
                                $item['contact-id'] = Contact::getIdForURL($activity['author']);
                        }
 
-                       if (!empty($activity['directmessage'])) {
-                               self::postMail($activity, $item);
+                       if (!empty($activity['directmessage']) && self::postMail($item)) {
                                continue;
                        }
 
-                       if (($receiver != 0) && ($item['gravity'] == Item::GRAVITY_PARENT) && !in_array($item['post-reason'], [Item::PR_FOLLOWER, Item::PR_TAG, item::PR_TO, Item::PR_CC])) {
-                               if (!($item['isForum'] ?? false)) {
+                       if (($receiver != 0) && ($item['gravity'] == Item::GRAVITY_PARENT) && !in_array($item['post-reason'], [Item::PR_FOLLOWER, Item::PR_TAG, item::PR_TO, Item::PR_CC, Item::PR_AUDIENCE])) {
+                               if (!$item['isGroup']) {
                                        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;
@@ -1080,16 +1126,16 @@ class Processor
                                        }
                                }
 
-                               $is_forum = false;
+                               $isGroup = false;
                                $user = User::getById($receiver, ['account-type']);
                                if (!empty($user['account-type'])) {
-                                       $is_forum = ($user['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
+                                       $isGroup = ($user['account-type'] == User::ACCOUNT_TYPE_COMMUNITY);
                                }
 
                                if ((DI::pConfig()->get($receiver, 'system', 'accept_only_sharer') == Item::COMPLETION_NONE)
-                                       && ((!$is_forum && !($item['isForum'] ?? false) && ($activity['type'] != 'as:Announce'))
+                                       && ((!$isGroup && !$item['isGroup'] && ($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']]);
+                                       Logger::info('Actor is a non sharer, is no group or it is no announce', ['uid' => $receiver, 'actor' => $activity['actor'], 'url' => $item['uri'], 'type' => $activity['type']]);
                                        continue;
                                }
 
@@ -1132,7 +1178,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'] == Item::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)
+                       // We send automatic follow requests for reshared messages. (We don't need though for group posts)
                        if ($author['type'] != 'Group') {
                                Logger::info('Send follow request', ['uri' => $item['uri'], 'stored' => $stored, 'to' => $item['author-link']]);
                                ActivityPub\Transmitter::sendFollowObject($item['uri'], $item['author-link']);
@@ -1268,15 +1314,17 @@ class Processor
 
        public static function storeReceivers(int $uriid, array $receivers)
        {
-               foreach (['as:to' => Tag::TO, 'as:cc' => Tag::CC, 'as:bto' => Tag::BTO, 'as:bcc' => Tag::BCC] as $element => $type) {
+               foreach (['as:to' => Tag::TO, 'as:cc' => Tag::CC, 'as:bto' => Tag::BTO, 'as:bcc' => Tag::BCC, 'as:audience' => Tag::AUDIENCE, 'as:attributedTo' => Tag::ATTRIBUTED] as $element => $type) {
                        if (!empty($receivers[$element])) {
                                foreach ($receivers[$element] as $receiver) {
                                        if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
                                                $name = Receiver::PUBLIC_COLLECTION;
                                        } elseif ($path = parse_url($receiver, PHP_URL_PATH)) {
                                                $name = trim($path, '/');
+                                       } elseif ($host = parse_url($receiver, PHP_URL_HOST)) {
+                                               $name = $host;
                                        } else {
-                                               Logger::warning('Unable to coerce name from receiver', ['receiver' => $receiver]);
+                                               Logger::warning('Unable to coerce name from receiver', ['element' => $element, 'type' => $type, 'receiver' => $receiver]);
                                                $name = '';
                                        }
 
@@ -1291,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 = [];
@@ -1398,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'])) {
@@ -1441,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.
@@ -1467,14 +1519,44 @@ class Processor
         * @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
+        * @param int        $uid         User id that is used to fetch the activity
+        * @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): 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, 0);
-               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 '';
                }
 
@@ -1507,27 +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['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) {
@@ -1536,17 +1619,21 @@ class Processor
 
                Contact::updateByUrlIfNeeded($actor);
 
-               if (!empty($relay_actor)) {
-                       $ldactivity['thread-completion'] = $ldactivity['from-relay'] = Contact::getIdForURL($relay_actor);
-                       $ldactivity['completion-mode']   = Receiver::COMPLETION_RELAY;
-               } elseif (!empty($child['thread-completion'])) {
+               if (!empty($child['thread-completion'])) {
                        $ldactivity['thread-completion'] = $child['thread-completion'];
                        $ldactivity['completion-mode']   = $child['completion-mode'] ?? Receiver::COMPLETION_NONE;
                } else {
-                       $ldactivity['thread-completion'] = Contact::getIdForURL($actor);
+                       $ldactivity['thread-completion'] = Contact::getIdForURL($relay_actor ?: $actor);
                        $ldactivity['completion-mode']   = $completion;
                }
 
+               if ($completion == Receiver::COMPLETION_RELAY) {
+                       $ldactivity['from-relay'] = $ldactivity['thread-completion'];
+                       if (in_array($type, Receiver::CONTENT_TYPES) && !self::acceptIncomingMessage($ldactivity, $object_id)) {
+                               return null;
+                       }
+               }
+
                if (!empty($child['thread-children-type'])) {
                        $ldactivity['thread-children-type'] = $child['thread-children-type'];
                } elseif (!empty($child['type'])) {
@@ -1555,21 +1642,42 @@ class Processor
                        $ldactivity['thread-children-type'] = 'as:Create';
                }
 
-               if (!empty($relay_actor) && !self::acceptIncomingMessage($ldactivity, $object['id'])) {
-                       return '';
-               }
-
                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']]);
+                       Logger::info('Activity has already been queued.', ['url' => $url, 'object' => $activity['id']]);
+               } elseif (ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity), $uid, true, false, $signer, '', $completion)) {
+                       Logger::info('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']]);
+                       Logger::info('Activity had been fetched and will be processed later.', ['url' => $url, 'entry' => $child['entry-id'] ?? 0, 'completion' => $completion, 'object' => $activity['id']]);
                }
 
                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
         *
@@ -1594,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;
        }
 
        /**
@@ -1836,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;
@@ -1858,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']]);
        }
 
        /**