]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge remote-tracking branch 'upstream/2022.09-rc' into quote-uri-id
[friendica.git] / src / Model / Item.php
index 2ce582c3223ecd9da151f5598ab67b47a6a95a97..797def3bd877e804abd3b777ce8f00e14cff6cfb 100644 (file)
@@ -93,7 +93,7 @@ class Item
                'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
                'wall', 'private', 'starred', 'origin', 'parent-origin', 'title', 'body', 'language',
                'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
-               'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'mention', 'global',
+               'quote-uri', 'quote-uri-id', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'mention', 'global',
                'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network', 'author-updated', 'author-gsid', 'author-addr', 'author-uri-id',
                'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network', 'owner-contact-type', 'owner-updated',
                'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type', 'causer-network',
@@ -115,7 +115,7 @@ class Item
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                        'author-id', 'author-link', 'author-name', 'author-avatar', 'owner-id', 'owner-link', 'contact-uid',
                        'signed_text', 'network', 'wall', 'contact-id', 'plink', 'origin',
-                       'thr-parent-id', 'parent-uri-id', 'postopts', 'pubmail',
+                       'thr-parent-id', 'parent-uri-id', 'quote-uri', 'quote-uri-id', 'postopts', 'pubmail',
                        'event-created', 'event-edited', 'event-start', 'event-finish',
                        'event-summary', 'event-desc', 'event-location', 'event-type',
                        'event-nofinish', 'event-ignore', 'event-id'];
@@ -123,7 +123,7 @@ class Item
        // All fields in the item table
        const ITEM_FIELDLIST = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
                        'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'conversation', 'vid',
-                       'contact-id', 'wall', 'gravity', 'extid', 'psid',
+                       'quote-uri', 'quote-uri-id', 'contact-id', 'wall', 'gravity', 'extid', 'psid',
                        'created', 'edited', 'commented', 'received', 'changed', 'verb',
                        'postopts', 'plink', 'resource-id', 'event-id', 'inform',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type', 'post-reason',
