]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Processor.php
Set the owner for group posts
[friendica.git] / src / Protocol / ActivityPub / Processor.php
index e98fa4596c122b3ac21b638a55cad6b4118a9b64..2db9f925a945c34d9f730ba1810c93b69f9f63b9 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);
@@ -412,7 +415,7 @@ 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']) {
@@ -431,7 +434,19 @@ class Processor
                        $item['owner-id'] = $item['author-id'];
                } else {
                        $actor = APContact::getByURL($item['owner-link'], false);
-                       $item['isForum'] = ($actor['type'] ?? 'Person') == 'Group';
+                       $item['isGroup'] = ($actor['type'] ?? 'Person') == 'Group';
+               }
+
+               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('Set owner to audience', ['audience' => $audience, 'actor' => $activity['actor'], 'author' => $activity['author']]);
+                                       $item['isGroup']    = true;
+                                       $item['owner-link'] = $audience;
+                                       $item['owner-id']   = Contact::getIdForURL($audience);
+                               }
+                       }
                }
 
                $item['uri'] = $activity['id'];
@@ -505,7 +520,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 +531,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 +585,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;
@@ -832,6 +852,9 @@ class Processor
                        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,11 +878,6 @@ 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'] ?? '');
@@ -943,7 +961,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;
@@ -1020,6 +1038,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,10 +1071,10 @@ 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'] ?? false) {
+                               $item['contact-id'] = Contact::getIdForURL($item['owner-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'])) {
@@ -1065,8 +1086,8 @@ class Processor
                                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'] ?? 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']]);
                                                continue;
@@ -1080,16 +1101,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'] ?? false) && ($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 +1153,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 +1289,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 = '';
                                        }
 
@@ -1467,13 +1490,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 '';
                }
@@ -1521,6 +1545,7 @@ class Processor
                $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;
@@ -1530,17 +1555,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'])) {
@@ -1549,16 +1584,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'];