3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Object\Api\Mastodon;
24 use Friendica\BaseDataTransferObject;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Model\Item;
27 use Friendica\Object\Api\Mastodon\Status\Counts;
28 use Friendica\Object\Api\Mastodon\Status\UserAttributes;
29 use Friendica\Util\DateTimeFormat;
34 * @see https://docs.joinmastodon.org/entities/status
36 class Status extends BaseDataTransferObject
40 /** @var string (Datetime) */
41 protected $created_at;
42 /** @var string|null */
43 protected $in_reply_to_id = null;
44 /** @var string|null */
45 protected $in_reply_to_account_id = null;
47 protected $sensitive = false;
49 protected $spoiler_text = "";
50 /** @var string (Enum of public, unlisted, private, direct)*/
51 protected $visibility;
52 /** @var string|null */
53 protected $language = null;
56 /** @var string|null (URL)*/
57 protected $url = null;
59 protected $replies_count = 0;
61 protected $reblogs_count = 0;
63 protected $favourites_count = 0;
65 protected $favourited = false;
67 protected $reblogged = false;
69 protected $muted = false;
71 protected $bookmarked = false;
73 protected $pinned = false;
76 /** @var Status|null */
77 protected $reblog = null;
78 /** @var Status|null */
79 protected $quote = null;
80 /** @var Application */
81 protected $application = null;
84 /** @var Attachment */
85 protected $media_attachments = [];
87 protected $mentions = [];
91 protected $emojis = [];
93 protected $card = null;
95 protected $poll = null;
98 * Creates a status record from an item record.
101 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
103 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, array $quote = null, array $poll = null)
105 $this->id = (string)$item['uri-id'];
106 $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::JSON);
108 if ($item['gravity'] == Item::GRAVITY_COMMENT) {
109 $this->in_reply_to_id = (string)$item['thr-parent-id'];
110 $this->in_reply_to_account_id = (string)$item['parent-author-id'];
113 $this->sensitive = $sensitive;
114 $this->spoiler_text = $item['title'] ?: $item['content-warning'] ?: '';
116 $visibility = ['public', 'private', 'unlisted'];
117 $this->visibility = $visibility[$item['private']];
119 $languages = json_decode($item['language'] ?? '', true);
120 if (is_array($languages)) {
122 $this->language = key($languages);
124 $this->language = null;
127 $this->uri = $item['uri'];
128 $this->url = $item['plink'] ?? null;
129 $this->replies_count = $counts->replies;
130 $this->reblogs_count = $counts->reblogs;
131 $this->favourites_count = $counts->favourites;
132 $this->favourited = $userAttributes->favourited;
133 $this->reblogged = $userAttributes->reblogged;
134 $this->muted = $userAttributes->muted;
135 $this->bookmarked = $userAttributes->bookmarked;
136 $this->pinned = $userAttributes->pinned;
137 $this->content = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::MASTODON_API);
138 $this->reblog = $reblog;
139 $this->quote = $quote;
140 $this->application = $application->toArray();
141 $this->account = $account->toArray();
142 $this->media_attachments = $attachments;
143 $this->mentions = $mentions;
146 $this->card = $card->toArray() ?: null;
151 * Returns the current entity as an array
155 public function toArray(): array
157 $status = parent::toArray();
159 if (!$status['pinned']) {
160 unset($status['pinned']);
163 if (empty($status['application']['name'])) {
164 unset($status['application']);
167 if (empty($status['reblog'])) {
168 $status['reblog'] = null;
171 if (empty($status['quote'])) {
172 $status['quote'] = null;