From 4d40bac8ee1e6d1ba367e6c931d02774c9d5908c Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 10 Apr 2021 21:13:37 +0000 Subject: [PATCH 1/2] Fix mentions for the summary --- src/Protocol/ActivityPub/Transmitter.php | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 65a3c9167..3a6cf56bd 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1333,6 +1333,27 @@ class Transmitter return '[url=' . $data['url'] . ']@' . $data['nick'] . '[/url]'; } + /** + * Callback function to replace a Friendica style mention in a mention for a summary + * + * @param array $match Matching values for the callback + * @return string Replaced mention + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + private static function mentionAddrCallback($match) + { + if (empty($match[1])) { + return ''; + } + + $data = Contact::getByURL($match[1], false, ['addr', 'nick']); + if (empty($data['addr'])) { + return $match[0]; + } + + return '@' . $data['addr']; + } + /** * Remove image elements since they are added as attachment * @@ -1495,7 +1516,9 @@ class Transmitter if ($type == 'Note') { $body = $item['raw-body'] ?? self::removePictures($body); } elseif (($type == 'Article') && empty($data['summary'])) { - $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000)); + $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism"; + $summary = preg_replace_callback($regexp, ['\Friendica\Protocol\ActivityPub\Transmitter', 'mentionAddrCallback'], $body); + $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($summary), 1000)); } if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) { From 304b52ec0fdcdad25b0a7e75f72d482c5677b178 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 10 Apr 2021 21:33:18 +0000 Subject: [PATCH 2/2] Improve the code --- 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 3a6cf56bd..2c1276c7e 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1346,7 +1346,7 @@ class Transmitter return ''; } - $data = Contact::getByURL($match[1], false, ['addr', 'nick']); + $data = Contact::getByURL($match[1], false, ['addr']); if (empty($data['addr'])) { return $match[0]; } @@ -1517,7 +1517,7 @@ class Transmitter $body = $item['raw-body'] ?? self::removePictures($body); } elseif (($type == 'Article') && empty($data['summary'])) { $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism"; - $summary = preg_replace_callback($regexp, ['\Friendica\Protocol\ActivityPub\Transmitter', 'mentionAddrCallback'], $body); + $summary = preg_replace_callback($regexp, ['self', 'mentionAddrCallback'], $body); $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($summary), 1000)); }