}
/**
- * 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
+ * Extracts a potential alternate URL from a list of additional URL elements
*
- * @param array $object The raw object
- * @param array $object_data The parsed object data for later processing
- * @return array the object data
+ * @param array $urls
+ * @return string
*/
- 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;
- }
+ private static function extractAlternateUrl(array $urls): string
+ {
+ $alternateUrl = '';
+ foreach ($urls as $key => $url) {
+ // Not a list but a single URL element
+ if (!is_numeric($key)) {
+ continue;
+ }
- $urls = $object['as:url'];
- $keys = array_keys($urls);
- if (!is_numeric(array_pop($keys))) {
- return $object_data;
+ 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') {
+ $alternateUrl = $href;
+ }
}
+ return $alternateUrl;
+ }
+
+ /**
+ * 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 $urls The object URL list
+ * @return array an array of attachments
+ */
+ private static function processAttachmentUrls(array $urls): array
+ {
$attachments = [];
+ foreach ($urls as $key => $url) {
+ // Not a list but a single URL element
+ if (!is_numeric($key)) {
+ continue;
+ }
- foreach ($urls as $url) {
if (empty($url['@type']) || ($url['@type'] != 'as:Link')) {
continue;
}
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, 'height' => null, 'size' => null];
} elseif ($filetype == 'video') {
$height = (int)JsonLD::fetchElement($url, 'as:height', '@value');
+ // PeerTube audio-only track
+ if ($height === 0) {
+ continue;
+ }
+
$size = (int)JsonLD::fetchElement($url, 'pt:size', '@value');
+ // For embedded video we take the smallest available size
+ if (!empty($attachments[$mediatype]['size']) && ($size > $attachments[$mediatype]['size'])) {
+ 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');
}
$attachments[$mediatype] = ['type' => $mediatype, 'url' => $href, 'height' => $height, 'size' => null];
+ } elseif ($mediatype == 'application/x-mpegURL') {
+ // PeerTube exception, actual video link is in the tags of this URL element
+ $attachments = array_merge($attachments, self::processAttachmentUrls($url['as:tag']));
}
}
- foreach ($attachments as $type => $attachment) {
- $object_data['attachments'][] = ['type' => $type,
- 'mediaType' => $attachment['type'],
- 'height' => $attachment['height'],
- 'size' => $attachment['size'],
- 'name' => '',
- 'url' => $attachment['url']];
- }
- return $object_data;
+ return $attachments;
}
/**
}
if (in_array($object_data['object_type'], ['as:Audio', 'as:Video'])) {
- $object_data = self::processAttachmentUrls($object, $object_data);
+ $object_data['alternate-url'] = self::extractAlternateUrl($object['as:url'] ?? []) ?: $object_data['alternate-url'];
+
+ $attachments = self::processAttachmentUrls($object['as:url'] ?? []);
+ foreach ($attachments as $type => $attachment) {
+ $object_data['attachments'][] = [
+ 'type' => $type,
+ 'mediaType' => $attachment['type'],
+ 'height' => $attachment['height'],
+ 'size' => $attachment['size'],
+ 'name' => '',
+ 'url' => $attachment['url']
+ ];
+ }
}
$receiverdata = self::getReceivers($object, $object_data['actor'], $object_data['tags'], true);