Merge pull request #14103 from annando/api-channel-list
[friendica.git/.git] / src / Object / Api / Mastodon / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Object\Api\Mastodon;
23
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;
31
32 /**
33  * Class Status
34  *
35  * @see https://docs.joinmastodon.org/entities/status
36  */
37 class Status extends BaseDataTransferObject
38 {
39         /** @var string */
40         protected $id;
41         /** @var string|null (Datetime) */
42         protected $created_at;
43         /** @var string|null (Datetime) */
44         protected $edited_at;
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;
51         /** @var bool */
52         protected $sensitive = false;
53         /** @var string */
54         protected $spoiler_text = "";
55         /** @var string (Enum of public, unlisted, private, direct)*/
56         protected $visibility;
57         /** @var string|null */
58         protected $language = null;
59         /** @var string */
60         protected $uri;
61         /** @var string|null (URL)*/
62         protected $url = null;
63         /** @var int */
64         protected $replies_count = 0;
65         /** @var int */
66         protected $reblogs_count = 0;
67         /** @var int */
68         protected $favourites_count = 0;
69         /** @var bool */
70         protected $favourited = false;
71         /** @var bool */
72         protected $reblogged = false;
73         /** @var bool */
74         protected $muted = false;
75         /** @var bool */
76         protected $bookmarked = false;
77         /** @var bool */
78         protected $pinned = false;
79         /** @var string */
80         protected $content;
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;
87         /** @var Account */
88         protected $account;
89         /** @var Attachment */
90         protected $media_attachments = [];
91         /** @var Mention */
92         protected $mentions = [];
93         /** @var Tag */
94         protected $tags = [];
95         /** @var Emoji[] */
96         protected $emojis = [];
97         /** @var Card|null */
98         protected $card = null;
99         /** @var Poll|null */
100         protected $poll = null;
101         /** @var FriendicaExtension */
102         protected $friendica;
103
104         /**
105          * Creates a status record from an item record.
106          *
107          * @param array   $item
108          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
109          */
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, array $emojis = null)
111         {
112                 $reblogged          = !empty($reblog);
113                 $this->id           = (string)$item['uri-id'];
114                 $this->created_at   = DateTimeFormat::utc($item['created'], DateTimeFormat::JSON);
115                 $this->edited_at    = DateTimeFormat::utc($item['edited'], DateTimeFormat::JSON);
116
117                 if ($item['gravity'] == Item::GRAVITY_COMMENT) {
118                         $this->in_reply_to_id         = (string)$item['thr-parent-id'];
119                         $this->in_reply_to_status     = $in_reply;
120                         $this->in_reply_to_account_id = (string)$item['parent-author-id'];
121                 }
122
123                 $this->sensitive    = $sensitive;
124                 $this->spoiler_text = $item['title'] ?: $item['content-warning'] ?: '';
125
126                 $visibility = ['public', 'private', 'unlisted'];
127                 $this->visibility = $visibility[$item['private']];
128
129                 $languages = json_decode($item['language'] ?? '', true);
130                 if (is_array($languages)) {
131                         reset($languages);
132                         $this->language = key($languages);
133                 } else {
134                         $this->language = null;
135                 }
136
137                 $this->uri = $item['uri'];
138                 $this->url = $item['plink'] ?? null;
139                 $this->replies_count = $reblogged ? 0 : $counts->replies;
140                 $this->reblogs_count = $reblogged ? 0 : $counts->reblogs;
141                 $this->favourites_count = $reblogged ? 0 : $counts->favourites;
142                 $this->favourited = $userAttributes->favourited;
143                 $this->reblogged = $userAttributes->reblogged;
144                 $this->muted = $userAttributes->muted;
145                 $this->bookmarked = $userAttributes->bookmarked;
146                 $this->pinned = $userAttributes->pinned;
147                 $this->content = $reblogged ? '' : BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::MASTODON_API);
148                 $this->reblog = $reblog;
149                 $this->quote = $quote;
150                 $this->application = $application->toArray();
151                 $this->account = $account->toArray();
152                 $this->media_attachments = $reblogged ? [] : $attachments;
153                 $this->mentions = $reblogged ? [] : $mentions;
154                 $this->tags = $reblogged ? [] : $tags;
155                 $this->emojis = $reblogged ? [] : ($emojis ?: []);
156                 $this->card = $reblogged ? null : ($card->toArray() ?: null);
157                 $this->poll = $reblogged ? null : $poll;
158                 $this->friendica = $reblogged ? null : $friendica;
159         }
160
161         /**
162          * Returns the current created_at string or null if not set
163          * @return ?string
164          */
165         public function createdAt(): ?string
166         {
167                 return $this->created_at;
168         }
169
170         /**
171          * Returns the current edited_at string or null if not set
172          * @return ?string
173          */
174         public function editedAt(): ?string
175         {
176                 return $this->edited_at;
177         }
178
179         /**
180          * Returns the Friendica Extension properties
181          * @return FriendicaExtension
182          */
183         public function friendicaExtension(): FriendicaExtension
184         {
185                 return $this->friendica;
186         }
187
188         /**
189          * Returns the current entity as an array
190          *
191          * @return array
192          */
193         public function toArray(): array
194         {
195                 $status = parent::toArray();
196
197                 if (!$status['pinned']) {
198                         unset($status['pinned']);
199                 }
200
201                 if (empty($status['application']['name'])) {
202                         unset($status['application']);
203                 }
204
205                 if (empty($status['reblog'])) {
206                         $status['reblog'] = null;
207                 }
208
209                 if (empty($status['quote'])) {
210                         $status['quote'] = null;
211                 }
212
213                 if (empty($status['in_reply_to_status'])) {
214                         $status['in_reply_to_status'] = null;
215                 }
216
217                 if ($status['created_at'] == $status['edited_at']) {
218                         $status['edited_at'] = null;
219                 }
220
221                 return $status;
222         }
223 }