]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Receiver.php
Merge pull request #8447 from annando/issue-7771-funkwhale
[friendica.git] / src / Protocol / ActivityPub / Receiver.php
index 21aba50944f7448bcc9b9411bf86e150526582e2..30728ff11afedadaff91508bde266bd193045305 100644 (file)
@@ -55,7 +55,7 @@ class Receiver
 {
        const PUBLIC_COLLECTION = 'as:Public';
        const ACCOUNT_TYPES = ['as:Person', 'as:Organization', 'as:Service', 'as:Group', 'as:Application'];
-       const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event'];
+       const CONTENT_TYPES = ['as:Note', 'as:Article', 'as:Video', 'as:Image', 'as:Event', 'as:Audio'];
        const ACTIVITY_TYPES = ['as:Like', 'as:Dislike', 'as:Accept', 'as:Reject', 'as:TentativeAccept'];
 
        /**
@@ -936,6 +936,75 @@ class Receiver
                return $object_data;
        }
 
+       /**
+        * Check if the "as:url" element is an array with multiple links
+        * This is the case with audio and video posts.
+        * Then the links are added as attachments
+        *
+        * @param array $object      The raw object
+        * @param array $object_data The parsed object data for later processing
+        * @return array the object data
+        */
+       private static function processAttachmentUrls(array $object, array $object_data) {
+               // Check if this is some url with multiple links
+               if (empty($object['as:url'])) {
+                       return $object_data;
+               }
+               
+               $urls = $object['as:url'];
+               $keys = array_keys($urls);
+               if (!is_numeric(array_pop($keys))) {
+                       return $object_data;
+               }
+
+               $attachments = [];
+
+               foreach ($urls as $url) {
+                       if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
+                               continue;
+                       }
+
+                       $href = JsonLD::fetchElement($url, 'as:href', '@id');
+                       if (empty($href)) {
+                               continue;
+                       }
+
+                       $mediatype = JsonLD::fetchElement($url, 'as:mediaType');
+                       if (empty($mediatype)) {
+                               continue;
+                       }
+
+                       if ($mediatype == 'text/html') {
+                               $object_data['alternate-url'] = $href;
+                       }
+
+                       $filetype = strtolower(substr($mediatype, 0, strpos($mediatype, '/')));
+
+                       if ($filetype == 'audio') {
+                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href];
+                       } elseif ($filetype == 'video') {
+                               $height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
+
+                               // We save bandwidth by using a moderate height
+                               // Peertube normally uses these heights: 240, 360, 480, 720, 1080
+                               if (!empty($attachments[$filetype]['height']) &&
+                                       (($height > 480) || $height < $attachments[$filetype]['height'])) {
+                                       continue;
+                               }
+
+                               $attachments[$filetype] = ['type' => $mediatype, 'url' => $href, 'height' => $height];
+                       }
+               }
+
+               foreach ($attachments as $type => $attachment) {
+                       $object_data['attachments'][] = ['type' => $type,
+                               'mediaType' => $attachment['type'],
+                               'name' => '',
+                               'url' => $attachment['url']];
+               }
+               return $object_data;
+       }
+
        /**
         * Fetches data from the object part of an activity
         *
@@ -983,6 +1052,7 @@ class Receiver
                        $actor = JsonLD::fetchElement($object, 'as:actor', '@id');
                }
 
+               $object_data['sc:identifier'] = JsonLD::fetchElement($object, 'sc:identifier', '@value');
                $object_data['diaspora:guid'] = JsonLD::fetchElement($object, 'diaspora:guid', '@value');
                $object_data['diaspora:comment'] = JsonLD::fetchElement($object, 'diaspora:comment', '@value');
                $object_data['diaspora:like'] = JsonLD::fetchElement($object, 'diaspora:like', '@value');
@@ -1018,6 +1088,10 @@ class Receiver
                        }
                }
 
+               if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
+                       $object_data = self::processAttachmentUrls($object, $object_data);
+               }
+
                $object_data['receiver'] = self::getReceivers($object, $object_data['actor'], $object_data['tags'], true);
                $object_data['unlisted'] = in_array(-1, $object_data['receiver']);
                unset($object_data['receiver']['uid:-1']);