]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Status.php
Merge pull request #12820 from MrPetovan/bug/fatal-errors
[friendica.git] / src / Object / Api / Mastodon / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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 (Datetime) */
42         protected $created_at;
43         /** @var string|null */
44         protected $in_reply_to_id = null;
45         /** @var Status|null - Fedilab extension, see issue https://github.com/friendica/friendica/issues/12672 */
46         protected $in_reply_to_status = null;
47         /** @var string|null */
48         protected $in_reply_to_account_id = null;
49         /** @var bool */
50         protected $sensitive = false;
51         /** @var string */
52         protected $spoiler_text = "";
53         /** @var string (Enum of public, unlisted, private, direct)*/
54         protected $visibility;
55         /** @var string|null */
56         protected $language = null;
57         /** @var string */
58         protected $uri;
59         /** @var string|null (URL)*/
60         protected $url = null;
61         /** @var int */
62         protected $replies_count = 0;
63         /** @var int */
64         protected $reblogs_count = 0;
65         /** @var int */
66         protected $favourites_count = 0;
67         /** @var bool */
68         protected $favourited = false;
69         /** @var bool */
70         protected $reblogged = false;
71         /** @var bool */
72         protected $muted = false;
73         /** @var bool */
74         protected $bookmarked = false;
75         /** @var bool */
76         protected $pinned = false;
77         /** @var string */
78         protected $content;
79         /** @var Status|null */
80         protected $reblog = null;
81         /** @var Status|null - Akkoma extension, see issue https://github.com/friendica/friendica/issues/12603 */
82         protected $quote = null;
83         /** @var Application */
84         protected $application = null;
85         /** @var Account */
86         protected $account;
87         /** @var Attachment */
88         protected $media_attachments = [];
89         /** @var Mention */
90         protected $mentions = [];
91         /** @var Tag */
92         protected $tags = [];
93         /** @var Emoji[] */
94         protected $emojis = [];
95         /** @var Card|null */
96         protected $card = null;
97         /** @var Poll|null */
98         protected $poll = null;
99         /** @var FriendicaExtension */
100         protected $friendica;
101
102         /**
103          * Creates a status record from an item record.
104          *
105          * @param array   $item
106          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
107          */
108         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, array $quote = null, array $poll = null)
109         {
110                 $this->id         = (string)$item['uri-id'];
111                 $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::JSON);
112
113                 if ($item['gravity'] == Item::GRAVITY_COMMENT) {
114                         $this->in_reply_to_id         = (string)$item['thr-parent-id'];
115                         $this->in_reply_to_status     = $in_reply;
116                         $this->in_reply_to_account_id = (string)$item['parent-author-id'];
117                 }
118
119                 $this->sensitive    = $sensitive;
120                 $this->spoiler_text = $item['title'] ?: $item['content-warning'] ?: '';
121
122                 $visibility = ['public', 'private', 'unlisted'];
123                 $this->visibility = $visibility[$item['private']];
124
125                 $languages = json_decode($item['language'] ?? '', true);
126                 if (is_array($languages)) {
127                         reset($languages);
128                         $this->language = key($languages);
129                 } else {
130                         $this->language = null;
131                 }
132
133                 $this->uri = $item['uri'];
134                 $this->url = $item['plink'] ?? null;
135                 $this->replies_count = $counts->replies;
136                 $this->reblogs_count = $counts->reblogs;
137                 $this->favourites_count = $counts->favourites;
138                 $this->favourited = $userAttributes->favourited;
139                 $this->reblogged = $userAttributes->reblogged;
140                 $this->muted = $userAttributes->muted;
141                 $this->bookmarked = $userAttributes->bookmarked;
142                 $this->pinned = $userAttributes->pinned;
143                 $this->content = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::MASTODON_API);
144                 $this->reblog = $reblog;
145                 $this->quote = $quote;
146                 $this->application = $application->toArray();
147                 $this->account = $account->toArray();
148                 $this->media_attachments = $attachments;
149                 $this->mentions = $mentions;
150                 $this->tags = $tags;
151                 $this->emojis = [];
152                 $this->card = $card->toArray() ?: null;
153                 $this->poll = $poll;
154                 $this->friendica = new FriendicaExtension($item['title']);
155         }
156
157         /**
158          * Returns the current entity as an array
159          *
160          * @return array
161          */
162         public function toArray(): array
163         {
164                 $status = parent::toArray();
165
166                 if (!$status['pinned']) {
167                         unset($status['pinned']);
168                 }
169
170                 if (empty($status['application']['name'])) {
171                         unset($status['application']);
172                 }
173
174                 if (empty($status['reblog'])) {
175                         $status['reblog'] = null;
176                 }
177
178                 if (empty($status['quote'])) {
179                         $status['quote'] = null;
180                 }
181
182                 if (empty($status['in_reply_to_status'])) {
183                         $status['in_reply_to_status'] = null;
184                 }
185
186                 return $status;
187         }
188 }