]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Statuses.php
6ae076733435aeebc18a339c8cb61a007cec0506
[friendica.git] / src / Module / Api / Mastodon / Statuses.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\Module\Api\Mastodon;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Content\Text\Markdown;
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         public static function post(array $parameters = [])
45         {
46                 self::checkAllowedScope(self::SCOPE_WRITE);
47                 $uid = self::getCurrentUserID();
48
49                 $request = self::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                 ]);
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                 $body = BBCode::expandTags($body);
67
68                 $item = [];
69                 $item['uid']        = $uid;
70                 $item['verb']       = Activity::POST;
71                 $item['contact-id'] = $owner['id'];
72                 $item['author-id']  = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
73                 $item['title']      = $request['spoiler_text'];
74                 $item['body']       = $body;
75
76                 if (!empty(self::getCurrentApplication()['name'])) {
77                         $item['app'] = self::getCurrentApplication()['name'];
78                 }
79
80                 if (empty($item['app'])) {
81                         $item['app'] = 'API';
82                 }
83
84                 switch ($request['visibility']) {
85                         case 'public':
86                                 $item['allow_cid'] = '';
87                                 $item['allow_gid'] = '';
88                                 $item['deny_cid']  = '';
89                                 $item['deny_gid']  = '';
90                                 $item['private']   = Item::PUBLIC;
91                                 break;
92                         case 'unlisted':
93                                 $item['allow_cid'] = '';
94                                 $item['allow_gid'] = '';
95                                 $item['deny_cid']  = '';
96                                 $item['deny_gid']  = '';
97                                 $item['private']   = Item::UNLISTED;
98                                 break;
99                         case 'private':
100                                 if (!empty($owner['allow_cid'] . $owner['allow_gid'] . $owner['deny_cid'] . $owner['deny_gid'])) {
101                                         $item['allow_cid'] = $owner['allow_cid'];
102                                         $item['allow_gid'] = $owner['allow_gid'];
103                                         $item['deny_cid']  = $owner['deny_cid'];
104                                         $item['deny_gid']  = $owner['deny_gid'];
105                                 } else {
106                                         $item['allow_cid'] = '';
107                                         $item['allow_gid'] = '<' . Group::FOLLOWERS . '>';
108                                         $item['deny_cid']  = '';
109                                         $item['deny_gid']  = '';
110                                 }
111                                 $item['private'] = Item::PRIVATE;
112                                 break;
113                         case 'direct':
114                                 // Direct messages are currently unsupported
115                                 DI::mstdnError()->InternalError('Direct messages are currently unsupported');
116                                 break;
117                         default:
118                                 $item['allow_cid'] = $owner['allow_cid'];
119                                 $item['allow_gid'] = $owner['allow_gid'];
120                                 $item['deny_cid']  = $owner['deny_cid'];
121                                 $item['deny_gid']  = $owner['deny_gid'];
122
123                                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
124                                         $item['private'] = Item::PRIVATE;
125                                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
126                                         $item['private'] = Item::UNLISTED;
127                                 } else {
128                                         $item['private'] = Item::PUBLIC;
129                                 }
130                                 break;
131                 }
132
133                 if (!empty($request['language'])) {
134                         $item['language'] = json_encode([$request['language'] => 1]);
135                 }
136
137                 if ($request['in_reply_to_id']) {
138                         $parent = Post::selectFirst(['uri'], ['uri-id' => $request['in_reply_to_id'], 'uid' => [0, $uid]]);
139                         $item['thr-parent']  = $parent['uri'];
140                         $item['gravity']     = GRAVITY_COMMENT;
141                         $item['object-type'] = Activity\ObjectType::COMMENT;
142                 } else {
143                         self::checkThrottleLimit();
144
145                         $item['gravity']     = GRAVITY_PARENT;
146                         $item['object-type'] = Activity\ObjectType::NOTE;
147                 }
148
149                 if (!empty($request['media_ids'])) {
150                         $item['object-type'] = Activity\ObjectType::IMAGE;
151                         $item['post-type']   = Item::PT_IMAGE;
152                         $item['attachments'] = [];
153
154                         foreach ($request['media_ids'] as $id) {
155                                 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
156                                                 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
157                                                 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
158
159                                 if (empty($media)) {
160                                         continue;
161                                 }
162
163                                 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
164
165                                 $ressources[] = $media[0]['resource-id'];
166                                 $phototypes = Images::supportedTypes();
167                                 $ext = $phototypes[$media[0]['type']];
168
169                                 $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
170                                         'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
171                                         'size' => $media[0]['datasize'],
172                                         'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
173                                         'description' => $media[0]['desc'] ?? '',
174                                         'width' => $media[0]['width'],
175                                         'height' => $media[0]['height']];
176
177                                 if (count($media) > 1) {
178                                         $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
179                                         $attachment['preview-width'] = $media[1]['width'];
180                                         $attachment['preview-height'] = $media[1]['height'];
181                                 }
182                                 $item['attachments'][] = $attachment;
183                         }
184                 }
185
186                 if (!empty($request['scheduled_at'])) {
187                         $item['guid'] = Item::guid($item, true);
188                         $item['uri'] = Item::newURI($item['uid'], $item['guid']);
189                         $id = Post\Delayed::add($item['uri'], $item, PRIORITY_HIGH, Post\Delayed::PREPARED, $request['scheduled_at']);
190                         if (empty($id)) {
191                                 DI::mstdnError()->InternalError();
192                         }
193                         System::jsonExit(DI::mstdnScheduledStatus()->createFromDelayedPostId($id, $uid)->toArray());
194                 }
195
196                 $id = Item::insert($item, true);
197                 if (!empty($id)) {
198                         $item = Post::selectFirst(['uri-id'], ['id' => $id]);
199                         if (!empty($item['uri-id'])) {
200                                 System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid));
201                         }
202                 }
203
204                 DI::mstdnError()->InternalError();
205         }
206
207         public static function delete(array $parameters = [])
208         {
209                 self::checkAllowedScope(self::SCOPE_READ);
210                 $uid = self::getCurrentUserID();
211
212                 if (empty($parameters['id'])) {
213                         DI::mstdnError()->UnprocessableEntity();
214                 }
215
216                 $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $parameters['id'], 'uid' => $uid]);
217                 if (empty($item['id'])) {
218                         DI::mstdnError()->RecordNotFound();
219                 }
220
221                 if (!Item::markForDeletionById($item['id'])) {
222                         DI::mstdnError()->RecordNotFound();
223                 }
224
225                 System::jsonExit([]);
226         }
227
228         /**
229          * @param array $parameters
230          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
231          */
232         public static function rawContent(array $parameters = [])
233         {
234                 $uid = self::getCurrentUserID();
235
236                 if (empty($parameters['id'])) {
237                         DI::mstdnError()->UnprocessableEntity();
238                 }
239
240                 System::jsonExit(DI::mstdnStatus()->createFromUriId($parameters['id'], $uid));
241         }
242 }