]> git.mxchange.org Git - friendica.git/commitdiff
More functions moved to content class
authorMichael <heluecht@pirati.ca>
Thu, 27 Oct 2022 05:44:44 +0000 (05:44 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 27 Oct 2022 05:44:44 +0000 (05:44 +0000)
mod/item.php
src/Content/Item.php
src/Database/PostUpdate.php
src/Factory/Api/Mastodon/Status.php
src/Factory/Api/Twitter/Status.php
src/Model/Item.php
src/Model/Tag.php
src/Protocol/ActivityPub/Processor.php
src/Protocol/ActivityPub/Transmitter.php
src/Protocol/DFRN.php

index f30798fa389dafa6590070a9ba7275b3ee0ae15a..a0bab514ec7fdf63b1cea314d4facf9e667cc711 100644 (file)
@@ -611,7 +611,7 @@ function item_post(App $a) {
                $datarray['author-uri-id'] = ItemURI::getIdByURI($datarray['author-link']);
                $datarray['owner-updated'] = '';
                $datarray['has-media'] = false;
-               $datarray['body'] = Item::improveSharedDataInBody($datarray);
+               $datarray['body'] = DI::contentItem()->improveSharedDataInBody($datarray);
 
                $o = DI::conversation()->create([array_merge($contact_record, $datarray)], 'search', false, true);
 
@@ -652,7 +652,7 @@ function item_post(App $a) {
        }
 
        $datarray['uri-id'] = ItemURI::getIdByURI($datarray['uri']);
-       $datarray['body']   = Item::improveSharedDataInBody($datarray);
+       $datarray['body']   = DI::contentItem()->improveSharedDataInBody($datarray);
 
        if ($orig_post) {
                $fields = [
index f72493c17ef908530ee949f91ca666e76347f6cb..510bae97034fa0ae25fc9c65db04dea3d5d0068d 100644 (file)
@@ -680,11 +680,11 @@ class Item
                        $shared_content .= '[h3]' . $item['title'] . "[/h3]\n";
                }
 
-               $shared = $this->getSharedPost($item, ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network']);
+               $shared = $this->getShareArray($item);
 
                // If it is a reshared post then reformat it to avoid display problems with two share elements
                if (!empty($shared)) {
-                       if (($encaspulated_share = $this->createSharedBlockByArray($shared['post'], $add_media))) {
+                       if (!empty($shared['guid']) && ($encaspulated_share = $this->createSharedPostByGuid($shared['guid'], 0, '', $add_media))) {
                                $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
                        }
 
@@ -729,4 +729,71 @@ class Item
 
                return [];
        }
+
+       /**
+        * Improve the data in shared posts
+        *
+        * @param array $item
+        * @param bool  $add_media
+        * @return string body
+        */
+       public function improveSharedDataInBody(array $item, bool $add_media = false): string
+       {
+               $shared = BBCode::fetchShareAttributes($item['body']);
+               if (empty($shared['guid']) && empty($shared['message_id'])) {
+                       return $item['body'];
+               }
+
+               $link = $shared['link'] ?: $shared['message_id'];
+
+               if (empty($shared_content)) {
+                       $shared_content = $this->createSharedPostByUrl($link, $item['uid'] ?? 0, $add_media);
+               }
+
+               if (empty($shared_content)) {
+                       return $item['body'];
+               }
+
+               $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content, $item['body']);
+
+               Logger::debug('New shared data', ['uri-id' => $item['uri-id'], 'link' => $link, 'guid' => $item['guid']]);
+               return $item['body'];
+       }
+
+       /**
+        * 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
+        */
+       private 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 [];
+       }
 }
index 04c218ca001896f7b7986b3985441c5805a5a953..adc88b13e82ec425edc0995724bc3a9bf04ef6bc 100644 (file)
@@ -359,7 +359,7 @@ class PostUpdate
                 }
                        }
 
-                       Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false);
+                       Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url']);
 
                        $id = $term['tid'];
                        ++$rows;
index caa219a237be235fc54011ce93d6dbd4dc76749e..9ff22119079ad15ee29876011e2c1ce8e50c72f6 100644 (file)
@@ -23,9 +23,9 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\BaseFactory;
 use Friendica\Content\ContactSelector;
+use Friendica\Content\Item as ContentItem;
 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;
