]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Logging for an exception when processing activities
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index bb4d61b5af6777ac010655b1a2a8beaffa44ce8e..b8d4005a6131df06c35677e79023ca7377611dbc 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
  *
@@ -44,13 +44,13 @@ use Friendica\Model\User;
 use Friendica\Model\Post;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
+use Friendica\Protocol\Delivery;
 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;
 
 /**
  * ActivityPub Processor Protocol class
@@ -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);
@@ -308,10 +311,10 @@ class Processor
                $item['thr-parent'] = $activity['reply-to-id'];
 
                if ($activity['reply-to-id'] == $activity['id']) {
-                       $item['gravity'] = GRAVITY_PARENT;
+                       $item['gravity'] = Item::GRAVITY_PARENT;
                        $item['object-type'] = Activity\ObjectType::NOTE;
                } else {
-                       $item['gravity'] = GRAVITY_COMMENT;
+                       $item['gravity'] = Item::GRAVITY_COMMENT;
                        $item['object-type'] = Activity\ObjectType::COMMENT;
                }
 
@@ -356,7 +359,7 @@ class Processor
 
                $item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
 
-               if (empty($conversation) && empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
+               if (empty($conversation) && empty($activity['directmessage']) && ($item['gravity'] != Item::GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
                        Logger::notice('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
                        if (!$fetch_parents) {
                                Queue::remove($activity);
@@ -431,7 +434,7 @@ class Processor
                        $item['owner-id'] = $item['author-id'];
                } else {
                        $actor = APContact::getByURL($item['owner-link'], false);
-                       $item['isForum'] = ($actor['type'] == 'Group');
+                       $item['isForum'] = ($actor['type'] ?? 'Person') == 'Group';
                }
 
                $item['uri'] = $activity['id'];
@@ -505,7 +508,7 @@ class Processor
                $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.', ['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']]);
@@ -516,10 +519,10 @@ class Processor
                        } 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]);
+                                       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::notice('The activity has been fetched and will hopefully be created later.', ['parent' => $result]);
+                                       Logger::info('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'])) {
@@ -570,7 +573,12 @@ class Processor
         */
        public static function isActivityGone(string $url): bool
        {
-               $curlResult = HTTPSignature::fetchRaw($url, 0);
+               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;
@@ -663,7 +671,7 @@ class Processor
 
                $item['verb'] = $verb;
                $item['thr-parent'] = $activity['object_id'];
-               $item['gravity'] = GRAVITY_ACTIVITY;
+               $item['gravity'] = Item::GRAVITY_ACTIVITY;
                unset($item['post-type']);
                $item['object-type'] = Activity\ObjectType::NOTE;
 
@@ -804,7 +812,7 @@ class Processor
        private static function processContent(array $activity, array $item)
        {
                if (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/markdown')) {
-                       $item['title'] = strip_tags($activity['name']);
+                       $item['title'] = strip_tags($activity['name'] ?? '');
                        $content = Markdown::toBBCode($activity['content']);
                } elseif (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/bbcode')) {
                        $item['title'] = $activity['name'];
@@ -827,31 +835,41 @@ class Processor
 
                $content = self::addMentionLinks($content, $activity['tags']);
 
+               if (!empty($activity['quote-url'])) {
+                       $id = Item::fetchByLink($activity['quote-url']);
+                       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']]);
+                       }
+               }
+
                if (!empty($activity['source'])) {
                        $item['body'] = $activity['source'];
                        $item['raw-body'] = $content;
-                       $item['body'] = Item::improveSharedDataInBody($item);
+
+                       $quote_uri_id = Item::getQuoteUriId($item['body']);
+                       if (empty($item['quote-uri-id']) && !empty($quote_uri_id)) {
+                               $item['quote-uri-id'] = $quote_uri_id;
+                       }
+
+                       $item['body'] = BBCode::removeSharedData($item['body']);
                } else {
                        $parent_uri = $item['parent-uri'] ?? $item['thr-parent'];
-                       if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
+                       if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == Item::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' => $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($activity['quote-url'])) {
-                               $item['body'] .= DI::contentItem()->createSharedPostByUrl($activity['quote-url']);
-                       }
                }
 
                self::storeFromBody($item);
@@ -931,13 +949,13 @@ 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;
                }
 
