From 30f31828aecd77601b40083eaa642690d5221fbf Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 12 Mar 2024 03:12:36 +0000 Subject: [PATCH] Fixes API-Issues #13985 and #13986 --- src/Content/Text/BBCode.php | 36 +++++++++++++++++++++++ src/Factory/Api/Mastodon/StatusSource.php | 10 +++++-- src/Module/Api/Mastodon/Markers.php | 2 +- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 574b9987fc..9a3db9e0ec 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1245,6 +1245,42 @@ class BBCode return $match[1] . '[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; } + /** + * Replace mention links + * + * @param string $body HTML/BBCode + * @return string Body with replaced mentions + */ + public static function setMentionsToAddr(string $body): string + { + DI::profiler()->startRecording('rendering'); + $regexp = "/([@!])\[url\=([^\[\]]*)\].*?\[\/url\]/ism"; + $body = preg_replace_callback($regexp, [self::class, 'mentionToAddrCallback'], $body); + DI::profiler()->stopRecording(); + return $body; + } + + /** + * Callback function to replace a Friendica style mention in a mention with the addr + * + * @param array $match Matching values for the callback + * @return string Replaced mention or empty string + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + private static function mentionToAddrCallback(array $match): string + { + if (empty($match[2])) { + return ''; + } + + $data = Contact::getByURL($match[2], false, ['url', 'nick', 'addr']); + if (empty($data['nick'])) { + return $match[0]; + } + + return $match[1] . ($data['addr'] ?: $data['nick']); + } + /** * Normalize links to Youtube and Vimeo to a unified format. * diff --git a/src/Factory/Api/Mastodon/StatusSource.php b/src/Factory/Api/Mastodon/StatusSource.php index 9ef6b6bf0a..7e2a18e292 100644 --- a/src/Factory/Api/Mastodon/StatusSource.php +++ b/src/Factory/Api/Mastodon/StatusSource.php @@ -38,10 +38,14 @@ class StatusSource extends BaseFactory */ public function createFromUriId(int $uriId, int $uid): \Friendica\Object\Api\Mastodon\StatusSource { - $post = Post::selectOriginal(['uri-id', 'raw-body', 'body', 'title'], ['uri-id' => $uriId, 'uid' => [0, $uid]]); + $post = Post::selectOriginal(['uri-id', 'raw-body', 'body', 'title', 'content-warning'], ['uri-id' => $uriId, 'uid' => [0, $uid]]); - $spoiler_text = $post['title'] ?: BBCode::toPlaintext(BBCode::getAbstract($post['body'], Protocol::ACTIVITYPUB)); - $body = BBCode::toMarkdown(Post\Media::removeFromEndOfBody($post['body'])); + $spoiler_text = $post['title'] ?: $post['content-warning'] ?: BBCode::toPlaintext(BBCode::getAbstract($post['body'], Protocol::ACTIVITYPUB)); + + $body = Post\Media::removeFromEndOfBody($post['body']); + $body = Post\Media::addHTMLLinkToBody($uriId, $body); + $body = BBCode::setMentionsToAddr($body); + $body = BBCode::toPlaintext($body); return new \Friendica\Object\Api\Mastodon\StatusSource($post['uri-id'], $body, $spoiler_text); } diff --git a/src/Module/Api/Mastodon/Markers.php b/src/Module/Api/Mastodon/Markers.php index 7319c2c475..f8a42fc0c1 100644 --- a/src/Module/Api/Mastodon/Markers.php +++ b/src/Module/Api/Mastodon/Markers.php @@ -82,7 +82,7 @@ class Markers extends BaseApi $values->{$marker['timeline']} = [ 'last_read_id' => $marker['last_read_id'], 'version' => $marker['version'], - 'updated_at' => $marker['updated_at'] + 'updated_at' => DateTimeFormat::utc($marker['updated_at'], DateTimeFormat::JSON) ]; } return $values;