From 05bd0d0b671ad509465fa6cddabc3c2a07c796a7 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 20 Jul 2020 00:26:42 -0400 Subject: [PATCH 1/2] Add support for multiple Link as urls of Images in ActivityPub\Receiver - Address https://github.com/friendica/friendica/issues/8676#issuecomment-650554955 --- src/Protocol/ActivityPub/Receiver.php | 58 ++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 7a0a9c1f78..aa59b9eb9e 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -877,7 +877,7 @@ class Receiver * * @param array $attachments Attachments in JSON-LD format * - * @return array with attachmants in a simplified format + * @return array Attachments in a simplified format */ private static function processAttachments(array $attachments) { @@ -925,6 +925,62 @@ class Receiver 'url' => JsonLD::fetchElement($attachment, 'as:href', '@id') ]; break; + case 'as:Image': + $mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value'); + $imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id'); + $imagePreviewUrl = null; + // Multiple URLs? + if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) { + $imageVariants = []; + $previewVariants = []; + foreach ($urls as $url) { + // Scalar URL, no discrimination possible + if (is_string($url)) { + $imageFullUrl = $url; + continue; + } + + // Not sure what to do with a different Link media type than the base Image, we skip + if ($mediaType != JsonLD::fetchElement($url, 'as:mediaType', '@value')) { + continue; + } + + $href = JsonLD::fetchElement($url, 'as:href', '@id'); + + // Default URL choice if no discriminating width is provided + $imageFullUrl = $href ?? $imageFullUrl; + + $width = intval(JsonLD::fetchElement($url, 'as:width', '@value') ?? 1); + + if ($href && $width) { + $imageVariants[$width] = $href; + // 632 is the ideal width for full screen frio posts, we compute the absolute distance to it + $previewVariants[abs(632 - $width)] = $href; + } + } + + if ($imageVariants) { + // Taking the maximum size image + ksort($imageVariants); + $imageFullUrl = array_pop($imageVariants); + + // Taking the minimum number distance to the target distance + ksort($previewVariants); + $imagePreviewUrl = array_shift($previewVariants); + } + + unset($imageVariants); + unset($previewVariants); + } + + $attachlist[] = [ + 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')), + 'mediaType' => $mediaType, + 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'), + 'url' => $imageFullUrl, + 'image' => $imagePreviewUrl !== $imageFullUrl ? $imagePreviewUrl : null, + ]; + break; default: $attachlist[] = [ 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')), From 3894976a2d7dd42784488d3ff3ddb7ff53c25bc1 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 20 Jul 2020 00:27:36 -0400 Subject: [PATCH 2/2] Add support for image preview in attachments in ActivityPub\Processor - Address https://github.com/friendica/friendica/issues/8676#issuecomment-650554955 --- src/Protocol/ActivityPub/Processor.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index e4cef17045..177813e32d 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -115,10 +115,22 @@ class Processor continue 2; } + $item['body'] .= "\n"; + + // image is the preview/thumbnail URL + if (!empty($attach['image'])) { + $item['body'] .= '[url=' . $attach['url'] . ']'; + $attach['url'] = $attach['image']; + } + if (empty($attach['name'])) { - $item['body'] .= "\n[img]" . $attach['url'] . '[/img]'; + $item['body'] .= '[img]' . $attach['url'] . '[/img]'; } else { - $item['body'] .= "\n[img=" . $attach['url'] . ']' . $attach['name'] . '[/img]'; + $item['body'] .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]'; + } + + if (!empty($attach['image'])) { + $item['body'] .= '[/url]'; } } elseif ($filetype == 'audio') { if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {