From bb5f738619dd29264253a1e51f37b5c3c253e2be Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 22 Jul 2021 04:50:01 +0000 Subject: [PATCH 1/2] Fix for Mastodon falsely adding previews to mentions --- src/Content/Text/BBCode.php | 39 +++++++++++++----------- src/Protocol/ActivityPub/Transmitter.php | 4 +-- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 177d648081..44a6189d9d 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1748,6 +1748,27 @@ class BBCode } } + // Handle mentions and hashtag links + if ($simple_html == self::DIASPORA) { + // The ! is converted to @ since Diaspora only understands the @ + $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", + '@$3', + $text); + } elseif (in_array($simple_html, [self::OSTATUS, self::ACTIVITYPUB])) { + $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", + '$1$3', + $text); + $text = preg_replace("/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism", + '', + $text); + } elseif (in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::API])) { + $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", + '$1$3', + $text); + } else { + $text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text); + } + if (!$for_plaintext) { if (in_array($simple_html, [self::OSTATUS, self::API, self::ACTIVITYPUB])) { $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text); @@ -1758,24 +1779,6 @@ class BBCode $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::removePictureLinksCallback', $text); } - // Remove all hashtag addresses - if ($simple_html && !in_array($simple_html, [self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB])) { - $text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text); - } elseif ($simple_html == self::DIASPORA) { - // The ! is converted to @ since Diaspora only understands the @ - $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", - '@$3', - $text); - } elseif (in_array($simple_html, [self::OSTATUS, self::ACTIVITYPUB])) { - $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", - '$1$3', - $text); - } elseif (!$simple_html) { - $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", - '$1$3', - $text); - } - // Bookmarks in red - will be converted to bookmarks in friendica $text = preg_replace("/#\^\[url\](.*?)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $text); $text = preg_replace("/#\^\[url\=(.*?)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $text); diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index a65ebc8cda..53c060e87d 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1356,12 +1356,12 @@ class Transmitter return ''; } - $data = Contact::getByURL($match[1], false, ['url', 'alias', 'nick']); + $data = Contact::getByURL($match[1], false, ['url', 'nick']); if (empty($data['nick'])) { return $match[0]; } - return '[url=' . $data['url'] . ']@' . $data['nick'] . '[/url]'; + return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; } /** From 71f53c946f0231e0d729fc435b965f29fdb5efa6 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 22 Jul 2021 09:13:39 +0000 Subject: [PATCH 2/2] New function to replace mentions with nicknames --- src/Content/Text/BBCode.php | 33 ++++++++++++++++++++++++ src/Object/Api/Mastodon/Status.php | 2 +- src/Protocol/ActivityPub/Transmitter.php | 27 ++----------------- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 44a6189d9d..286c9900c7 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1263,6 +1263,39 @@ class BBCode return $bbcode; } + /** + * Replace names in mentions with nicknames + * + * @param string $body + * @return string Body with replaced mentions + */ + public static function setMentionsToNicknames(string $body):string + { + $regexp = "/([@!])\[url\=([^\[\]]*)\].*?\[\/url\]/ism"; + return preg_replace_callback($regexp, ['self', 'mentionCallback'], $body); + } + + /** + * Callback function to replace a Friendica style mention in a mention with the nickname + * + * @param array $match Matching values for the callback + * @return string Replaced mention + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + private static function mentionCallback($match) + { + if (empty($match[2])) { + return ''; + } + + $data = Contact::getByURL($match[2], false, ['url', 'nick']); + if (empty($data['nick'])) { + return $match[0]; + } + + return $match[1] . '[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; + } + /** * Converts a BBCode message for a given URI-ID to a HTML message * diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index 468126b4e6..1adad09326 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -131,7 +131,7 @@ class Status extends BaseDataTransferObject $this->muted = $userAttributes->muted; $this->bookmarked = $userAttributes->bookmarked; $this->pinned = $userAttributes->pinned; - $this->content = BBCode::convertForUriId($item['uri-id'], ($item['raw-body'] ?? $item['body']), BBCode::API); + $this->content = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::API); $this->reblog = $reblog; $this->application = $application->toArray(); $this->account = $account->toArray(); diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 53c060e87d..b7a180e064 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1343,27 +1343,6 @@ class Transmitter return $attachments; } - /** - * Callback function to replace a Friendica style mention in a mention that is used on AP - * - * @param array $match Matching values for the callback - * @return string Replaced mention - * @throws \Friendica\Network\HTTPException\InternalServerErrorException - */ - private static function mentionCallback($match) - { - if (empty($match[1])) { - return ''; - } - - $data = Contact::getByURL($match[1], false, ['url', 'nick']); - if (empty($data['nick'])) { - return $match[0]; - } - - return '@[url=' . $data['url'] . ']' . $data['nick'] . '[/url]'; - } - /** * Callback function to replace a Friendica style mention in a mention for a summary * @@ -1568,8 +1547,7 @@ class Transmitter if ($type == 'Event') { $data = array_merge($data, self::createEvent($item)); } else { - $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism"; - $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body); + $body = BBCode::setMentionsToNicknames($body); $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB); } @@ -1579,8 +1557,7 @@ class Transmitter // The contentMap does contain the unmodified HTML. $language = self::getLanguage($item); if (!empty($language)) { - $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism"; - $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']); + $richbody = BBCode::setMentionsToNicknames($item['body']); $richbody = BBCode::removeAttachment($richbody); $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);