]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Receiver.php
Merge pull request #9590 from MrPetovan/bug/fatal-errors
[friendica.git] / src / Protocol / ActivityPub / Receiver.php
index 3c6dd28e5632360e833f6f501a52cd4adc92cd0a..af6eb56ceef326e991540aab74a3d1dc41d3fb57 100644 (file)
@@ -99,9 +99,13 @@ class Receiver
                $actor = JsonLD::fetchElement($ldactivity, 'as:actor', '@id');
 
                $apcontact = APContact::getByURL($actor);
-               if (!empty($apcontact) && ($apcontact['type'] == 'Application') && ($apcontact['nick'] == 'relay')) {
+               if (empty($apcontact)) {
+                       Logger::notice('Unable to retrieve AP contact for actor', ['actor' => $actor]);
+               } elseif ($apcontact['type'] == 'Application' && $apcontact['nick'] == 'relay') {
                        self::processRelayPost($ldactivity, $actor);
                        return;
+               } else {
+                       APContact::unmarkForArchival($apcontact);
                }
 
                $http_signer = HTTPSignature::getSigner($body, $header);
@@ -173,6 +177,17 @@ class Receiver
                        return;
                }
 
+               $contact = Contact::getByURL($actor);
+               if (empty($contact)) {
+                       Logger::info('Relay contact not found', ['actor' => $actor]);
+                       return;
+               }
+
+               if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
+                       Logger::notice('Relay is no sharer', ['actor' => $actor]);
+                       return;
+               }
+
                Logger::info('Got relayed message id', ['id' => $object_id]);
 
                $item_id = Item::searchByLink($object_id);
@@ -181,7 +196,11 @@ class Receiver
                        return;
                }
 
