From 59004711ec346eccba5b77c6fc76e347993f8295 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 29 Sep 2022 22:29:15 +0000 Subject: [PATCH 1/5] Support for transmitting quoted posts --- src/Protocol/ActivityPub.php | 2 ++ src/Protocol/ActivityPub/Transmitter.php | 39 ++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 93204e81d3..603416a5d2 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -72,6 +72,8 @@ class ActivityPub 'schema' => 'http://schema.org#', 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag', + 'quoteUrl' => 'as:quoteUrl', + 'conversation' => 'ostatus:conversation', 'directMessage' => 'litepub:directMessage', 'discoverable' => 'toot:discoverable', 'PropertyValue' => 'schema:PropertyValue', diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 297020e8ed..e0d9117e3e 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1355,11 +1355,12 @@ class Transmitter /** * Returns a tag array for a given item array * - * @param array $item Item array + * @param array $item Item array + * @param string $quote_url Url of the attached quote link * @return array of tags * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function createTagList(array $item): array + private static function createTagList(array $item, string $quote_url): array { $tags = []; @@ -1389,6 +1390,16 @@ class Transmitter $tags[] = ['type' => 'Mention', 'href' => $announce['actor']['url'], 'name' => '@' . $announce['actor']['addr']]; } + // @see https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md + if (!empty($quote_url)) { + $tags[] = [ + 'type' => 'Link', + 'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', + 'href' => $quote_url, + 'name' => BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB) + ]; + } + return $tags; } @@ -1662,6 +1673,12 @@ class Transmitter $body = BBCode::setMentionsToNicknames($body); + $shared = BBCode::fetchShareAttributes($body); + if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) { + $body = self::ReplaceSharedData($body); + $data['quoteUrl'] = $shared['link']; + } + $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB); } @@ -1683,7 +1700,7 @@ class Transmitter } $data['attachment'] = self::createAttachmentList($item, $type); - $data['tag'] = self::createTagList($item); + $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? ''); if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) { $data['location'] = self::createLocation($item); @@ -1698,6 +1715,22 @@ class Transmitter return $data; } + /** + * Replace the share block with a link + * + * @param string $body + * @return string + */ + private static function ReplaceSharedData(string $body): string + { + return BBCode::convertShare( + $body, + function (array $attributes) { + return $attributes['link']; + } + ); + } + /** * Fetches the language from the post, the user or the system. * From d43b85e94b729b4d9c0e7d4bfda4a946486f124c Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 30 Sep 2022 07:10:43 +0000 Subject: [PATCH 2/5] Don't transmit the shared attachments --- src/Protocol/ActivityPub/Transmitter.php | 70 +++++++++++------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index e0d9117e3e..82816e6685 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1407,51 +1407,39 @@ class Transmitter * Adds attachment data to the JSON document * * @param array $item Data of the item that is to be posted - * @param string $type Object type * * @return array with attachment data * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - private static function createAttachmentList(array $item, string $type): array + private static function createAttachmentList(array $item): array { $attachments = []; - $uriids = [$item['uri-id']]; - $shared = BBCode::fetchShareAttributes($item['body']); - if (!empty($shared['guid'])) { - $shared_item = Post::selectFirst(['uri-id'], ['guid' => $shared['guid']]); - if (!empty($shared_item['uri-id'])) { - $uriids[] = $shared_item['uri-id']; - } - } - $urls = []; - foreach ($uriids as $uriid) { - foreach (Post\Media::getByURIId($uriid, [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) { - if (in_array($attachment['url'], $urls)) { - continue; - } - $urls[] = $attachment['url']; - - $attach = ['type' => 'Document', - 'mediaType' => $attachment['mimetype'], - 'url' => $attachment['url'], - 'name' => $attachment['description']]; - - if (!empty($attachment['height'])) { - $attach['height'] = $attachment['height']; - } - - if (!empty($attachment['width'])) { - $attach['width'] = $attachment['width']; - } - - if (!empty($attachment['preview'])) { - $attach['image'] = $attachment['preview']; - } - - $attachments[] = $attach; + foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO, Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) { + if (in_array($attachment['url'], $urls)) { + continue; } + $urls[] = $attachment['url']; + + $attach = ['type' => 'Document', + 'mediaType' => $attachment['mimetype'], + 'url' => $attachment['url'], + 'name' => $attachment['description']]; + + if (!empty($attachment['height'])) { + $attach['height'] = $attachment['height']; + } + + if (!empty($attachment['width'])) { + $attach['width'] = $attachment['width']; + } + + if (!empty($attachment['preview'])) { + $attach['image'] = $attachment['preview']; + } + + $attachments[] = $attach; } return $attachments; @@ -1688,6 +1676,12 @@ class Transmitter $language = self::getLanguage($item); if (!empty($language)) { $richbody = BBCode::setMentionsToNicknames($item['body'] ?? ''); + + $shared = BBCode::fetchShareAttributes($richbody); + if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) { + $richbody = self::ReplaceSharedData($richbody); + } + $richbody = BBCode::removeAttachment($richbody); $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL); @@ -1699,8 +1693,8 @@ class Transmitter $data['diaspora:comment'] = $item['signed_text']; } - $data['attachment'] = self::createAttachmentList($item, $type); - $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? ''); + $data['attachment'] = self::createAttachmentList($item); + $data['tag'] = self::createTagList($item, ''); // $data['quoteUrl'] ?? if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) { $data['location'] = self::createLocation($item); From 8eb4ab2a3398fe4cf92ddc621e43f253c5dce415 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 30 Sep 2022 07:17:07 +0000 Subject: [PATCH 3/5] Added comment for the deactivated part --- src/Protocol/ActivityPub/Transmitter.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 82816e6685..fae9250aac 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1392,12 +1392,13 @@ class Transmitter // @see https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md if (!empty($quote_url)) { - $tags[] = [ - 'type' => 'Link', - 'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', - 'href' => $quote_url, - 'name' => BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB) - ]; + // Currently deactivated because of compatibility issues with Pleroma + //$tags[] = [ + // 'type' => 'Link', + // 'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', + // 'href' => $quote_url, + // 'name' => BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB) + //]; } return $tags; @@ -1694,7 +1695,7 @@ class Transmitter } $data['attachment'] = self::createAttachmentList($item); - $data['tag'] = self::createTagList($item, ''); // $data['quoteUrl'] ?? + $data['tag'] = self::createTagList($item, $data['quoteUrl'] ?? ''); if (empty($data['location']) && (!empty($item['coord']) || !empty($item['location']))) { $data['location'] = self::createLocation($item); From 6fb5e13b08a163f73a8371c9e62702e09679053e Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 1 Oct 2022 22:36:02 +0000 Subject: [PATCH 4/5] =?UTF-8?q?Add=20=E2=99=B2=20symbol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Protocol/ActivityPub/Transmitter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index fae9250aac..96f39e2c16 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1397,7 +1397,7 @@ class Transmitter // 'type' => 'Link', // 'mediaType' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', // 'href' => $quote_url, - // 'name' => BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB) + // 'name' => '♲ ' . BBCode::convertForUriId($item['uri-id'], $quote_url, BBCode::ACTIVITYPUB) //]; } @@ -1721,7 +1721,7 @@ class Transmitter return BBCode::convertShare( $body, function (array $attributes) { - return $attributes['link']; + return '♲ ' . $attributes['link']; } ); } From 0de458e134617131376792bf42155abcb4fce0b0 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 2 Oct 2022 04:51:00 +0000 Subject: [PATCH 5/5] Case changed --- src/Protocol/ActivityPub/Transmitter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 96f39e2c16..38482d3ec9 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1664,7 +1664,7 @@ class Transmitter $shared = BBCode::fetchShareAttributes($body); if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) { - $body = self::ReplaceSharedData($body); + $body = self::replaceSharedData($body); $data['quoteUrl'] = $shared['link']; } @@ -1680,7 +1680,7 @@ class Transmitter $shared = BBCode::fetchShareAttributes($richbody); if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) { - $richbody = self::ReplaceSharedData($richbody); + $richbody = self::replaceSharedData($richbody); } $richbody = BBCode::removeAttachment($richbody); @@ -1716,7 +1716,7 @@ class Transmitter * @param string $body * @return string */ - private static function ReplaceSharedData(string $body): string + private static function replaceSharedData(string $body): string { return BBCode::convertShare( $body,