From d86045058e7f507de9f9583ac03bdbb196d86695 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 7 Jan 2023 15:11:34 +0000 Subject: [PATCH 1/2] Diaspora: Photos are now transmitted separately --- src/Module/Photo.php | 12 ++++++++ src/Protocol/ActivityPub/Transmitter.php | 1 + src/Protocol/Diaspora.php | 37 +++++++++++++++++++++++- static/routes.config.php | 1 + 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Module/Photo.php b/src/Module/Photo.php index 3f18058de..687ba98c7 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -137,6 +137,18 @@ class Photo extends BaseModule $scale = intval(substr($photoid, -1, 1)); $photoid = substr($photoid, 0, -2); } + + if (!empty($this->parameters['size'])) { + switch ($this->parameters['size']) { + case 'thumb_small': + $scale = 2; + break; + case 'scaled_full': + $scale = 1; + break; + } + } + $photo = MPhoto::getPhoto($photoid, $scale); if ($photo === false) { throw new HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid)); diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 418a727e2..6e1180bca 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1707,6 +1707,7 @@ class Transmitter $language = self::getLanguage($item); if (!empty($language)) { $richbody = BBCode::setMentionsToNicknames($item['body'] ?? ''); + $richbody = Post\Media::removeFromEndOfBody($richbody); if (!empty($item['quote-uri-id'])) { if ($real_quote) { $richbody = DI::contentItem()->addShareLink($richbody, $item['quote-uri-id']); diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index bf6bfdd63..225809b8b 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3317,8 +3317,10 @@ class Diaspora $type = 'reshare'; } else { + $item['body'] = Post\Media::removeFromEndOfBody($item['body']); + $title = $item['title']; - $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item)); + $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item), [Post\Media::AUDIO, Post\Media::VIDEO]); // Fetch the title from an attached link - if there is one if (empty($item['title']) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) { @@ -3366,6 +3368,8 @@ class Diaspora 'location' => $location ]; + $message = self::addPhotos($item, $message); + // Diaspora rejects messages when they contain a location without "lat" or "lng" if (!isset($location['lat']) || !isset($location['lng'])) { unset($message['location']); @@ -3400,6 +3404,37 @@ class Diaspora return $msg; } + private static function addPhotos(array $item, array $message): array + { + $medias = Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE]); + $public = ($item['private'] == Item::PRIVATE ? 'false' : 'true'); + + $counter = 0; + foreach ($medias as $media) { + if (Item::containsLink($item['body'], $media['preview'] ?? $media['url'], $media['type'])) { + continue; + } + + $name = basename($media['url']); + $path = str_replace($name, '', $media['url']); + + $message[++$counter . ':photo'] = [ + 'guid' => Item::guid(['uri' => $media['url']], false), + 'author' => $item['author-addr'], + 'public' => $public, + 'created_at' => $item['created'], + 'remote_photo_path' => $path, + 'remote_photo_name' => $name, + 'status_message_guid' => $item['guid'], + 'height' => $media['height'], + 'width' => $media['width'], + 'text' => $media['description'], + ]; + } + + return $message; + } + private static function prependParentAuthorMention(string $body, string $profile_url): string { $profile = Contact::getByURL($profile_url, false, ['addr', 'name']); diff --git a/static/routes.config.php b/static/routes.config.php index ad8cff519..1c6268aa9 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -554,6 +554,7 @@ return [ '/permission/tooltip/{type}/{id:\d+}' => [Module\PermissionTooltip::class, [R::GET]], '/photo' => [ + '/{size:thumb_small|scaled_full}_{name}' => [Module\Photo::class, [R::GET]], '/{name}' => [Module\Photo::class, [R::GET]], '/{type}/{id:\d+}' => [Module\Photo::class, [R::GET]], '/{type:contact|header}/{guid}' => [Module\Photo::class, [R::GET]], From aa3dc7c40c6b75e5ac04f028daf9deebb3b705ce Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 7 Jan 2023 21:28:21 +0000 Subject: [PATCH 2/2] Add configuration --- src/Protocol/Diaspora.php | 21 ++++++++++++++++++--- static/defaults.config.php | 7 +++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 9f23fe13b..fd000dbe8 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3316,10 +3316,16 @@ class Diaspora $type = 'reshare'; } else { - $item['body'] = Post\Media::removeFromEndOfBody($item['body']); + $native_photos = DI::config()->get('diaspora', 'native_photos'); + if ($native_photos) { + $item['body'] = Post\Media::removeFromEndOfBody($item['body']); + $attach_media = [Post\Media::AUDIO, Post\Media::VIDEO]; + } else { + $attach_media = [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]; + } $title = $item['title']; - $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item), [Post\Media::AUDIO, Post\Media::VIDEO]); + $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item), $attach_media); // Fetch the title from an attached link - if there is one if (empty($item['title']) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) { @@ -3367,7 +3373,9 @@ class Diaspora 'location' => $location ]; - $message = self::addPhotos($item, $message); + if ($native_photos) { + $message = self::addPhotos($item, $message); + } // Diaspora rejects messages when they contain a location without "lat" or "lng" if (!isset($location['lat']) || !isset($location['lng'])) { @@ -3403,6 +3411,13 @@ class Diaspora return $msg; } + /** + * Add photo elements to the message array + * + * @param array $item + * @param array $message + * @return array + */ private static function addPhotos(array $item, array $message): array { $medias = Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE]); diff --git a/static/defaults.config.php b/static/defaults.config.php index 967ca54eb..b3175c5d7 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -711,6 +711,13 @@ return [ // Must be writable by the ejabberd process. if set then it will prevent the running of multiple processes. 'lockpath' => '', ], + 'diaspora' => [ + // native_photos (Boolean) + // If enabled, photos to Diaspora will be transmitted via the "photo" element instead of embedding them to the body. + // This is some visual improvement over the embedding but comes with the cost of losing accessibility. + // Is is disabled by default until Diaspora eventually will work on issue https://github.com/diaspora/diaspora/issues/8297 + 'native_photos' => false, + ], 'debug' => [ // ap_inbox_log (Boolean) // Logs every call to /inbox as a JSON file in Friendica's temporary directory