-               Processor::fetchMissingActivity($object_id, [], $actor);
+               $id = Processor::fetchMissingActivity($object_id, [], $actor);
+               if (empty($id)) {
+                       Logger::notice('Relayed message had not been fetched', ['id' => $object_id]);
+                       return;
+               }
 
                $item_id = Item::searchByLink($object_id);
                if ($item_id) {
@@ -218,6 +237,7 @@ class Receiver
 
                $profile = APContact::getByURL($object_id);
                if (!empty($profile['type'])) {
+                       APContact::unmarkForArchival($profile);
                        return 'as:' . $profile['type'];
                }
 
@@ -278,14 +298,14 @@ class Receiver
                $receivers = $reception_types = [];
                foreach ($receiverdata as $key => $data) {
                        $receivers[$key] = $data['uid'];
-                       $reception_types[$data['uid']] = $data['type'] ?? 0;
+                       $reception_types[$data['uid']] = $data['type'] ?? self::TARGET_UNKNOWN;
                }
 
                // When it is a delivery to a personal inbox we add that user to the receivers
                if (!empty($uid)) {
-                       $additional = ['uid:' . $uid => $uid];
-                       $receivers = array_merge($receivers, $additional);
-                       if (empty($reception_types[$uid]) || in_array($reception_types[$uid], [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL])) {
+                       $additional = [$uid => $uid];
+                       $receivers = array_replace($receivers, $additional);
+                       if (empty($activity['thread-completion']) && (empty($reception_types[$uid]) || in_array($reception_types[$uid], [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL]))) {
                                $reception_types[$uid] = self::TARGET_BCC;
                        }
                } else {
@@ -368,8 +388,8 @@ class Receiver
                $object_data['type'] = $type;
                $object_data['actor'] = $actor;
                $object_data['item_receiver'] = $receivers;
-               $object_data['receiver'] = array_merge($object_data['receiver'] ?? [], $receivers);
-               $object_data['reception_type'] = array_merge($object_data['reception_type'] ?? [], $reception_types);
+               $object_data['receiver'] = array_replace($object_data['receiver'] ?? [], $receivers);
+               $object_data['reception_type'] = array_replace($object_data['reception_type'] ?? [], $reception_types);
 
                $author = $object_data['author'] ?? $actor;
                if (!empty($author) && !empty($object_data['id'])) {
@@ -490,7 +510,7 @@ class Receiver
 
                        case 'as:Announce':
                                if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
-                                       $object_data['thread-completion'] = true;
+                                       $object_data['thread-completion'] = Contact::getIdForURL($actor);
 
                                        $item = ActivityPub\Processor::createItem($object_data);
                                        if (empty($item)) {
@@ -629,7 +649,7 @@ class Receiver
                if (!empty($reply)) {
                        $parents = Item::select(['uid'], ['uri' => $reply]);
                        while ($parent = Item::fetch($parents)) {
-                               $receivers['uid:' . $parent['uid']] = ['uid' => $parent['uid'], 'type' => self::TARGET_ANSWER];
+                               $receivers[$parent['uid']] = ['uid' => $parent['uid'], 'type' => self::TARGET_ANSWER];
                        }
                }
 
@@ -643,6 +663,9 @@ class Receiver
                        $followers = '';
                }
 
+               // We have to prevent false follower assumptions upon thread completions
+               $follower_target = empty($activity['thread-completion']) ? self::TARGET_FOLLOWER : self::TARGET_UNKNOWN;
+
                foreach (['as:to', 'as:cc', 'as:bto', 'as:bcc'] as $element) {
                        $receiver_list = JsonLD::fetchElementArray($activity, $element, '@id');
                        if (empty($receiver_list)) {
@@ -651,17 +674,17 @@ class Receiver
 
                        foreach ($receiver_list as $receiver) {
                                if ($receiver == self::PUBLIC_COLLECTION) {
-                                       $receivers['uid:0'] = ['uid' => 0, 'type' => self::TARGET_GLOBAL];
+                                       $receivers[0] = ['uid' => 0, 'type' => self::TARGET_GLOBAL];
                                }
 
                                // Add receiver "-1" for unlisted posts 
                                if ($fetch_unlisted && ($receiver == self::PUBLIC_COLLECTION) && ($element == 'as:cc')) {
-                                       $receivers['uid:-1'] = ['uid' => -1, 'type' => self::TARGET_GLOBAL];
+                                       $receivers[-1] = ['uid' => -1, 'type' => self::TARGET_GLOBAL];
                                }
 
                                // Fetch the receivers for the public and the followers collection
                                if (in_array($receiver, [$followers, self::PUBLIC_COLLECTION]) && !empty($actor)) {
-                                       $receivers = self::getReceiverForActor($actor, $tags, $receivers);
+                                       $receivers = self::getReceiverForActor($actor, $tags, $receivers, $follower_target);
                                        continue;
                                }
 
@@ -689,7 +712,7 @@ class Receiver
                                        }
                                }
 
-                               $type = $receivers['uid:' . $contact['uid']]['type'] ?? self::TARGET_UNKNOWN;
+                               $type = $receivers[$contact['uid']]['type'] ?? self::TARGET_UNKNOWN;
                                if (in_array($type, [self::TARGET_UNKNOWN, self::TARGET_FOLLOWER, self::TARGET_ANSWER, self::TARGET_GLOBAL])) {
                                        switch ($element) {
                                                case 'as:to':
@@ -706,7 +729,7 @@ class Receiver
                                                        break;
                                        }
 
-                                       $receivers['uid:' . $contact['uid']] = ['uid' => $contact['uid'], 'type' => $type];
+                                       $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $type];
                                }
                        }
                }
@@ -719,32 +742,34 @@ class Receiver
        /**
         * Fetch the receiver list of a given actor
         *
-        * @param string $actor
-        * @param array  $tags
+        * @param string  $actor
+        * @param array   $tags
+        * @param array   $receivers
+        * @param integer $target_type
         *
         * @return array with receivers (user id)
         * @throws \Exception
         */
-       private static function getReceiverForActor($actor, $tags, $receivers)
+       private static function getReceiverForActor($actor, $tags, $receivers, $target_type)
        {
                $basecondition = ['rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
                        'network' => Protocol::FEDERATED, 'archive' => false, 'pending' => false];
 
-               $condition = DBA::mergeConditions($basecondition, ['nurl' => Strings::normaliseLink($actor)]);
+               $condition = DBA::mergeConditions($basecondition, ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($actor), 0]);
                $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
                while ($contact = DBA::fetch($contacts)) {
-                       if (empty($receivers['uid:' . $contact['uid']]) && self::isValidReceiverForActor($contact, $actor, $tags)) {
-                               $receivers['uid:' . $contact['uid']] = ['uid' => $contact['uid'], 'type' => self::TARGET_FOLLOWER];
+                       if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
+                               $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type];
                        }
                }
                DBA::close($contacts);
 
                // The queries are split because of performance issues
-               $condition = DBA::mergeConditions($basecondition, ["`alias` IN (?, ?)", Strings::normaliseLink($actor), $actor]);
+               $condition = DBA::mergeConditions($basecondition, ["`alias` IN (?, ?) AND `uid` != ?", Strings::normaliseLink($actor), $actor, 0]);
                $contacts = DBA::select('contact', ['uid', 'rel'], $condition);
                while ($contact = DBA::fetch($contacts)) {
-                       if (empty($receivers['uid:' . $contact['uid']]) && self::isValidReceiverForActor($contact, $actor, $tags)) {
-                               $receivers['uid:' . $contact['uid']] = ['uid' => $contact['uid'], 'type' => self::TARGET_FOLLOWER];
+                       if (empty($receivers[$contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
+                               $receivers[$contact['uid']] = ['uid' => $contact['uid'], 'type' => $target_type];
                        }
                }
                DBA::close($contacts);
@@ -761,13 +786,8 @@ class Receiver
         * @return bool with receivers (user id)
         * @throws \Exception
         */
-       private static function isValidReceiverForActor($contact, $actor, $tags)
+       private static function isValidReceiverForActor($contact, $tags)
        {
-               // Public contacts are no valid receiver
-               if ($contact['uid'] == 0) {
-                       return false;
-               }
-
                // Are we following the contact? Then this is a valid receiver
                if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
                        return true;
@@ -785,7 +805,7 @@ class Receiver
                                continue;
                        }
 
-                       if ($tag['href'] == $owner['url']) {
+                       if (Strings::compareLink($tag['href'], $owner['url'])) {
                                return true;
                        }
                }
@@ -964,7 +984,7 @@ class Receiver
         *
         * @return array with tags in a simplified format
         */
-       private static function processTags(array $tags)
+       public static function processTags(array $tags)
        {
                $taglist = [];
 
@@ -1216,24 +1236,36 @@ class Receiver
                        $filetype = strtolower(substr($mediatype, 0, strpos($mediatype, '/')));
 
                        if ($filetype == 'audio') {
-                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href];
+                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href, 'height' => null, 'size' => null];
                        } elseif ($filetype == 'video') {
                                $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
+                               $size = (int)JsonLD::fetchElement($url, 'pt:size', '@value');
 
-                               // We save bandwidth by using a moderate height
+                               // We save bandwidth by using a moderate height (alt least 480 pixel height)
                                // Peertube normally uses these heights: 240, 360, 480, 720, 1080
                                if (!empty($attachments[$filetype]['height']) &&
-                                       (($height > 480) || $height < $attachments[$filetype]['height'])) {
+                                       ($height > $attachments[$filetype]['height']) && ($attachments[$filetype]['height'] >= 480)) {
+                                       continue;
+                               }
+
+                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href, 'height' => $height, 'size' => $size];
+                       } elseif (in_array($mediatype, ['application/x-bittorrent', 'application/x-bittorrent;x-scheme-handler/magnet'])) {
+                               $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
+
+                               // For Torrent links we always store the highest resolution
+                               if (!empty($attachments[$mediatype]['height']) && ($height < $attachments[$mediatype]['height'])) {
                                        continue;
                                }
 
-                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href, 'height' => $height];
+                               $attachments[$mediatype] = ['type' => $mediatype, 'url' => $href, 'height' => $height, 'size' => null];
                        }
                }
 
                foreach ($attachments as $type => $attachment) {
                        $object_data['attachments'][] = ['type' => $type,
                                'mediaType' => $attachment['type'],
+                               'height' => $attachment['height'],
+                               'size' => $attachment['size'],
                                'name' => '',
                                'url' => $attachment['url']];
                }
@@ -1318,7 +1350,7 @@ class Receiver
                $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
                $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment') ?? []);
                $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag') ?? []);
-               $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji') ?? []);
+               $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', null, '@type', 'toot:Emoji') ?? []);
                $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
                $object_data['generator'] = JsonLD::fetchElement($object_data, 'generator', '@value');
                $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url', '@id');
@@ -1347,7 +1379,8 @@ class Receiver
                $object_data['reception_type'] = $reception_types;
 
                $object_data['unlisted'] = in_array(-1, $object_data['receiver']);
-               unset($object_data['receiver']['uid:-1']);
+               unset($object_data['receiver'][-1]);
+               unset($object_data['reception_type'][-1]);
 
                // Common object data: