]> git.mxchange.org Git - friendica.git/blobdiff - src/Factory/Api/Mastodon/Status.php
Only search for empty quoted reshares (Diaspora type)
[friendica.git] / src / Factory / Api / Mastodon / Status.php
index 4e1f790f500e109a20ec91100ea275ca364d0c5e..df5970d2982ac7043aa699e58fc1ad3d11d8521a 100644 (file)
@@ -23,7 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
 
 use Friendica\BaseFactory;
 use Friendica\Content\ContactSelector;
-use Friendica\Content\Text\BBCode;
+use Friendica\Content\Item as ContentItem;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\Model\Item;
@@ -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,20 +72,22 @@ class Status extends BaseFactory
                $this->mstdnAttachementFactory = $mstdnAttachementFactory;
                $this->mstdnErrorFactory       = $mstdnErrorFactory;
                $this->mstdnPollFactory        = $mstdnPollFactory;
+               $this->contentItem             = $contentItem;
        }
 
        /**
-        * @param int $uriId Uri-ID of the item
-        * @param int $uid   Item user
+        * @param int  $uriId  Uri-ID of the item
+        * @param int  $uid    Item user
+        * @param bool $reblog Check for reblogged post
         *
         * @return \Friendica\Object\Api\Mastodon\Status
         * @throws HTTPException\InternalServerErrorException
         * @throws ImagickException|HTTPException\NotFoundException
         */
-       public function createFromUriId(int $uriId, int $uid = 0): \Friendica\Object\Api\Mastodon\Status
+       public function createFromUriId(int $uriId, int $uid = 0, bool $reblog = true): \Friendica\Object\Api\Mastodon\Status
        {
-               $fields = ['uri-id', 'uid', 'author-id', 'author-uri-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id',
-                       'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media'];
+               $fields = ['uri-id', 'uid', 'author-id', 'causer-id', 'author-uri-id', 'author-link', 'causer-uri-id', 'post-reason', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id',
+                       'created', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id'];
                $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
                if (!$item) {
                        $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
@@ -91,20 +96,37 @@ class Status extends BaseFactory
                        }
                        throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
                }
-               $account = $this->mstdnAccountFactory->createFromUriId($item['author-uri-id'], $uid);
+
+               if (($item['gravity'] == Item::GRAVITY_ACTIVITY) && ($item['vid'] == Verb::getID(Activity::ANNOUNCE))) {
+                       $is_reshare = true;
+                       $account    = $this->mstdnAccountFactory->createFromUriId($item['author-uri-id'], $uid);
+                       $uriId      = $item['thr-parent-id'];
+                       $item       = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
+                       if (!$item) {
+                               throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
+                       }
+               } else {
+                       $is_reshare = $reblog && !is_null($item['causer-uri-id']) && ($item['causer-id'] != $item['author-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT);
+                       $account    = $this->mstdnAccountFactory->createFromUriId($is_reshare ? $item['causer-uri-id'] : $item['author-uri-id'], $uid);
+               }
 
                $count_announce = Post::countPosts([
                        'thr-parent-id' => $uriId,
                        'gravity'       => Item::GRAVITY_ACTIVITY,
                        'vid'           => Verb::getID(Activity::ANNOUNCE),
                        'deleted'       => false
-               ], []);
+               ]) + Post::countPosts([
+                       'quote-uri-id' => $uriId,
+                       'body'         => '',
+                       'deleted'      => false
+               ]);
+
                $count_like = Post::countPosts([
                        'thr-parent-id' => $uriId,
                        'gravity'       => Item::GRAVITY_ACTIVITY,
                        'vid'           => Verb::getID(Activity::LIKE),
                        'deleted'       => false
-               ], []);
+               ]);
 
                $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(
                        Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_COMMENT, 'deleted' => false], []),
@@ -127,6 +149,12 @@ class Status extends BaseFactory
                        'gravity'       => Item::GRAVITY_ACTIVITY,
                        'vid'           => Verb::getID(Activity::ANNOUNCE),
                        'deleted'       => false
+               ]) || Post::exists([
+                       'quote-uri-id' => $uriId,
+                       'uid'           => $uid,
+                       'origin'        => true,
+                       'body'          => '',
+                       'deleted'       => false
                ]);
                $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
                        $origin_like,
@@ -155,26 +183,41 @@ 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 = $this->contentItem->getSharedPost($item, ['uri-id']);
+               if (!empty($shared)) {
+                       $shared_uri_id = $shared['post']['uri-id'];
+
+                       foreach ($this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy() as $mention) {
+                               if (!in_array($mention, $mentions)) {
+                                       $mentions[] = $mention;
+                               }
+                       }
 
-                       $shared_uri_id = $shared_item['uri-id'] ?? 0;
+                       foreach ($this->mstdnTagFactory->createFromUriId($shared_uri_id) as $tag) {
+                               if (!in_array($tag, $tags)) {
+                                       $tags[] = $tag;
+                               }
+                       }
 
-                       $mentions    = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy());
-                       $tags        = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id));
-                       $attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id));
+                       foreach ($this->mstdnAttachementFactory->createFromUriId($shared_uri_id) as $attachment) {
+                               if (!in_array($attachment, $attachments)) {
+                                       $attachments[] = $attachment;
+                               }
+                       }
 
                        if (empty($card->toArray())) {
                                $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
                        }
                }
 
-               if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
-                       $reshare       = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
-                       $reshared_item = Post::selectFirst(['title', 'body'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]);
-                       $item['title'] = $reshared_item['title'] ?? $item['title'];
-                       $item['body']  = $reshared_item['body'] ?? $item['body'];
+               $item['body'] = $this->contentItem->addSharedPost($item);
+
+               if (!is_null($item['raw-body'])) {
+                       $item['raw-body'] = $this->contentItem->addSharedPost($item, $item['raw-body']);
+               }
+
+               if ($is_reshare) {
+                       $reshare = $this->createFromUriId($uriId, $uid, false)->toArray();
                } else {
                        $reshare = [];
                }