]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Item.php
Merge remote-tracking branch 'upstream/develop' into linked-posts
[friendica.git] / src / Content / Item.php
index ca60139eaf9982dde17c57b00007753b93256e61..bfa107325c1730d7c5e070d2922906229d9f06ab 100644 (file)
 namespace Friendica\Content;
 
 use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\HTML;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
+use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
@@ -34,6 +36,7 @@ use Friendica\Model\Photo;
 use Friendica\Model\Tag;
 use Friendica\Model\Post;
 use Friendica\Protocol\Activity;
+use Friendica\Protocol\Diaspora;
 use Friendica\Util\Profiler;
 use Friendica\Util\Proxy;
 use Friendica\Util\XML;
@@ -565,4 +568,127 @@ class Item
 
                return $owner_thumb;
        }
+
+       /**
+        * Add a share block for the given url
+        *
+        * @param string $url
+        * @param integer $uid
+        * @param bool $add_media
+        * @return string
+        */
+       public function createSharedPostByUrl(string $url, int $uid = 0, bool $add_media = false): string
+       {
+               if (!empty($uid)) {
+                       $id = ModelItem::searchByLink($url, $uid);
+               }
+
+               if (empty($id)) {
+                       $id = ModelItem::fetchByLink($url);
+               }
+
+               if (!$id) {
+                       Logger::notice('Post could not be fetched.', ['url' => $url, 'uid' => $uid, 'callstack' => System::callstack()]);
+                       return '';
+               }
+
+               Logger::debug('Fetched shared post', ['id' => $id, 'url' => $url, 'uid' => $uid, 'callstack' => System::callstack()]);
+
+               $shared_item = Post::selectFirst(['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'], ['id' => $id]);
+               if (!DBA::isResult($shared_item)) {
+                       Logger::warning('Post does not exist.', ['id' => $id, 'url' => $url, 'uid' => $uid]);
+                       return '';
+               }
+
+               return $this->createSharedBlockByArray($shared_item, $add_media);
+       }
+
+       /**
+        * Add a share block for the given uri-id
+        *
+        * @param integer $UriId
+        * @param integer $uid
+        * @param bool $add_media
+        * @return string
+        */
+       public function createSharedPostByUriId(int $UriId, int $uid = 0, bool $add_media = false): string
+       {
+               $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'];
+               $shared_item = Post::selectFirst($fields, ['uri-id' => $UriId, 'uid' => [$uid, 0], 'private' => [ModelItem::PUBLIC, ModelItem::UNLISTED]]);
+               if (!DBA::isResult($shared_item)) {
+                       Logger::notice('Post does not exist.', ['uri-id' => $UriId, 'uid' => $uid]);
+                       return '';
+               }
+
+               return $this->createSharedBlockByArray($shared_item, $add_media);
+       }
+
+       /**
+        * Add a share block for the given guid
+        *
+        * @param string $guid
+        * @param integer $uid
+        * @param bool $add_media
+        * @return string
+        */
+       public function createSharedPostByGuid(string $guid, int $uid = 0, string $host = '', bool $add_media = false): string
+       {
+               $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'];
+               $shared_item = Post::selectFirst($fields, ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [ModelItem::PUBLIC, ModelItem::UNLISTED]]);
+
+               if (!DBA::isResult($shared_item) && !empty($host) && Diaspora::storeByGuid($guid, $host, true)) {
+                       Logger::debug('Fetched post', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
+                       $shared_item = Post::selectFirst($fields, ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [ModelItem::PUBLIC, ModelItem::UNLISTED]]);
+               } elseif (DBA::isResult($shared_item)) {
+                       Logger::debug('Found existing post', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
+               }
+
+               if (!DBA::isResult($shared_item)) {
+                       Logger::notice('Post does not exist.', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
+                       return '';
+               }
+
+               return $this->createSharedBlockByArray($shared_item, $add_media);
+       }
+
+       /**
+        * Add a share block for the given item array
+        *
+        * @param array $item
+        * @param bool $add_media
+        * @return string
+        */
+       public function createSharedBlockByArray(array $item, bool $add_media = false): string
+       {
+               if ($item['network'] == Protocol::FEED) {
+                       return PageInfo::getFooterFromUrl($item['plink']);
+               } elseif (!in_array($item['network'] ?? '', Protocol::FEDERATED)) {
+                       $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']);
+
+               if (!empty($item['title'])) {
+                       $shared_content .= '[h3]' . $item['title'] . "[/h3]\n";
+               }
+
+               $shared = BBCode::fetchShareAttributes($item['body']);
+
+               // If it is a reshared post then reformat it to avoid display problems with two share elements
+               if (Diaspora::isReshare($item['body'], false)) {
+                       if (!empty($shared['guid']) && ($encaspulated_share = self::createSharedPostByGuid($shared['guid'], 0, '', $add_media))) {
+                               $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
+                       }
+
+                       $item['body'] = HTML::toBBCode(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::ACTIVITYPUB));
+               }
+
+               $shared_content .= $item['body'] . '[/share]';
+
+               return $shared_content;
+       }
 }