diff --git a/src/Content/Item.php b/src/Content/Item.php index 6cbcb6369..1838d4ac4 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -574,9 +574,10 @@ class Item * * @param string $url * @param integer $uid + * @param bool $add_media * @return string */ - public function createSharedPostByUrl(string $url, int $uid = 0): string + public function createSharedPostByUrl(string $url, int $uid = 0, bool $add_media = false): string { if (!empty($uid)) { $id = ModelItem::searchByLink($url, $uid); @@ -599,7 +600,7 @@ class Item return ''; } - return $this->createSharedBlockByArray($shared_item); + return $this->createSharedBlockByArray($shared_item, $add_media); } /** @@ -652,9 +653,10 @@ class Item * Add a share block for the given item array * * @param array $item + * @param bool $add_media * @return string */ - public function createSharedBlockByArray(array $item): string + public function createSharedBlockByArray(array $item, bool $add_media = false): string { if ($item['network'] == Protocol::FEED) { return PageInfo::getFooterFromUrl($item['plink']); @@ -662,6 +664,8 @@ class Item $item['guid'] = ''; $item['uri'] = ''; $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']); + } elseif ($add_media) { + $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']); } $shared_content = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid'], $item['uri']); diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 59b2e77a4..d03161df9 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1085,6 +1085,22 @@ class BBCode ); } + /** + * Remove the share block + * + * @param string $body + * @return string + */ + public static function removeSharedData(string $body): string + { + return BBCode::convertShare( + $body, + function (array $attributes) { + return ''; + } + ); + } + /** * This function converts a [share] block to text according to a provided callback function whose signature is: * @@ -1134,7 +1150,7 @@ class BBCode ); DI::profiler()->stopRecording(); - return $return; + return trim($return); } /** diff --git a/src/Model/Item.php b/src/Model/Item.php index ceb8fcc88..439c1b990 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1123,7 +1123,7 @@ class Item if (!empty($quote_id) && Post::exists(['uri-id' => $quote_id, 'network' => Protocol::FEDERATED])) { $item['quote-uri-id'] = $quote_id; - $item['raw-body'] = BBCode::replaceSharedData($item['raw-body']); + $item['raw-body'] = BBCode::removeSharedData($item['raw-body']); } if (!DBA::exists('contact', ['id' => $item['author-id'], 'network' => Protocol::DFRN])) { @@ -3612,9 +3612,10 @@ class Item * Improve the data in shared posts * * @param array $item + * @param bool $add_media * @return string body */ - public static function improveSharedDataInBody(array $item): string + public static function improveSharedDataInBody(array $item, bool $add_media = false): string { $shared = BBCode::fetchShareAttributes($item['body']); if (empty($shared['guid']) && empty($shared['message_id'])) { @@ -3624,7 +3625,7 @@ class Item $link = $shared['link'] ?: $shared['message_id']; if (empty($shared_content)) { - $shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0); + $shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0, $add_media); } if (empty($shared_content)) { diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index de9a3535f..c37fefb42 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -40,6 +40,7 @@ use Friendica\Model\User; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; +use Friendica\Protocol\Diaspora; use Friendica\Protocol\Relay; use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPSignature; @@ -1621,6 +1622,8 @@ class Transmitter $permission_block = self::createPermissionBlockForItem($item, false); + $real_quote = false; + $body = $item['body']; if ($type == 'Note') { @@ -1662,9 +1665,22 @@ class Transmitter $body = BBCode::setMentionsToNicknames($body); - if (!empty($item['quote-uri'])) { - $body = BBCode::replaceSharedData($body); + if (!empty($item['quote-uri']) && Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) { + $real_quote = true; + if (Diaspora::isReshare($body, false)) { + $body = BBCode::replaceSharedData($body); + } elseif (strpos($body, $item['quote-uri']) === false) { + $body .= "\n♲ " . $item['quote-uri']; + } $data['quoteUrl'] = $item['quote-uri']; + } elseif (!empty($item['quote-uri']) && !Diaspora::isReshare($body, false)) { + $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network']; + $shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id']]); + if (!empty($shared_item['uri-id'])) { + $shared_item['body'] = Post\Media::addAttachmentsToBody($shared_item['uri-id'], $shared_item['body']); + $body .= "\n" . DI::contentItem()->createSharedBlockByArray($shared_item); + $item['body'] = Item::improveSharedDataInBody($item, true); + } } $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB); @@ -1677,9 +1693,11 @@ class Transmitter if (!empty($language)) { $richbody = BBCode::setMentionsToNicknames($item['body'] ?? ''); - $shared = BBCode::fetchShareAttributes($richbody); - if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) { - $richbody = BBCode::replaceSharedData($richbody); + if ($real_quote) { + $shared = BBCode::fetchShareAttributes($richbody); + if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) { + $richbody = BBCode::replaceSharedData($richbody); + } } $richbody = BBCode::removeAttachment($richbody);