]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Statuses.php
50e0eaa725a980e50752592c45f801368c7a430c
[friendica.git] / src / Module / Api / Mastodon / Statuses.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\Module\Api\Mastodon;
23
24 use Friendica\Content\Text\Markdown;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Group;
31 use Friendica\Model\Item;
32 use Friendica\Model\Photo;
33 use Friendica\Model\Post;
34 use Friendica\Model\User;
35 use Friendica\Module\BaseApi;
36 use Friendica\Protocol\Activity;
37 use Friendica\Util\Images;
38
39 /**
40  * @see https://docs.joinmastodon.org/methods/statuses/
41  */
42 class Statuses extends BaseApi
43 {
44         protected function post(array $request = [])
45         {
46                 self::checkAllowedScope(self::SCOPE_WRITE);
47                 $uid = self::getCurrentUserID();
48
49                 $request = $this->getRequest([
50                         'status'         => '',    // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
51                         'media_ids'      => [],    // Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used.
52                         'poll'           => [],    // Poll data. If provided, media_ids cannot be used, and poll[expires_in] must be provided.
53                         'in_reply_to_id' => 0,     // ID of the status being replied to, if status is a reply
54                         'sensitive'      => false, // Mark status and attached media as sensitive?
55                         'spoiler_text'   => '',    // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
56                         'visibility'     => '',    // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct".
57                         'scheduled_at'   => '',    // ISO 8601 Datetime at which to schedule a status. Providing this paramter will cause ScheduledStatus to be returned instead of Status. Must be at least 5 minutes in the future.
58                         'language'       => '',    // ISO 639 language code for this status.
59                 ], $request);
60
61                 $owner = User::getOwnerDataById($uid);
62
63                 // The imput is defined as text. So we can use Markdown for some enhancements
64                 $body = Markdown::toBBCode($request['status']);
65
66                 $item               = [];
67                 $item['network']    = Protocol::DFRN;
68                 $item['uid']        = $uid;
69                 $item['verb']       = Activity::POST;
70                 $item['contact-id'] = $owner['id'];
71                 $item['author-id']  = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
72                 $item['body']       = $body;
73
74                 if (!empty(self::getCurrentApplication()['name'])) {
75                         $item['app'] = self::getCurrentApplication()['name'];
76                 }
77
78                 if (empty($item['app'])) {
79                         $item['app'] = 'API';
80                 }
81
82                 switch ($request['visibility']) {
83                         case 'public':
84                                 $item['allow_cid'] = '';
85                                 $item['allow_gid'] = '';
86                                 $item['deny_cid']  = '';
87                                 $item['deny_gid']  = '';
88                                 $item['private']   = Item::PUBLIC;
89                                 break;
90                         case 'unlisted':
91                                 $item['allow_cid'] = '';
92                                 $item['allow_gid'] = '';
93                                 $item['deny_cid']  = '';
94                                 $item['deny_gid']  = '';
95                                 $item['private']   = Item::UNLISTED;
96                                 break;
97                         case 'private':
98                                 if (!empty($owner['allow_cid'] . $owner['allow_gid'] . $owner['deny_cid'] . $owner['deny_gid'])) {
99                                         $item['allow_cid'] = $owner['allow_cid'];
100                                         $item['allow_gid'] = $owner['allow_gid'];
101                                         $item['deny_cid']  = $owner['deny_cid'];
102                                         $item['deny_gid']  = $owner['deny_gid'];
103                                 } else {
104                                         $item['allow_cid'] = '';
105                                         $item['allow_gid'] = '<' . Group::FOLLOWERS . '>';
106                                         $item['deny_cid']  = '';
107                                         $item['deny_gid']  = '';
108                                 }
109                                 $item['private'] = Item::PRIVATE;
110                                 break;
111                         case 'direct':
112                                 // The permissions are assigned in "expandTags"
113                                 break;
114                         default:
115                                 if (is_numeric($request['visibility']) && Group::exists($request['visibility'], $uid)) {
116                                         $item['allow_cid'] = '';
117                                         $item['allow_gid'] = '<' . $request['visibility'] . '>';
118                                         $item['deny_cid']  = '';
119                                         $item['deny_gid']  = '';
120                                 } else {
121                                         $item['allow_cid'] = $owner['allow_cid'];
122                                         $item['allow_gid'] = $owner['allow_gid'];
123                                         $item['deny_cid']  = $owner['deny_cid'];
124                                         $item['deny_gid']  = $owner['deny_gid'];
125                                 }
126
127                                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
128                                         $item['private'] = Item::PRIVATE;
129                                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
130                                         $item['private'] = Item::UNLISTED;
131                                 } else {
132                                         $item['private'] = Item::PUBLIC;
133                                 }
134                                 break;
135                 }
136
137                 if (!empty($request['language'])) {
138                         $item['language'] = json_encode([$request['language'] => 1]);
139                 }
140
141                 if ($request['in_reply_to_id']) {
142                         $parent = Post::selectFirst(['uri'], ['uri-id' => $request['in_reply_to_id'], 'uid' => [0, $uid]]);
143
144                         $item['thr-parent']  = $parent['uri'];
145                         $item['gravity']     = GRAVITY_COMMENT;
146                         $item['object-type'] = Activity\ObjectType::COMMENT;
147                         $item['body']        = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $request['spoiler_text'] . "[/abstract]\n" . $item['body'];
148                 } else {
149                         self::checkThrottleLimit();
150
151                         $item['gravity']     = GRAVITY_PARENT;
152                         $item['object-type'] = Activity\ObjectType::NOTE;
153                         $item['title']       = $request['spoiler_text'];
154                 }
155
156                 $item = DI::contentItem()->expandTags($item, $request['visibility'] == 'direct');
157
158                 if (!empty($request['media_ids'])) {
159                         $item['object-type'] = Activity\ObjectType::IMAGE;
160                         $item['post-type']   = Item::PT_IMAGE;
161                         $item['attachments'] = [];
162
163                         foreach ($request['media_ids'] as $id) {
164                                 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
165                                                 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
166                                                 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
167
168                                 if (empty($media)) {
169                                         continue;
170                                 }
171
172                                 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
173
174                                 $ressources[] = $media[0]['resource-id'];
175                                 $phototypes = Images::supportedTypes();
176                                 $ext = $phototypes[$media[0]['type']];
177
178                                 $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
179                                         'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
180                                         'size' => $media[0]['datasize'],
181                                         'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
182                                         'description' => $media[0]['desc'] ?? '',
183                                         'width' => $media[0]['width'],
184                                         'height' => $media[0]['height']];
185
186                                 if (count($media) > 1) {
187                                         $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
188                                         $attachment['preview-width'] = $media[1]['width'];
189                                         $attachment['preview-height'] = $media[1]['height'];
190                                 }
191                                 $item['attachments'][] = $attachment;
192                         }
193                 }
194
195                 if (!empty($request['scheduled_at'])) {
196                         $item['guid'] = Item::guid($item, true);
197                         $item['uri'] = Item::newURI($item['uid'], $item['guid']);
198                         $id = Post\Delayed::add($item['uri'], $item, PRIORITY_HIGH, Post\Delayed::PREPARED, $request['scheduled_at']);
199                         if (empty($id)) {
200                                 DI::mstdnError()->InternalError();
201                         }
202                         System::jsonExit(DI::mstdnScheduledStatus()->createFromDelayedPostId($id, $uid)->toArray());
203                 }
204
205                 $id = Item::insert($item, true);
206                 if (!empty($id)) {
207                         $item = Post::selectFirst(['uri-id'], ['id' => $id]);
208                         if (!empty($item['uri-id'])) {
209                                 System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid));
210                         }
211                 }
212
213                 DI::mstdnError()->InternalError();
214         }
215
216         protected function delete(array $request = [])
217         {
218                 self::checkAllowedScope(self::SCOPE_READ);
219                 $uid = self::getCurrentUserID();
220
221                 if (empty($this->parameters['id'])) {
222                         DI::mstdnError()->UnprocessableEntity();
223                 }
224
225                 $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => $uid]);
226                 if (empty($item['id'])) {
227                         DI::mstdnError()->RecordNotFound();
228                 }
229
230                 if (!Item::markForDeletionById($item['id'])) {
231                         DI::mstdnError()->RecordNotFound();
232                 }
233
234                 System::jsonExit([]);
235         }
236
237         /**
238          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
239          */
240         protected function rawContent(array $request = [])
241         {
242                 $uid = self::getCurrentUserID();
243
244                 if (empty($this->parameters['id'])) {
245                         DI::mstdnError()->UnprocessableEntity();
246                 }
247
248                 System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid));
249         }
250 }