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