]> git.mxchange.org Git - friendica.git/commitdiff
Improve quite generation
authorMichael <heluecht@pirati.ca>
Sat, 15 Oct 2022 13:00:52 +0000 (13:00 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 15 Oct 2022 13:00:52 +0000 (13:00 +0000)
src/Content/Item.php
src/Content/Text/BBCode.php
src/Model/Item.php
src/Protocol/ActivityPub/Transmitter.php

index 6cbcb636955f58446e6273e703a946995c6b0147..1838d4ac40ffc9090d943d890d28ad386fe1ee2d 100644 (file)
@@ -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']);
index 59b2e77a4a9e51c3e4a06c426c4f4091dc4e6e5c..d03161df9f9e8a8c0e49579a0e846654f096555d 100644 (file)
@@ -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);
        }
 
        /**
index ceb8fcc88fd4f6ad5da7986eb894980b94079a19..439c1b990094b7b059de751197589e001554a29e 100644 (file)
@@ -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)) {
index de9a3535f3060246c96c3145777b147502bf8690..c37fefb42cebb96931ed53220fc198ed730aa3cb 100644 (file)
@@ -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);