Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diaspora: Support for native photos #12630

Merged
merged 4 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Module/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ protected function rawContent(array $request = [])
$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));
Expand Down
1 change: 1 addition & 0 deletions src/Protocol/ActivityPub/Transmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,7 @@ public static function createNote(array $item): array
$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']);
Expand Down
52 changes: 51 additions & 1 deletion src/Protocol/Diaspora.php
Original file line number Diff line number Diff line change
Expand Up @@ -3316,8 +3316,16 @@ public static function buildStatus(array $item, array $owner)

$type = 'reshare';
} else {
$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));
$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')) {
Expand Down Expand Up @@ -3365,6 +3373,10 @@ public static function buildStatus(array $item, array $owner)
'location' => $location
];

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'])) {
unset($message['location']);
Expand Down Expand Up @@ -3399,6 +3411,44 @@ public static function buildStatus(array $item, array $owner)
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]);
$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']);
Expand Down
7 changes: 7 additions & 0 deletions static/defaults.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,13 @@
// 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
Expand Down
1 change: 1 addition & 0 deletions static/routes.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@
'/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]],
Expand Down