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