]> 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 3df4e28e7bec80a9815a0b782637dffee6bfb977..df5970d2982ac7043aa699e58fc1ad3d11d8521a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Factory\Api\Mastodon;
 
-use Friendica\App\BaseURL;
 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\DI;
 use Friendica\Model\Item;
 use Friendica\Model\Post;
+use Friendica\Model\Tag as TagModel;
 use Friendica\Model\Verb;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\Activity;
-use Friendica\Repository\ProfileField;
+use Friendica\Protocol\ActivityPub;
+use ImagickException;
 use Psr\Log\LoggerInterface;
 
 class Status extends BaseFactory
 {
-       /** @var BaseURL */
-       protected $baseUrl;
-       /** @var ProfileField */
-       protected $profileField;
-       /** @var Field */
-       protected $mstdnField;
-
-       public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
+       /** @var Database */
+       private $dba;
+       /** @var Account */
+       private $mstdnAccountFactory;
+       /** @var Mention */
+       private $mstdnMentionFactory;
+       /** @var Tag */
+       private $mstdnTagFactory;
+       /** @var Card */
+       private $mstdnCardFactory;
+       /** @var Attachment */
+       private $mstdnAttachementFactory;
+       /** @var Error */
+       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, ContentItem $contentItem)
        {
                parent::__construct($logger);
-
-               $this->baseUrl = $baseURL;
-               $this->profileField = $profileField;
-               $this->mstdnField = $mstdnField;
+               $this->dba                     = $dba;
+               $this->mstdnAccountFactory     = $mstdnAccountFactory;
+               $this->mstdnMentionFactory     = $mstdnMentionFactory;
+               $this->mstdnTagFactory         = $mstdnTagFactory;
+               $this->mstdnCardFactory        = $mstdnCardFactory;
+               $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
+        * @throws ImagickException|HTTPException\NotFoundException
         */
-       public function createFromUriId(int $uriId, $uid = 0)
+       public function createFromUriId(int $uriId, int $uid = 0, bool $reblog = true): \Friendica\Object\Api\Mastodon\Status
        {
-               $fields = ['uri-id', 'uid', 'author-id', 'author-link', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network',
-                       'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity'];
+               $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) {
-                       throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . 'not found' . ($uid ? ' for user ' . $uid : '.'));
+                       $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]);
+                       if ($mail) {
+                               return $this->createFromMailId($mail['id']);
+                       }
+                       throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
                }
 
-               $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
+               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::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_COMMENT, 'deleted' => false], [], false),
-                       Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE), 'deleted' => false], [], false),
-                       Post::count(['thr-parent-id' => $uriId, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE), 'deleted' => false], [], false)
+                       Post::countPosts(['thr-parent-id' => $uriId, 'gravity' => Item::GRAVITY_COMMENT, 'deleted' => false], []),
+                       $count_announce,
+                       $count_like
                );
 