@@ -54,11 +54,14 @@ class Status extends BaseFactory
        private $mstdnErrorFactory;
        /** @var Poll */
        private $mstdnPollFactory;
+       /** @var ContentItem */
+       private $contentItem;
 
        public function __construct(LoggerInterface $logger, Database $dba,
                Account $mstdnAccountFactory, Mention $mstdnMentionFactory,
                Tag $mstdnTagFactory, Card $mstdnCardFactory,
-               Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory, Poll $mstdnPollFactory)
+               Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory,
+               Poll $mstdnPollFactory, ContentItem $contentItem)
        {
                parent::__construct($logger);
                $this->dba                     = $dba;
@@ -69,6 +72,7 @@ class Status extends BaseFactory
                $this->mstdnAttachementFactory = $mstdnAttachementFactory;
                $this->mstdnErrorFactory       = $mstdnErrorFactory;
                $this->mstdnPollFactory        = $mstdnPollFactory;
+               $this->contentItem             = $contentItem;
        }
 
        /**
@@ -155,7 +159,7 @@ class Status extends BaseFactory
                        $poll = null;
                }
 
-               $shared = DI::contentItem()->getSharedPost($item, ['uri-id']);
+               $shared = $this->contentItem->getSharedPost($item, ['uri-id']);
                if (!empty($shared)) {
                        $shared_uri_id = $shared['post']['uri-id'];
 
index 33abfdbcc5d2fc4613136ad5873b1e09b4743aa7..55f843ed28e546ac86f755940c5388ddf4209408 100644 (file)
 namespace Friendica\Factory\Api\Twitter;
 
 use Friendica\BaseFactory;
+use Friendica\Content\Item as ContentItem;
 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;
@@ -54,8 +54,10 @@ class Status extends BaseFactory
        private $activities;
        /** @var Activities entity */
        private $attachment;
+       /** @var ContentItem */
+       private $contentItem;
 
-       public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities, Attachment $attachment)
+       public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities, Attachment $attachment, ContentItem $contentItem)
        {
                parent::__construct($logger);
                $this->dba         = $dba;
@@ -66,6 +68,7 @@ class Status extends BaseFactory
                $this->mention     = $mention;
                $this->activities  = $activities;
                $this->attachment  = $attachment;
+               $this->contentItem = $contentItem;
        }
 
        /**
@@ -179,7 +182,7 @@ class Status extends BaseFactory
 
                $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid);
 
-               $shared = DI::contentItem()->getSharedPost($item, ['uri-id']);
+               $shared = $this->contentItem->getSharedPost($item, ['uri-id']);
                if (!empty($shared)) {
                        $shared_uri_id = $shared['post']['uri-id'];
 
index ced18fc8a68f74aed8557da4522580aa8eb2c73d..e1e4883da5e0d05eb5bb869ca92f899b38fdd3d7 100644 (file)
@@ -3623,36 +3623,6 @@ class Item
                return true;
        }
 
-       /**
-        * Improve the data in shared posts
-        *
-        * @param array $item
-        * @param bool  $add_media
-        * @return string body
-        */
-       public static function improveSharedDataInBody(array $item, bool $add_media = false): string
-       {
-               $shared = BBCode::fetchShareAttributes($item['body']);
-               if (empty($shared['guid']) && empty($shared['message_id'])) {
-                       return $item['body'];
-               }
-
-               $link = $shared['link'] ?: $shared['message_id'];
-
-               if (empty($shared_content)) {
-                       $shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0, $add_media);
-               }
-
-               if (empty($shared_content)) {
-                       return $item['body'];
-               }
-
-               $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content, $item['body']);
-
-               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
         *
index 6611245a0c69ff3268ddb2740ceeef39eb2b58e8..2f034e92b7767846b005a0fea7697d38f7a8b8e5 100644 (file)
@@ -257,17 +257,16 @@ class Tag
         * @param string $hash
         * @param string $name
         * @param string $url
-        * @param boolean $probing Whether probing is active
         * @return void
         */
