]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Item.php
Merge pull request #12017 from annando/gravity
[friendica.git] / src / Content / Item.php
index 7a86135f9fe3f1755ac0b94a364d10964437015a..b363ad982bfefcf498a7ec10e3d9d73dc41b804d 100644 (file)
@@ -31,7 +31,7 @@ use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
-use Friendica\Model\Item as ModelItem;
+use Friendica\Model\Item as ItemModel;
 use Friendica\Model\Photo;
 use Friendica\Model\Tag;
 use Friendica\Model\Post;
@@ -331,7 +331,7 @@ class Item
                $sub_link = $contact_url = $pm_url = $status_link = '';
                $photos_link = $posts_link = $block_link = $ignore_link = '';
 
-               if (local_user() && local_user() == $item['uid'] && $item['gravity'] == GRAVITY_PARENT && !$item['self'] && !$item['mention']) {
+               if (local_user() && local_user() == $item['uid'] && $item['gravity'] == ItemModel::GRAVITY_PARENT && !$item['self'] && !$item['mention']) {
                        $sub_link = 'javascript:doFollowThread(' . $item['id'] . '); return false;';
                }
 
@@ -392,7 +392,7 @@ class Item
                        ];
 
                        if (!empty($item['language'])) {
-                               $menu[$this->l10n->t('Languages')] = 'javascript:alert(\'' . ModelItem::getLanguageMessage($item) . '\');';
+                               $menu[$this->l10n->t('Languages')] = 'javascript:alert(\'' . ItemModel::getLanguageMessage($item) . '\');';
                        }
 
                        if ((($cid == 0) || ($rel == Contact::FOLLOWER)) &&
@@ -470,7 +470,7 @@ class Item
                        }
                        $item['inform'] .= 'cid:' . $contact['id'];
 
-                       if (($item['gravity'] == GRAVITY_COMMENT) || empty($contact['cid']) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY)) {
+                       if (($item['gravity'] == ItemModel::GRAVITY_COMMENT) || empty($contact['cid']) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY)) {
                                continue;
                        }
 
@@ -492,9 +492,9 @@ class Item
                }
                Logger::info('Got inform', ['inform' => $item['inform']]);
 
-               if (($item['gravity'] == GRAVITY_PARENT) && !empty($forum_contact) && ($private_forum || $only_to_forum)) {
+               if (($item['gravity'] == ItemModel::GRAVITY_PARENT) && !empty($forum_contact) && ($private_forum || $only_to_forum)) {
                        // we tagged a forum in a top level post. Now we change the post
-                       $item['private'] = $private_forum ? ModelItem::PRIVATE : ModelItem::UNLISTED;
+                       $item['private'] = $private_forum ? ItemModel::PRIVATE : ItemModel::UNLISTED;
 
                        if ($only_to_forum) {
                                $item['postopts'] = '';
@@ -510,14 +510,14 @@ class Item
                                $item['allow_cid'] = '';
                                $item['allow_gid'] = '';
                        }
-               } elseif ($setPermissions && ($item['gravity'] == GRAVITY_PARENT)) {
+               } elseif ($setPermissions && ($item['gravity'] == ItemModel::GRAVITY_PARENT)) {
                        if (empty($receivers)) {
                                // For security reasons direct posts without any receiver will be posts to yourself
                                $self = Contact::selectFirst(['id'], ['uid' => $item['uid'], 'self' => true]);
                                $receivers[] = $self['id'];
                        }
 
-                       $item['private']   = ModelItem::PRIVATE;
+                       $item['private']   = ItemModel::PRIVATE;
                        $item['allow_cid'] = '';
                        $item['allow_gid'] = '';
                        $item['deny_cid']  = '';
@@ -574,16 +574,17 @@ 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);
+                       $id = ItemModel::searchByLink($url, $uid);
                }
 
                if (empty($id)) {
-                       $id = ModelItem::fetchByLink($url);
+                       $id = ItemModel::fetchByLink($url);
                }
 
                if (!$id) {
@@ -599,7 +600,7 @@ class Item
                        return '';
                }
 
-               return $this->createSharedBlockByArray($shared_item);
+               return $this->createSharedBlockByArray($shared_item, $add_media);
        }
 
        /**
@@ -607,18 +608,19 @@ class Item
         *
         * @param integer $UriId
         * @param integer $uid
+        * @param bool $add_media
         * @return string
         */
-       public function createSharedPostByUriId(int $UriId, int $uid = 0): 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]]);
+               $shared_item = Post::selectFirst($fields, ['uri-id' => $UriId, 'uid' => [$uid, 0], 'private' => [ItemModel::PUBLIC, ItemModel::UNLISTED]]);
                if (!DBA::isResult($shared_item)) {
                        Logger::notice('Post does not exist.', ['uri-id' => $UriId, 'uid' => $uid]);
                        return '';
                }
 
-               return $this->createSharedBlockByArray($shared_item);
+               return $this->createSharedBlockByArray($shared_item, $add_media);
        }
 
        /**
@@ -626,16 +628,17 @@ class Item
         *
         * @param string $guid
         * @param integer $uid
+        * @param bool $add_media
         * @return string
         */
-       public function createSharedPostByGuid(string $guid, int $uid = 0, string $host = ''): 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]]);
+               $shared_item = Post::selectFirst($fields, ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [ItemModel::PUBLIC, ItemModel::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]]);
+                       $shared_item = Post::selectFirst($fields, ['guid' => $guid, 'uid' => [$uid, 0], 'private' => [ItemModel::PUBLIC, ItemModel::UNLISTED]]);
                } elseif (DBA::isResult($shared_item)) {
                        Logger::debug('Found existing post', ['guid' => $guid, 'host' => $host, 'uid' => $uid]);
                }
@@ -645,19 +648,26 @@ class Item
                        return '';
                }
 
-               return $this->createSharedBlockByArray($shared_item);
+               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): string
+       public function createSharedBlockByArray(array $item, bool $add_media = false): string
        {
-               if (!in_array($item['network'] ?? '', Protocol::FEDERATED)) {
+               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']);
@@ -668,17 +678,12 @@ class Item
 
                $shared = BBCode::fetchShareAttributes($item['body']);
 
-               $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $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']);
-                               if (!empty($encaspulated_share)) {
-                                       $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']);
-                               }
+                       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));
                }