]> git.mxchange.org Git - friendica.git/commitdiff
Some more improvements for posts with shares
authorMichael <heluecht@pirati.ca>
Wed, 26 Oct 2022 17:00:55 +0000 (17:00 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 26 Oct 2022 17:00:55 +0000 (17:00 +0000)
mod/display.php
mod/share.php
src/Content/Item.php
src/Content/Text/BBCode.php
src/Factory/Api/Mastodon/Status.php
src/Factory/Api/Twitter/Status.php
src/Model/Item.php
src/Model/Post/Media.php
src/Protocol/ActivityPub/Transmitter.php
src/Protocol/Diaspora.php

index 41c4cc3b9a8a2683fa771c20f892342c3c6f5759..62e180874d1c60b6d61f19cd7516fda6688a719e 100644 (file)
@@ -122,9 +122,9 @@ function display_init(App $a)
 
 function display_fetchauthor($item)
 {
-       $shared = Item::getShareArray($item);
-       if (empty($shared['comment']) && !empty($shared['guid']) && !empty($shared['profile'])) {
-               $contact = Contact::getByURLForUser($shared['profile'], DI::userSession()->getLocalUserId());
+       $shared = DI::contentItem()->getSharedPost($item, ['author-link']);
+       if (!empty($shared) && empty($shared['comment'])) {
+               $contact = Contact::getByURLForUser($shared['post']['author-link'], DI::userSession()->getLocalUserId());
        }
 
        if (empty($contact)) {
index 9f94d9ddc9e0ecbd571c537e5cd78a080080caff..f0a2170b5e90b683c9d2b2b080181e96f9f05d5d 100644 (file)
@@ -41,9 +41,9 @@ function share_init(App $a) {
                System::exit();
        }
 
-       $shared = Item::getShareArray($item);
-       if (empty($shared['comment']) && (!empty($shared['message_id']) || !empty($shared['link']))) {
-               $content = '[share]' . ($shared['message_id'] ?: $shared['link']) . '[/share]';
+       $shared = DI::contentItem()->getSharedPost($item, ['uri']);
+       if (!empty($shared) && empty($shared['comment'])) {
+               $content = '[share]' . $shared['post']['uri'] . '[/share]';
        } else {
                $content = '[share]' . $item['uri'] . '[/share]';
        }
index f03e42a088c91b69fe87c2b8841d877553845cee..f72493c17ef908530ee949f91ca666e76347f6cb 100644 (file)
@@ -680,11 +680,11 @@ class Item
                        $shared_content .= '[h3]' . $item['title'] . "[/h3]\n";
                }
 
-               $shared = ItemModel::getShareArray($item);
+               $shared = $this->getSharedPost($item, ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network']);
 
                // If it is a reshared post then reformat it to avoid display problems with two share elements
                if (!empty($shared)) {
-                       if (!empty($shared['guid']) && ($encaspulated_share = self::createSharedPostByGuid($shared['guid'], 0, '', $add_media))) {
+                       if (($encaspulated_share = $this->createSharedBlockByArray($shared['post'], $add_media))) {
                                $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
                        }
 
@@ -695,4 +695,38 @@ class Item
 
                return $shared_content;
        }
+
+       /**
+        * Return the shared post from an item array (if the item is shared item)
+        *
+        * @param array $item
+        * @param array $fields
+        *
+        * @return array with the shared post
+        */
+       public function getSharedPost(array $item, array $fields = []): array
+       {
+               if (!empty($item['quote-uri-id'])) {
+                       $shared = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id'], 'uid' => [0, $item['uid'] ?? 0]]);
+                       if (is_array($shared)) {
+                               return [
+                                       'comment' => BBCode::removeSharedData($item['body'] ?? ''),
+                                       'post'    => $shared
+                               ];
+                       }
+               }
+
+               $attributes = BBCode::fetchShareAttributes($item['body'] ?? '');
+               if (!empty($attributes)) {
+                       $shared = Post::selectFirst($fields, ['guid' => $attributes['guid'], 'uid' => [0, $item['uid'] ?? 0]]);
+                       if (is_array($shared)) {
+                               return [
+                                       'comment' => $attributes['comment'],
+                                       'post'    => $shared
+                               ];
+                       }
+               }
+
+               return [];
+       }
 }
index 7394f5fd7b0d435b85648c50e998253f02fc2193..aa413cd022a67b3cc1586d2db230886caf0f4914 100644 (file)
@@ -1070,6 +1070,30 @@ class BBCode
                return $attributes;
        }
 
+       /**
+        * Checks, if the provided body contains a native reshare
+        *
+        * @param string $body
+        * @return boolean
+        */
+       public static function isNativeReshare(string $body): bool
+       {
+               $shared = BBCode::fetchShareAttributes($body);
+               return !empty($shared['guid'] ?? '');
+       }
+
+       /**
+        * Checks if the provided body contains a "share" element
+        *
+        * @param string $body
+        * @return boolean
+        */
+       public static function existsShare(string $body): bool
+       {
+               $shared = BBCode::fetchShareAttributes($body);
+               return !empty($shared['link'] ?? '');
+       }
+
        /**
         * Replace the share block with a link
         *
@@ -1094,7 +1118,7 @@ class BBCode
         */
        public static function removeSharedData(string $body): string
        {
-               return preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
+               return trim(preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body));
        }
 
        /**
index 4e1f790f500e109a20ec91100ea275ca364d0c5e..caa219a237be235fc54011ce93d6dbd4dc76749e 100644 (file)
@@ -23,9 +23,9 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\BaseFactory;
 use Friendica\Content\ContactSelector;
-use Friendica\Content\Text\BBCode;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
+use Friendica\DI;
 use Friendica\Model\Item;
 use Friendica\Model\Post;
 use Friendica\Model\Tag as TagModel;
@@ -155,11 +155,9 @@ class Status extends BaseFactory
                        $poll = null;
                }
 
-               $shared = Item::getShareArray($item);
-               if (!empty($shared['guid'])) {
-                       $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
-
-                       $shared_uri_id = $shared_item['uri-id'] ?? 0;
+               $shared = DI::contentItem()->getSharedPost($item, ['uri-id']);
+               if (!empty($shared)) {
+                       $shared_uri_id = $shared['post']['uri-id'];
 
                        $mentions    = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
                        $tags        = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
index ac03172e89fe3031698baedd267537693345765d..33abfdbcc5d2fc4613136ad5873b1e09b4743aa7 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\BaseFactory;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Database\Database;
+use Friendica\DI;
 use Friendica\Factory\Api\Friendica\Activities;
 use Friendica\Factory\Api\Twitter\User as TwitterUser;
 use Friendica\Model\Item;
@@ -178,11 +179,9 @@ class Status extends BaseFactory
 
                $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid);
 
-               $shared = Item::getShareArray($item);
-               if (!empty($shared['guid'])) {
-                       $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
-
-                       $shared_uri_id = $shared_item['uri-id'] ?? 0;
+               $shared = DI::contentItem()->getSharedPost($item, ['uri-id']);
+               if (!empty($shared)) {
+                       $shared_uri_id = $shared['post']['uri-id'];
 
                        if ($include_entities) {
                                $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text));
index 5eb3ba6a5d5034b0b85e25bd351cd53829dad2d9..ced18fc8a68f74aed8557da4522580aa8eb2c73d 100644 (file)
@@ -2949,20 +2949,18 @@ class Item
                $item['mentions'] = $tags['mentions'];
 
                $body = $item['body'] ?? '';
-               $shared = self::getShareArray($item);
-               if (!empty($shared['guid'])) {
-                       $shared_item = Post::selectFirst(['uri-id', 'guid', 'plink', 'has-media'], ['guid' => $shared['guid'], 'uid' => [$item['uid'], 0]]);
-               }
 
                $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media'];
 
                $shared_uri_id = 0;
                $shared_links  = [];
 
-               if (empty($shared_item['uri-id']) && !empty($item['quote-uri-id'])) {
-                       $shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id']]);
-                       $quote_uri_id = $item['quote-uri-id'] ?? 0;
-                       $shared_links[] = strtolower($item['quote-uri']);
+               $shared = DI::contentItem()->getSharedPost($item, $fields);
+               if (!empty($shared['post'])) {
+                       $shared_item  = $shared['post'];
+                       $quote_uri_id = $shared['post']['uri-id'];
+                       $shared_links[] = strtolower($shared['post']['uri']);
+                       $item['body'] = BBCode::removeSharedData($item['body']);
                } elseif (empty($shared_item['uri-id']) && empty($item['quote-uri-id'])) {
                        $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::ACTIVITY]);
                        if (!empty($media)) {
@@ -3588,43 +3586,6 @@ class Item
                return 0;
        }
 
-       /**
-        * Return share data from an item array (if the item is shared item)
-        * We are providing the complete Item array, because at some time in the future
-        * we hopefully will define these values not in the body anymore but in some item fields.
-        * This function is meant to replace all similar functions in the system.
-        *
-        * @param array $item
-        *
-        * @return array with share information
-        */
-       public static function getShareArray(array $item): array
-       {
-               $attributes = BBCode::fetchShareAttributes($item['body'] ?? '');
-               if (!empty($attributes)) {
-                       return $attributes;
-               }
-
-               if (!empty($item['quote-uri-id'])) {
-                       $shared = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'uri', 'body'], ['uri-id' => $item['quote-uri-id']]);
-                       if (!empty($shared)) {
-                               return [
-                                       'author'     => $shared['author-name'],
-                                       'profile'    => $shared['author-link'],
-                                       'avatar'     => $shared['author-avatar'],
-                                       'link'       => $shared['plink'],
-                                       'posted'     => $shared['created'],
-                                       'guid'       => $shared['guid'],
-                                       'message_id' => $shared['uri'],
-                                       'comment'    => $item['body'],
-                                       'shared'     => $shared['body'],
-                               ];                              
-                       }
-               }
-
-               return [];
-       }
-
        /**
         * Check a prospective item array against user-level permissions
         *
index 8d86bc757cba596d8688ca5c93809edc688f6bad..37603ad372659b7bb61a989f61f8e2b08f352610 100644 (file)
@@ -404,8 +404,7 @@ class Media
                $unshared_body = $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
 
                // Only remove the shared data from "real" reshares
-               $shared = BBCode::fetchShareAttributes($body);
-               if (!empty($shared['guid'])) {
+               if (BBCode::isNativeReshare($body)) {
                        $unshared_body = BBCode::removeSharedData($body);
                }
 
@@ -486,8 +485,7 @@ class Media
        public static function insertFromRelevantUrl(int $uriid, string $body)
        {
                // Only remove the shared data from "real" reshares
-               $shared = BBCode::fetchShareAttributes($body);
-               if (!empty($shared['guid'])) {
+               if (BBCode::isNativeReshare($body)) {
                        // Don't look at the shared content
                        $body = BBCode::removeSharedData($body);
                }
index cc7774b23a9f1ff67e3febec29159f18cad940d5..7cb1eecf50507fe30bf182189bab3ed21e6cb71d 100644 (file)
@@ -1664,18 +1664,18 @@ class Transmitter
                                $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
                        }
 
-                       $body   = BBCode::setMentionsToNicknames($body);
-                       $shared = BBCode::fetchShareAttributes($body);
+                       $body           = BBCode::setMentionsToNicknames($body);
+                       $exists_reshare = BBCode::existsShare($body);
 
                        if (!empty($item['quote-uri']) && Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
                                $real_quote = true;
-                               if (!empty($shared['link'])) {
+                               if ($exists_reshare) {
                                        $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']) && empty($shared)) {
+                       } elseif (!empty($item['quote-uri']) && !$exists_reshare) {
                                $body .= "\n" . DI::contentItem()->createSharedPostByUriId($item['quote-uri-id'], $item['uid'], true);
                                $item['body'] = Item::improveSharedDataInBody($item, true);
                        }
@@ -1691,8 +1691,7 @@ class Transmitter
                        $richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
 
                        if ($real_quote) {
-                               $shared = BBCode::fetchShareAttributes($richbody);
-                               if (!empty($shared['link'])) {
+                               if (BBCode::existsShare($richbody)) {
                                        $richbody = BBCode::replaceSharedData($richbody);
                                } elseif (strpos($richbody, $item['quote-uri']) === false) {
                                        $richbody .= "\n♲ " . $item['quote-uri'];
@@ -1821,28 +1820,23 @@ class Transmitter
         * @param array $item
         * @return array Announcement array
         */
-       public static function getAnnounceArray(array $item): array
+       private static function getAnnounceArray(array $item): array
        {
-               $reshared = Item::getShareArray($item);
-               if (empty($reshared['guid'])) {
+               $reshared = DI::contentItem()->getSharedPost($item, Item::DELIVER_FIELDLIST);
+               if (empty($reshared)) {
                        return [];
                }
 
-               $reshared_item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['guid' => $reshared['guid']]);
-               if (!DBA::isResult($reshared_item)) {
+               if (!in_array($reshared['post']['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
                        return [];
                }
 
-               if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
-                       return [];
-               }
-
-               $profile = APContact::getByURL($reshared_item['author-link'], false);
+               $profile = APContact::getByURL($reshared['post']['author-link'], false);
                if (empty($profile)) {
                        return [];
                }
 
-               return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']];
+               return ['object' => $reshared['post'], 'actor' => $profile, 'comment' => $reshared['comment']];
        }
 
        /**
index 389bbc55d27fb1116414d6b51cbdd0f1f36da443..bd5da5445ac427492e17bab3b381a740f2a8c072 100644 (file)
@@ -3181,27 +3181,20 @@ class Diaspora
         */
        public static function getReshareDetails(array $item): array
        {
-               $reshared = Item::getShareArray($item);
+               $reshared = DI::contentItem()->getSharedPost($item, ['network', 'author-addr']);
                if (empty($reshared)) {
                        return [];
                }
 
                // Skip if it isn't a pure repeated messages or not a real reshare
-               if (!empty($reshared['comment']) || empty($reshared['guid'])) {
+               if (!empty($reshared['comment']) || !in_array($reshared['post']['network'], [Protocol::DFRN, Protocol::DIASPORA])) {
                        return [];
                }
 
-               $condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
-               $item = Post::selectFirst(['author-addr'], $condition);
-               if (DBA::isResult($item)) {
-                       return [
-                               'root_handle' => strtolower($item['author-addr']),
-                               'root_guid'   => $reshared['guid']
-                       ];
-               }
-
-               // We are resharing something that isn't a DFRN or Diaspora post.
-               return [];
+               return [
+                       'root_handle' => strtolower($reshared['post']['author-addr']),
+                       'root_guid'   => $reshared['guid']
+               ];
        }
 
        /**