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