]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Merge remote-tracking branch 'upstream/develop' into diaspora-item
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index 79626abbb8efcab4e372b70a30e2dc956f605374..d57842c2b226362eae4c16cccc9fa3e4eaf842c2 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
@@ -431,7 +431,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'];
@@ -804,7 +804,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,10 +827,26 @@ 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'];
+                       } 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'] == Item::GRAVITY_COMMENT)) {
@@ -848,10 +864,6 @@ class Processor
                        }
                        $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);
@@ -945,7 +957,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 {
@@ -1173,7 +1185,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) {
@@ -1261,8 +1273,11 @@ class Processor
                                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, '/');
                                        } else {
-                                               $name = trim(parse_url($receiver, PHP_URL_PATH), '/');
+                                               Logger::warning('Unable to coerce name from receiver', ['receiver' => $receiver]);
+                                               $name = '';
                                        }
 
                                        $target = Tag::getTargetType($receiver);
@@ -1515,6 +1530,12 @@ class Processor
 
                $ldactivity['recursion-depth'] = !empty($child['recursion-depth']) ? $child['recursion-depth'] + 1 : 0;
 
+               if ($object_actor != $actor) {
+                       Contact::updateByUrlIfNeeded($object_actor);
+               }
+
+               Contact::updateByUrlIfNeeded($actor);
+
                if (!empty($relay_actor)) {
                        $ldactivity['thread-completion'] = $ldactivity['from-relay'] = Contact::getIdForURL($relay_actor);
                        $ldactivity['completion-mode']   = Receiver::COMPLETION_RELAY;
@@ -1586,7 +1607,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 +1721,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 +1741,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 +1827,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 +1873,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];