From a653c6350d6d5ddc76d86040271df142a3ad237c Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 29 Sep 2022 16:04:33 +0000 Subject: [PATCH] Support for quoted links is added --- src/Protocol/ActivityPub/Processor.php | 38 ++++++++++++++++++++++++++ src/Protocol/ActivityPub/Receiver.php | 18 ++++++++++++ src/Util/JsonLD.php | 2 ++ static/litepub-0.1.jsonld | 6 +++- 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 6fcce4a3a..f75e6189b 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -848,6 +848,10 @@ class Processor } $item['content-warning'] = HTML::toBBCode($activity['summary'] ?? ''); $item['raw-body'] = $item['body'] = $content; + + if (!empty($activity['quote-url'])) { + $item['body'] .= self::addSharedData($activity['quote-url']); + } } self::storeFromBody($item); @@ -866,6 +870,40 @@ class Processor return $item; } + /** + * Add a share block for the given quote link + * + * @param string $url + * @return string + */ + private static function addSharedData(string $url): string + { + $id = Item::fetchByLink($url); + if (empty($id)) { + return ''; + } + + $shared_item = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'title', 'body'], ['id' => $id]); + if (!DBA::isResult($shared_item)) { + return ''; + } + + $prefix = BBCode::getShareOpeningTag( + $shared_item['author-name'], + $shared_item['author-link'], + $shared_item['author-avatar'], + $shared_item['plink'], + $shared_item['created'], + $shared_item['guid'] + ); + + if (!empty($shared_item['title'])) { + $prefix .= '[h3]' . $shared_item['title'] . "[/h3]\n"; + } + + return $prefix . $shared_item['body'] . '[/share]'; + } + /** * Store hashtags and mentions * diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index c161ce4d1..a410c3525 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1916,6 +1916,24 @@ class Receiver $object_data['attachments'] = array_merge($object_data['attachments'], self::processAttachmentUrls($object['as:url'] ?? [])); } + // Support for quoted posts (Pleroma, Fedibird and Misskey) + $object_data['quote-url'] = JsonLD::fetchElement($object, 'as:quoteUrl', '@value'); + if (empty($object_data['quote-url'])) { + $object_data['quote-url'] = JsonLD::fetchElement($object, 'fedibird:quoteUri', '@value'); + } + if (empty($object_data['quote-url'])) { + $object_data['quote-url'] = JsonLD::fetchElement($object, 'misskey:_misskey_quote', '@value'); + } + + // Misskey adds some data to the standard "content" value for quoted posts for backwards compatibility. + // Their own "_misskey_content" value does then contain the content without this extra data. + if (!empty($object_data['quote-url'])) { + $misskey_content = JsonLD::fetchElement($object, 'misskey:_misskey_content', '@value'); + if (!empty($misskey_content)) { + $object_data['content'] = $misskey_content; + } + } + // For page types we expect that the alternate url posts to some page. // So we add this to the attachments if it differs from the id. // Currently only Lemmy is using the page type. diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php index 947274134..25ce74fcc 100644 --- a/src/Util/JsonLD.php +++ b/src/Util/JsonLD.php @@ -157,6 +157,8 @@ class JsonLD 'sc' => (object)['@id' => 'http://schema.org#', '@type' => '@id'], 'pt' => (object)['@id' => 'https://joinpeertube.org/ns#', '@type' => '@id'], 'mobilizon' => (object)['@id' => 'https://joinmobilizon.org/ns#', '@type' => '@id'], + 'fedibird' => (object)['@id' => 'http://fedibird.com/ns#', '@type' => '@id'], + 'misskey' => (object)['@id' => 'https://misskey-hub.net/ns#', '@type' => '@id'], ]; $orig_json = $json; diff --git a/static/litepub-0.1.jsonld b/static/litepub-0.1.jsonld index e7722cf72..16c22ff0f 100644 --- a/static/litepub-0.1.jsonld +++ b/static/litepub-0.1.jsonld @@ -17,6 +17,7 @@ "ostatus": "http://ostatus.org#", "schema": "http://schema.org#", "toot": "http://joinmastodon.org/ns#", + "fedibird": "http://fedibird.com/ns#", "value": "schema:value", "sensitive": "as:sensitive", "litepub": "http://litepub.social/ns#", @@ -26,6 +27,8 @@ "@id": "litepub:listMessage", "@type": "@id" }, + "quoteUrl": "as:quoteUrl", + "quoteUri": "fedibird:quoteUri", "oauthRegistrationEndpoint": { "@id": "litepub:oauthRegistrationEndpoint", "@type": "@id" @@ -35,7 +38,8 @@ "alsoKnownAs": { "@id": "as:alsoKnownAs", "@type": "@id" - } + }, + "vcard": "http://www.w3.org/2006/vcard/ns#" } ] }