@@ -1119,6 +1119,11 @@ class Item
                $item['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $item['raw-body']);
                $item['raw-body'] = self::setHashtags($item['raw-body']);
 
+               if ($quote_id = self::getQuoteUriId($item['body'])) {
+                       $item['quote-uri-id'] = $quote_id;
+                       $item['raw-body'] = BBCode::replaceSharedData($item['raw-body']);
+               }
+
                if (!DBA::exists('contact', ['id' => $item['author-id'], 'network' => Protocol::DFRN])) {
                        Post\Media::insertFromRelevantUrl($item['uri-id'], $item['raw-body']);
                }
@@ -1552,14 +1557,16 @@ class Item
 
                $item = array_merge($item, $fields);
 
-               $item['post-reason'] = self::getPostReason($item);
+               if (($uid != 0) && Contact::isSharing(($item['gravity'] == GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'], $uid)) {
+                       $item['post-reason'] = self::PR_FOLLOWER;
+               }
 
                $is_reshare = ($item['gravity'] == GRAVITY_ACTIVITY) && ($item['verb'] == Activity::ANNOUNCE);
 
                if (($uid != 0) && (($item['gravity'] == GRAVITY_PARENT) || $is_reshare) &&
                        DI::pConfig()->get($uid, 'system', 'accept_only_sharer') == self::COMPLETION_NONE &&
                        !in_array($item['post-reason'], [self::PR_FOLLOWER, self::PR_TAG, self::PR_TO, self::PR_CC])) {
-                       Logger::info('Contact is not a follower, thread will not be stored', ['author' => $item['author-link'], 'uid' => $uid, 'uri-id' => $uri_id]);
+                       Logger::info('Contact is not a follower, thread will not be stored', ['author' => $item['author-link'], 'uid' => $uid, 'uri-id' => $uri_id, 'post-reason' => $item['post-reason']]);
                        return 0;
                }
 
@@ -1870,23 +1877,40 @@ class Item
                        return '';
                }
 
+               $languages = self::getLanguageArray(trim($item['title'] . "\n" . $item['body']), 3);
+               if (empty($languages)) {
+                       return '';
+               }
+
+               return json_encode($languages);
+       }
+
+       /**
+        * Get a language array from a given text
+        *
+        * @param string  $body
+        * @param integer $count
+        * @return array
+        */
+       public static function getLanguageArray(string $body, int $count): array
+       {
                // Convert attachments to links
-               $naked_body = BBCode::removeAttachment($item['body']);
+               $naked_body = BBCode::removeAttachment($body);
                if (empty($naked_body)) {
-                       return '';
+                       return [];
                }
 
                // Remove links and pictures
                $naked_body = BBCode::removeLinks($naked_body);
 
                // Convert the title and the body to plain text
-               $naked_body = trim($item['title'] . "\n" . BBCode::toPlaintext($naked_body));
+               $naked_body = BBCode::toPlaintext($naked_body);
 
                // Remove possibly remaining links
                $naked_body = preg_replace(Strings::autoLinkRegEx(), '', $naked_body);
 
                if (empty($naked_body)) {
-                       return '';
+                       return [];
                }
 
                $naked_body = self::getDominantLanguage($naked_body);
@@ -1898,12 +1922,7 @@ class Item
                $availableLanguages['fa'] = 'fa';
 
                $ld = new Language(array_keys($availableLanguages));
-               $languages = $ld->detect($naked_body)->limit(0, 3)->close();
-               if (is_array($languages)) {
-                       return json_encode($languages);
-               }
-
-               return '';
+               return $ld->detect($naked_body)->limit(0, $count)->close() ?: [];
        }
 
        /**
@@ -1916,7 +1935,7 @@ class Item
        {
                $latin = '';
                $non_latin = '';
-               for ($i = 0; $i < mb_strlen($body); $i++) { 
+               for ($i = 0; $i < mb_strlen($body); $i++) {
                        $character = mb_substr($body, $i, 1);
                        $ord = mb_ord($character);
 
@@ -3550,63 +3569,6 @@ class Item
                return BBCode::fetchShareAttributes($item['body']);
        }
 
-       /**
-        * Fetch item information for shared items from the original items and adds it.
-        *
-        * @param array $item
-        *
-        * @return array item array with data from the original item
-        */
-       public static function addShareDataFromOriginal(array $item): array
-       {
-               $shared = self::getShareArray($item);
-               if (empty($shared)) {
-                       return $item;
-               }
-
-               // Real reshares always have got a GUID.
-               if (empty($shared['guid'])) {
-                       return $item;
-               }
-
-               $uid = $item['uid'] ?? 0;
-
-               // first try to fetch the item via the GUID. This will work for all reshares that had been created on this system
-               $shared_item = Post::selectFirst(['title', 'body'], ['guid' => $shared['guid'], 'uid' => [0, $uid]]);
-               if (!DBA::isResult($shared_item)) {
-                       if (empty($shared['link'])) {
-                               return $item;
-                       }
-
-                       // Otherwhise try to find (and possibly fetch) the item via the link. This should work for Diaspora and ActivityPub posts
-                       $id = self::fetchByLink($shared['link'] ?? '', $uid);
-                       if (empty($id)) {
-                               Logger::info('Original item not found', ['url' => $shared['link'] ?? '', 'callstack' => System::callstack()]);
-                               return $item;
-                       }
-
-                       $shared_item = Post::selectFirst(['title', 'body'], ['id' => $id]);
-                       if (!DBA::isResult($shared_item)) {
-                               return $item;
-                       }
-                       Logger::info('Got shared data from url', ['url' => $shared['link'], 'callstack' => System::callstack()]);
-               } else {
-                       Logger::info('Got shared data from guid', ['guid' => $shared['guid'], 'callstack' => System::callstack()]);
-               }
-
-               if (!empty($shared_item['title'])) {
-                       $body = '[h3]' . $shared_item['title'] . "[/h3]\n" . $shared_item['body'];
-                       unset($shared_item['title']);
-               } else {
-                       $body = $shared_item['body'];
-               }
-
-               $item['body'] = preg_replace("/\[share ([^\[\]]*)\].*\[\/share\]/ism", '[share $1]' . str_replace('$', '\$', $body) . '[/share]', $item['body']);
-               unset($shared_item['body']);
-
-               return array_merge($item, $shared_item);
-       }
-
        /**
         * Check a prospective item array against user-level permissions
         *
@@ -3653,31 +3615,38 @@ class Item
        public static function improveSharedDataInBody(array $item): string
        {
                $shared = BBCode::fetchShareAttributes($item['body']);
-               if (empty($shared['link'])) {
+               if (empty($shared['guid']) && empty($shared['message_id'])) {
                        return $item['body'];
                }
 
-               $id = self::fetchByLink($shared['link']);
-               Logger::info('Fetched shared post', ['uri-id' => $item['uri-id'], 'id' => $id, 'author' => $shared['profile'], 'url' => $shared['link'], 'guid' => $shared['guid'], 'callstack' => System::callstack()]);
-               if (!$id) {
-                       return $item['body'];
-               }
+               $link = $shared['link'] ?: $shared['message_id'];
 
-               $shared_item = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'title', 'body'], ['id' => $id]);
-               if (!DBA::isResult($shared_item)) {
-                       return $item['body'];
+               if (empty($shared_content)) {
+                       $shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0);
                }
 
-               $shared_content = BBCode::getShareOpeningTag($shared_item['author-name'], $shared_item['author-link'], $shared_item['author-avatar'], $shared_item['plink'], $shared_item['created'], $shared_item['guid']);
-
-               if (!empty($shared_item['title'])) {
-                       $shared_content .= '[h3]'.$shared_item['title'].'[/h3]'."\n";
+               if (empty($shared_content)) {
+                       return $item['body'];
                }
 
-               $shared_content .= $shared_item['body'];
+               $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content, $item['body']);
 
-               $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content . '[/share]', $item['body']);
-               Logger::info('New shared data', ['uri-id' => $item['uri-id'], 'id' => $id, 'shared_item' => $shared_item]);
+               Logger::debug('New shared data', ['uri-id' => $item['uri-id'], 'link' => $link, 'guid' => $item['guid']]);
                return $item['body'];
        }
+
+       /**
+        * Fetch the uri-id of a quote
+        *
+        * @param string $body
+        * @return integer
+        */
+       private static function getQuoteUriId(string $body): int
+       {
+               $shared = BBCode::fetchShareAttributes($body);
+               if (empty($shared['message_id'])) {
+                       return 0;
+               }
+               return ItemURI::getIdByURI($shared['message_id']);
+       }
 }