]> git.mxchange.org Git - friendica.git/blobdiff - src/Object/Api/Mastodon/Status.php
Merge pull request #10359 from annando/milliseconds
[friendica.git] / src / Object / Api / Mastodon / Status.php
index 5ebddf469162f3e64ba0abb5ecfc01c6745c1aa2..0985c0002a9fc9a3fbd96be530e13cd1aa6137d8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Object\Api\Mastodon;
 
-use Friendica\BaseEntity;
+use Friendica\BaseDataTransferObject;
 use Friendica\Content\Text\BBCode;
+use Friendica\Object\Api\Mastodon\Status\Counts;
+use Friendica\Object\Api\Mastodon\Status\UserAttributes;
 use Friendica\Util\DateTimeFormat;
 
 /**
@@ -30,7 +32,7 @@ use Friendica\Util\DateTimeFormat;
  *
  * @see https://docs.joinmastodon.org/entities/status
  */
-class Status extends BaseEntity
+class Status extends BaseDataTransferObject
 {
        /** @var string */
        protected $id;
@@ -95,42 +97,73 @@ class Status extends BaseEntity
         * @param array   $item
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function __construct(array $item, Account $account)
+       public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $reblog)
        {
                $this->id         = (string)$item['uri-id'];
-               $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
+               $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::JSON);
 
                if ($item['gravity'] == GRAVITY_COMMENT) {
                        $this->in_reply_to_id         = (string)$item['thr-parent-id'];
                        $this->in_reply_to_account_id = (string)$item['parent-author-id'];
                }
 
-               $this->sensitive = false;
+               $this->sensitive = $sensitive;
                $this->spoiler_text = $item['title'];
 
                $visibility = ['public', 'private', 'unlisted'];
                $this->visibility = $visibility[$item['private']];
 
-               $this->language = null;
+               $languages = json_decode($item['language'], true);
+               if (is_array($languages)) {
+                       reset($languages);
+                       $this->language = key($languages);
+               } else {
+                       $this->language = null;
+               }
+
                $this->uri = $item['uri'];
                $this->url = $item['plink'] ?? null;
-               $this->replies_count = 0;
-               $this->reblogs_count = 0;
-               $this->favourites_count = 0;
-               $this->favourited = false;
-               $this->reblogged = false;
-               $this->muted = false;
-               $this->bookmarked = false;
-               $this->pinned = false;
-               $this->content = BBCode::convert($item['body'], false);
-               $this->reblog = null;
-               $this->application = null;
+               $this->replies_count = $counts->replies;
+               $this->reblogs_count = $counts->reblogs;
+               $this->favourites_count = $counts->favourites;
+               $this->favourited = $userAttributes->favourited;
+               $this->reblogged = $userAttributes->reblogged;
+               $this->muted = $userAttributes->muted;
+               $this->bookmarked = $userAttributes->bookmarked;
+               $this->pinned = $userAttributes->pinned;
+               $this->content = BBCode::convert($item['raw-body'] ?? $item['body'], false, BBCode::API);
+               $this->reblog = $reblog;
+               $this->application = $application->toArray();
                $this->account = $account->toArray();
-               $this->media_attachments = [];
-               $this->mentions = [];
-               $this->tags = [];
+               $this->media_attachments = $attachments;
+               $this->mentions = $mentions;
+               $this->tags = $tags;
                $this->emojis = [];
-               $this->card = null;
+               $this->card = $card->toArray() ?: null;
                $this->poll = null;
        }
+
+       /**
+        * Returns the current entity as an array
+        *
+        * @return array
+        */
+       public function toArray(): array
+       {
+               $status = parent::toArray();
+
+               if (!$status['pinned']) {
+                       unset($status['pinned']);
+               }
+
+               if (empty($status['application']['name'])) {
+                       unset($status['application']);
+               }
+
+               if (empty($status['reblog'])) {
+                       $status['reblog'] = null;
+               }
+
+               return $status;
+       }
 }