]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Status.php
Merge remote-tracking branch 'upstream/develop' into api-again
[friendica.git] / src / Object / Api / Mastodon / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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::ATOM);
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'];
112
113                 $visibility = ['public', 'private', 'unlisted'];
114                 $this->visibility = $visibility[$item['private']];
115
116                 $languages = json_decode($item['language'], true);
117                 $this->language = is_array($languages) ? array_key_first($languages) : null;
118
119                 $this->uri = $item['uri'];
120                 $this->url = $item['plink'] ?? null;
121                 $this->replies_count = $counts->replies;
122                 $this->reblogs_count = $counts->reblogs;
123                 $this->favourites_count = $counts->favourites;
124                 $this->favourited = $userAttributes->favourited;
125                 $this->reblogged = $userAttributes->reblogged;
126                 $this->muted = $userAttributes->muted;
127                 $this->bookmarked = $userAttributes->bookmarked;
128                 $this->pinned = $userAttributes->pinned;
129                 $this->content = BBCode::convert($item['raw-body'] ?? $item['body'], false, BBCode::API);
130                 $this->reblog = $reblog;
131                 $this->application = $application->toArray();
132                 $this->account = $account->toArray();
133                 $this->media_attachments = $attachments;
134                 $this->mentions = $mentions;
135                 $this->tags = $tags;
136                 $this->emojis = [];
137                 $this->card = $card->toArray() ?: null;
138                 $this->poll = null;
139         }
140
141         /**
142          * Returns the current entity as an array
143          *
144          * @return array
145          */
146         public function toArray(): array
147         {
148                 $status = parent::toArray();
149
150                 if (!$status['pinned']) {
151                         unset($status['pinned']);
152                 }
153
154                 if (empty($status['application']['name'])) {
155                         unset($status['application']);
156                 }
157
158                 if (empty($status['reblog'])) {
159                         $status['reblog'] = null;
160                 }
161
162                 return $status;
163         }
164 }