+               $origin_like = ($count_like == 0) ? false : Post::exists([
+                       'thr-parent-id' => $uriId,
+                       'uid'           => $uid,
+                       'origin'        => true,
+                       'gravity'       => Item::GRAVITY_ACTIVITY,
+                       'vid'           => Verb::getID(Activity::LIKE),
+                       'deleted'     => false
+               ]);
+               $origin_announce = ($count_announce == 0) ? false : Post::exists([
+                       'thr-parent-id' => $uriId,
+                       'uid'           => $uid,
+                       'origin'        => true,
+                       '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(
-                       Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE), 'deleted' => false]),
-                       Post::exists(['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE), 'deleted' => false]),
+                       $origin_like,
+                       $origin_announce,
                        Post\ThreadUser::getIgnored($uriId, $uid),
-                       (bool)$item['starred'],
-                       Post\ThreadUser::getPinned($uriId, $uid)
+                       (bool)($item['starred'] && ($item['gravity'] == Item::GRAVITY_PARENT)),
+                       $item['featured']
                );
 
-               $sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
+               $sensitive   = $this->dba->exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw', 'type' => TagModel::HASHTAG]);
                $application = new \Friendica\Object\Api\Mastodon\Application($item['app'] ?: ContactSelector::networkToName($item['network'], $item['author-link']));
 
-               $mentions    = DI::mstdnMention()->createFromUriId($uriId);
-               $tags        = DI::mstdnTag()->createFromUriId($uriId);
-               $card        = DI::mstdnCard()->createFromUriId($uriId);
-               $attachments = DI::mstdnAttachment()->createFromUriId($uriId);
+               $mentions    = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy();
+               $tags        = $this->mstdnTagFactory->createFromUriId($uriId);
+               if ($item['has-media']) {
+                       $card        = $this->mstdnCardFactory->createFromUriId($uriId);
+                       $attachments = $this->mstdnAttachementFactory->createFromUriId($uriId);
+               } else {
+                       $card        = new \Friendica\Object\Api\Mastodon\Card([]);
+                       $attachments = [];
+               }
+
+               if (!empty($item['question-id'])) {
+                       $poll = $this->mstdnPollFactory->createFromId($item['question-id'], $uid)->toArray();
+               } else {
+                       $poll = null;
+               }
+
+               $shared = $this->contentItem->getSharedPost($item, ['uri-id']);
+               if (!empty($shared)) {
+                       $shared_uri_id = $shared['post']['uri-id'];
 
-               $shared = BBCode::fetchShareAttributes($item['body']);
-               if (!empty($shared['guid'])) {
-                       $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
+                       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, DI::mstdnMention()->createFromUriId($shared_uri_id));
-                       $tags        = array_merge($tags, DI::mstdnTag()->createFromUriId($shared_uri_id));
-                       $attachments = array_merge($attachments, DI::mstdnAttachment()->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 = DI::mstdnCard()->createFromUriId($shared_uri_id);
+                               $card = $this->mstdnCardFactory->createFromUriId($shared_uri_id);
                        }
                }
 
+               $item['body'] = $this->contentItem->addSharedPost($item);
 
-               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'];
+               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 = [];
                }
 
-               return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
+               return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare, $poll);
        }
 
        /**
         * @param int $uriId id of the mail
-        * @param int $uid   mail user
+        *
         * @return \Friendica\Object\Api\Mastodon\Status
         * @throws HTTPException\InternalServerErrorException
-        * @throws \ImagickException
+        * @throws ImagickException|HTTPException\NotFoundException
         */
-       public function createFromMailId(int $id, $uid = 0)
+       public function createFromMailId(int $id): \Friendica\Object\Api\Mastodon\Status
        {
-               $mail = DBA::selectFirst('mail', [], ['id' => $id, 'uid' => $uid]);
-               if (!$mail) {
-                       DI::mstdnError()->RecordNotFound();
+               $item = ActivityPub\Transmitter::getItemArrayFromMail($id, true);
+               if (empty($item)) {
+                       $this->mstdnErrorFactory->RecordNotFound();
                }
 
-               $conv = DBA::selectFirst('conv', ['subject'], ['id' => $mail['convid'], 'uid' => $uid]);
-               if (!$conv) {
-                       DI::mstdnError()->RecordNotFound();
-               }
+               $account = $this->mstdnAccountFactory->createFromContactId($item['author-id']);
 
-               $account = DI::mstdnAccount()->createFromContactId($mail['contact-id']);
+               $replies = $this->dba->count('mail', ['thr-parent-id' => $item['uri-id'], 'reply' => true]);
 
-               $counts = new \Friendica\Object\Api\Mastodon\Status\Counts(0, 0, 0);
+               $counts = new \Friendica\Object\Api\Mastodon\Status\Counts($replies, 0, 0);
 
                $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(false, false, false, false, false);
 
@@ -154,19 +255,6 @@ class Status extends BaseFactory
                $attachments = [];
                $reshare     = [];
 
-               $item = [
-                       'uri-id'           => $mail['id'],
-                       'created'          => $mail['created'],
-                       'thr-parent-id'    => 0,
-                       'parent-author-id' => 0,
-                       'title'            => $conv['subject'],
-                       'private'          => Item::PRIVATE,
-                       'language'         => '',
-                       'uri'              => $mail['uri'],
-                       'plink'            => '',
-                       'body'             => BBCode::convert($mail['body'], false, BBCode::API)
-               ];
-
                return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $reshare);
        }
 }