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\FriendicaExtension;
29 use Friendica\Object\Api\Mastodon\Status\UserAttributes;
30 use Friendica\Util\DateTimeFormat;
35 * @see https://docs.joinmastodon.org/entities/status
37 class Status extends BaseDataTransferObject
41 /** @var string|null (Datetime) */
42 protected $created_at;
43 /** @var string|null (Datetime) */
45 /** @var string|null */
46 protected $in_reply_to_id = null;
47 /** @var Status|null - Fedilab extension, see issue https://github.com/friendica/friendica/issues/12672 */
48 protected $in_reply_to_status = null;
49 /** @var string|null */
50 protected $in_reply_to_account_id = null;
52 protected $sensitive = false;
54 protected $spoiler_text = "";
55 /** @var string (Enum of public, unlisted, private, direct)*/
56 protected $visibility;
57 /** @var string|null */
58 protected $language = null;
61 /** @var string|null (URL)*/
62 protected $url = null;
64 protected $replies_count = 0;
66 protected $reblogs_count = 0;
68 protected $favourites_count = 0;
70 protected $favourited = false;
72 protected $reblogged = false;
74 protected $muted = false;
76 protected $bookmarked = false;
78 protected $pinned = false;
81 /** @var Status|null */
82 protected $reblog = null;
83 /** @var Status|null - Akkoma extension, see issue https://github.com/friendica/friendica/issues/12603 */
84 protected $quote = null;
85 /** @var Application */
86 protected $application = null;
89 /** @var Attachment */
90 protected $media_attachments = [];
92 protected $mentions = [];
96 protected $emojis = [];
98 protected $card = null;
100 protected $poll = null;
101 /** @var FriendicaExtension */
102 protected $friendica;
105 * Creates a status record from an item record.
108 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
110 public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $in_reply, array $reblog, FriendicaExtension $friendica, array $quote = null, array $poll = null)
112 $this->id = (string)$item['uri-id'];
113 $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::JSON);
114 $this->edited_at = DateTimeFormat::utc($item['edited'], DateTimeFormat::JSON);
116 if ($item['gravity'] == Item::GRAVITY_COMMENT) {
117 $this->in_reply_to_id = (string)$item['thr-parent-id'];
118 $this->in_reply_to_status = $in_reply;
119 $this->in_reply_to_account_id = (string)$item['parent-author-id'];
122 $this->sensitive = $sensitive;
123 $this->spoiler_text = $item['title'] ?: $item['content-warning'] ?: '';
125 $visibility = ['public', 'private', 'unlisted'];
126 $this->visibility = $visibility[$item['private']];
128 $languages = json_decode($item['language'] ?? '', true);
129 if (is_array($languages)) {
131 $this->language = key($languages);
133 $this->language = null;
136 $this->uri = $item['uri'];
137 $this->url = $item['plink'] ?? null;
138 $this->replies_count = $counts->replies;
139 $this->reblogs_count = $counts->reblogs;
140 $this->favourites_count = $counts->favourites;
141 $this->favourited = $userAttributes->favourited;
142 $this->reblogged = $userAttributes->reblogged;
143 $this->muted = $userAttributes->muted;
144 $this->bookmarked = $userAttributes->bookmarked;
145 $this->pinned = $userAttributes->pinned;
146 $this->content = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::MASTODON_API);
147 $this->reblog = $reblog;
148 $this->quote = $quote;
149 $this->application = $application->toArray();
150 $this->account = $account->toArray();
151 $this->media_attachments = $attachments;
152 $this->mentions = $mentions;
155 $this->card = $card->toArray() ?: null;
157 $this->friendica = $friendica;
161 * Returns the current created_at string or null if not set
162 * @return \DateTime|null
164 public function createdAt(): ?string
166 return $this->created_at;
170 * Returns the current edited_at string or null if not set
173 public function editedAt(): ?string
175 return $this->edited_at;
179 * Returns the Friendica Extension properties
180 * @return FriendicaExtension
182 public function friendicaExtension(): FriendicaExtension
184 return $this->friendica;
188 * Returns the current entity as an array
192 public function toArray(): array
194 $status = parent::toArray();
196 if (!$status['pinned']) {
197 unset($status['pinned']);
200 if (empty($status['application']['name'])) {
201 unset($status['application']);
204 if (empty($status['reblog'])) {
205 $status['reblog'] = null;
208 if (empty($status['quote'])) {
209 $status['quote'] = null;
212 if (empty($status['in_reply_to_status'])) {
213 $status['in_reply_to_status'] = null;
216 if ($status['created_at'] == $status['edited_at']) {
217 $status['edited_at'] = null;