]> git.mxchange.org Git - friendica.git/commitdiff
New function to replace mentions with nicknames
authorMichael <heluecht@pirati.ca>
Thu, 22 Jul 2021 09:13:39 +0000 (09:13 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 22 Jul 2021 09:13:39 +0000 (09:13 +0000)
src/Content/Text/BBCode.php
src/Object/Api/Mastodon/Status.php
src/Protocol/ActivityPub/Transmitter.php

index 44a6189d9d5533bc0a916e75ab7fba5eb57d9d2d..286c9900c7028851779f60f7ad4d650dd9ed76ea 100644 (file)
@@ -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
         *
index 468126b4e6cfc816ec84495459cc618f05ab12f7..1adad0932611dffe6fc552993af3d22ede3e33dd 100644 (file)
@@ -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();
index 53c060e87d4ceed9ba23e21e770cb0d12cd7dd4d..b7a180e06493518f7f31180027ecb891d6996a5a 100644 (file)
@@ -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);