-       public static function storeByHash(int $uriId, string $hash, string $name, string $url = '', bool $probing = true)
+       public static function storeByHash(int $uriId, string $hash, string $name, string $url = '')
        {
                $type = self::getTypeForHash($hash);
                if ($type == self::UNKNOWN) {
                        return;
                }
 
-               self::store($uriId, $type, $name, $url, $probing);
+               self::store($uriId, $type, $name, $url);
        }
 
        /**
@@ -297,32 +296,43 @@ class Tag
         * @param integer $uriId   URI-Id
         * @param string  $body    Body of the post
         * @param string  $tags    Accepted tags
-        * @param boolean $probing Perform a probing for contacts, adding them if needed
         * @return void
         */
-       public static function storeFromBody(int $uriId, string $body, string $tags = null, bool $probing = true)
+       public static function storeFromBody(int $uriId, string $body, string $tags = null)
        {
-               Logger::info('Check for tags', ['uri-id' => $uriId, 'hash' => $tags, 'callstack' => System::callstack()]);
+               $item = ['uri-id' => $uriId, 'body' => $body, 'quote-uri-id' => null];
+               self::storeFromArray($item, $tags);
+       }
+
+       /**
+        * Store tags and mentions from the item array
+        *
+        * @param array   $item    Item array
+        * @param string  $tags    Accepted tags
+        * @return void
+        */
+       public static function storeFromArray(array $item, string $tags = null)
+       {
+               Logger::info('Check for tags', ['uri-id' => $item['uri-id'], 'hash' => $tags, 'callstack' => System::callstack()]);
 
                if (is_null($tags)) {
                        $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
                }
 
                // Only remove the shared data from "real" reshares
-               $shared = BBCode::fetchShareAttributes($body);
-               if (!empty($shared['guid'])) {
-                       $share_body = $shared['shared'];
-                       $body = BBCode::removeSharedData($body);
+               $shared = DI::contentItem()->getSharedPost($item, ['uri-id']);
+               if (!empty($shared)) {
+                       $item['body'] = BBCode::removeSharedData($item['body']);
                }
 
-               foreach (self::getFromBody($body, $tags) as $tag) {
-                       self::storeByHash($uriId, $tag[1], $tag[3], $tag[2], $probing);
+               foreach (self::getFromBody($item['body'], $tags) as $tag) {
+                       self::storeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]);
                }
 
                // Search for hashtags in the shared body (but only if hashtags are wanted)
-               if (!empty($share_body) && (strpos($tags, self::TAG_CHARACTER[self::HASHTAG]) !== false)) {
-                       foreach (self::getFromBody($share_body, self::TAG_CHARACTER[self::HASHTAG]) as $tag) {
-                               self::storeByHash($uriId, $tag[1], $tag[3], $tag[2], $probing);
+               if (!empty($shared) && (strpos($tags, self::TAG_CHARACTER[self::HASHTAG]) !== false)) {
+                       foreach (self::getByURIId($shared['post']['uri-id'], [self::HASHTAG]) as $tag) {
+                               self::store($item['uri-id'], $tag['type'], $tag['name'], $tag['url']);
                        }
                }
        }
index 79626abbb8efcab4e372b70a30e2dc956f605374..38361ec06951c26c66f6abc99091347c89626b01 100644 (file)
@@ -830,7 +830,7 @@ class Processor
                if (!empty($activity['source'])) {
                        $item['body'] = $activity['source'];
                        $item['raw-body'] = $content;
-                       $item['body'] = Item::improveSharedDataInBody($item);
+                       $item['body'] = DI::contentItem()->improveSharedDataInBody($item);
                } else {
                        $parent_uri = $item['parent-uri'] ?? $item['thr-parent'];
                        if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == Item::GRAVITY_COMMENT)) {
index 7cb1eecf50507fe30bf182189bab3ed21e6cb71d..afc2d5eac047d5c08b2ec460d38d13b388628598 100644 (file)
@@ -1677,7 +1677,7 @@ class Transmitter
                                $data['quoteUrl'] = $item['quote-uri'];
                        } 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);
+                               $item['body'] = DI::contentItem()->improveSharedDataInBody($item, true);
                        }
 
                        $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
index 8fe3c5556ef6b5b3ef65ca43321b3118b412ecf8..f626582babf17321fc4b1ca7338b59d76bfe411e 100644 (file)
@@ -1838,7 +1838,7 @@ class DFRN
 
                $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
 
-               $item['body'] = Item::improveSharedDataInBody($item);
+               $item['body'] = DI::contentItem()->improveSharedDataInBody($item);
 
                Tag::storeFromBody($item['uri-id'], $item['body']);