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