-               if ($item['gravity'] != GRAVITY_PARENT) {
+               if ($item['gravity'] != Item::GRAVITY_PARENT) {
                        // We cannot reliably check at this point if a comment or activity belongs to an accepted post or needs to be fetched
                        // This can possibly be improved in the future.
                        Logger::debug('Message is no parent - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]);
@@ -945,7 +963,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)) {
+               if (Relay::isSolicitedPost($tags, $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 {
@@ -1008,6 +1026,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;
@@ -1035,7 +1056,7 @@ class Processor
                                // When a post arrives via a relay and we follow the author, we have to override the causer.
                                // Otherwise the system assumes that we follow the relay. (See "addRowInformation")
                                Logger::debug('Relay post for follower', ['receiver' => $receiver, 'guid' => $item['guid'], 'relay' => $activity['from-relay']]);
-                               $item['causer-id'] = ($item['gravity'] == GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
+                               $item['causer-id'] = ($item['gravity'] == Item::GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
                        }
 
                        if ($item['isForum'] ?? false) {
@@ -1053,7 +1074,7 @@ class Processor
                                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 (($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['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']]);
@@ -1088,7 +1109,7 @@ class Processor
                                continue;
                        }
 
-                       if (($item['gravity'] != GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) {
+                       if (($item['gravity'] != Item::GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) {
                                $event_id = self::createEvent($activity, $item);
 
                                $item = Event::getItemArrayForImportedId($event_id, $item);
@@ -1118,7 +1139,7 @@ class Processor
                }
 
                // Store send a follow request for every reshare - but only when the item had been stored
-               if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && !empty($item['author-link']) && ($item['author-link'] != $item['owner-link'])) {
+               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)
                        if ($author['type'] != 'Group') {
@@ -1138,7 +1159,7 @@ class Processor
         */
        private static function hasParents(array $item, int $receiver)
        {
-               if (($receiver == 0) || ($item['gravity'] == GRAVITY_PARENT)) {
+               if (($receiver == 0) || ($item['gravity'] == Item::GRAVITY_PARENT)) {
                        return true;
                }
 
@@ -1149,7 +1170,7 @@ class Processor
                if ($item['verb'] != Activity::ANNOUNCE) {
                        switch (DI::pConfig()->get($receiver, 'system', 'accept_only_sharer')) {
                                case Item::COMPLETION_COMMENT:
-                                       $add_parent = ($item['gravity'] != GRAVITY_ACTIVITY);
+                                       $add_parent = ($item['gravity'] != Item::GRAVITY_ACTIVITY);
                                        break;
 
                                case Item::COMPLETION_NONE:
@@ -1173,7 +1194,7 @@ class Processor
                if (!empty($item['parent-uri-id'])) {
                        if (Post::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $receiver])) {
                                $has_parents = true;
-                       } elseif ($add_parent && Post::exists(['uri-id' => $item['parent-uri'], 'uid' => 0])) {
+                       } elseif ($add_parent && Post::exists(['uri-id' => $item['parent-uri-id'], 'uid' => 0])) {
                                $stored = Item::storeForUserByUriId($item['parent-uri-id'], $receiver, $fields);
                                $has_parents = (bool)$stored;
                                if ($stored) {
@@ -1256,13 +1277,18 @@ 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 {
-                                               $name = trim(parse_url($receiver, PHP_URL_PATH), '/');
+                                               Logger::warning('Unable to coerce name from receiver', ['element' => $element, 'type' => $type, 'receiver' => $receiver]);
+                                               $name = '';
                                        }
 
                                        $target = Tag::getTargetType($receiver);
@@ -1283,7 +1309,7 @@ class Processor
         */
        private static function postMail(array $activity, array $item)
        {
-               if (($item['gravity'] != GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) {
+               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;
                }
@@ -1452,13 +1478,14 @@ 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_*
+        * @param int        $uid         User id that is used to fetch the activity
         * @return string fetched message URL
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL): string
+       public static function fetchMissingActivity(string $url, array $child = [], string $relay_actor = '', int $completion = Receiver::COMPLETION_MANUAL, int $uid = 0): string
        {
-               $object = self::fetchCachedActivity($url, 0);
+               $object = self::fetchCachedActivity($url, $uid);
                if (empty($object)) {
                        return '';
                }
@@ -1515,17 +1542,27 @@ class Processor
 
                $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;
-               } elseif (!empty($child['thread-completion'])) {
+               if ($object_actor != $actor) {
+                       Contact::updateByUrlIfNeeded($object_actor);
+               }
+
+               Contact::updateByUrlIfNeeded($actor);
+
+               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 (!self::acceptIncomingMessage($ldactivity, $object['id'])) {
+                               return '';
+                       }
+               }
+
                if (!empty($child['thread-children-type'])) {
                        $ldactivity['thread-children-type'] = $child['thread-children-type'];
                } elseif (!empty($child['type'])) {
@@ -1534,16 +1571,12 @@ 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'];
@@ -1586,7 +1619,7 @@ class Processor
                        }
                }
 
-               return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB);
+               return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0);
        }
 
        /**
@@ -1700,11 +1733,13 @@ class Processor
        {
                if (empty($activity['object_id']) || empty($activity['actor'])) {
                        Logger::info('Empty object id or actor.');
+                       Queue::remove($activity);
                        return;
                }
 
                if ($activity['object_id'] != $activity['actor']) {
                        Logger::info('Object id does not match actor.');
+                       Queue::remove($activity);
                        return;
                }
 
@@ -1718,6 +1753,42 @@ class Processor
                Queue::remove($activity);
        }
 
+       /**
+        * Add moved contacts as followers for all subscribers of the old contact
+        *
+        * @param array $activity
+        * @return void
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public static function movePerson(array $activity)
+       {
+               if (empty($activity['target_id']) || empty($activity['object_id'])) {
+                       Queue::remove($activity);
+                       return;
+               }
+
+               if ($activity['object_id'] != $activity['actor']) {
+                       Logger::notice('Object is not the actor', ['activity' => $activity]);
+                       Queue::remove($activity);
+                       return;
+               }
+
+               $from = Contact::getByURL($activity['object_id'], false, ['uri-id']);
+               if (empty($from['uri-id'])) {
+                       Logger::info('Object not found', ['activity' => $activity]);
+                       Queue::remove($activity);
+                       return;
+               }
+
+               $contacts = DBA::select('contact', ['uid', 'url'], ["`uri-id` = ? AND `uid` != ? AND `rel` IN (?, ?)", $from['uri-id'], 0, Contact::FRIEND, Contact::SHARING]);
+               while ($from_contact = DBA::fetch($contacts)) {
+                       $result = Contact::createFromProbeForUser($from_contact['uid'], $activity['target_id']);
+                       Logger::debug('Follower added', ['from' => $from_contact, 'result' => $result]);
+               }
+               DBA::close($contacts);
+               Queue::remove($activity);
+       }
+
        /**
         * Blocks the user by the contact
         *
@@ -1768,6 +1839,43 @@ class Processor
                Queue::remove($activity);
        }
 
+       /**
+        * Report a user
+        *
+        * @param array $activity
+        * @return void
+        * @throws \Exception
+        */
+       public static function ReportAccount(array $activity)
+       {
+               $account_id = Contact::getIdForURL($activity['object_id']);
+               if (empty($account_id)) {
+                       Logger::info('Unknown account', ['activity' => $activity]);
+                       Queue::remove($activity);
+                       return;
+               }
+
+               $reporter_id = Contact::getIdForURL($activity['actor']);
+               if (empty($reporter_id)) {
+                       Logger::info('Unknown actor', ['activity' => $activity]);
+                       Queue::remove($activity);
+                       return;
+               }
+
+               $uri_ids = [];
+               foreach ($activity['object_ids'] as $status_id) {
+                       $post = Post::selectFirst(['uri-id'], ['uri' => $status_id]);
+                       if (!empty($post['uri-id'])) {
+                               $uri_ids[] = $post['uri-id'];
+                       }
+               }
+
+               $report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, '', 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']]);
+       }
+
        /**
         * Accept a follow request
         *
@@ -1777,17 +1885,38 @@ class Processor
         */
        public static function acceptFollowUser(array $activity)
        {
-               $uid = User::getIdForURL($activity['object_actor']);
+               if (!empty($activity['object_actor'])) {
+                       $uid      = User::getIdForURL($activity['object_actor']);
+                       $check_id = false;
+               } elseif (!empty($activity['receiver']) && (count($activity['receiver']) == 1)) {
+                       $uid      = array_shift($activity['receiver']);
+                       $check_id = true;
+               }
+
                if (empty($uid)) {
+                       Logger::notice('User could not be detected', ['activity' => $activity]);
+                       Queue::remove($activity);
                        return;
                }
 
                $cid = Contact::getIdForURL($activity['actor'], $uid);
                if (empty($cid)) {
-                       Logger::info('No contact found', ['actor' => $activity['actor']]);
+                       Logger::notice('No contact found', ['actor' => $activity['actor']]);
+                       Queue::remove($activity);
                        return;
                }
 
+               $id = Transmitter::activityIDFromContact($cid);
+               if ($id == $activity['object_id']) {
+                       Logger::info('Successful id check', ['uid' => $uid, 'cid' => $cid]);
+               } else {
+                       Logger::info('Unsuccessful id check', ['uid' => $uid, 'cid' => $cid, 'id' => $id, 'object_id' => $activity['object_id']]);
+                       if ($check_id) {
+                               Queue::remove($activity);
+                               return;
+                       }
+               }
+
                self::switchContact($cid);
 
                $fields = ['pending' => false];
@@ -1859,7 +1988,7 @@ class Processor
                        return;
                }
 
-               Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]);
+               Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => Item::GRAVITY_ACTIVITY]);
                Queue::remove($activity);
        }