]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Statuses.php
Merge pull request #10275 from very-ape/authenticate-hook
[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::login(self::SCOPE_WRITE);
47                 $uid = self::getCurrentUserID();
48
49                 $data = self::getJsonPostData();
50
51                 $status         = $data['status'] ?? '';
52                 $media_ids      = $data['media_ids'] ?? [];
53                 $in_reply_to_id = $data['in_reply_to_id'] ?? 0;
54                 $sensitive      = $data['sensitive'] ?? false; // @todo Possibly trigger "nsfw" flag?
55                 $spoiler_text   = $data['spoiler_text'] ?? '';
56                 $visibility     = $data['visibility'] ?? '';
57                 $scheduled_at   = $data['scheduled_at'] ?? ''; // Currently unsupported, but maybe in the future
58                 $language       = $data['language'] ?? '';
59
60                 $owner = User::getOwnerDataById($uid);
61
62                 // The imput is defined as text. So we can use Markdown for some enhancements
63                 $body = Markdown::toBBCode($status);
64
65                 $body = BBCode::expandTags($body);
66
67                 $item = [];
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['title']      = $spoiler_text;
73                 $item['body']       = $body;
74
75                 if (!empty(self::getCurrentApplication()['name'])) {
76                         $item['app'] = self::getCurrentApplication()['name'];
77                 }
78
79                 if (empty($item['app'])) {
80                         $item['app'] = 'API';
81                 }
82
83                 switch ($visibility) {
84                         case 'public':
85                                 $item['allow_cid'] = '';
86                                 $item['allow_gid'] = '';
87                                 $item['deny_cid']  = '';
88                                 $item['deny_gid']  = '';
89                                 $item['private']   = Item::PUBLIC;
90                                 break;
91                         case 'unlisted':
92                                 $item['allow_cid'] = '';
93                                 $item['allow_gid'] = '';
94                                 $item['deny_cid']  = '';
95                                 $item['deny_gid']  = '';
96                                 $item['private']   = Item::UNLISTED;
97                                 break;
98                         case 'private':
99                                 if (!empty($owner['allow_cid'] . $owner['allow_gid'] . $owner['deny_cid'] . $owner['deny_gid'])) {
100                                         $item['allow_cid'] = $owner['allow_cid'];
101                                         $item['allow_gid'] = $owner['allow_gid'];
102                                         $item['deny_cid']  = $owner['deny_cid'];
103                                         $item['deny_gid']  = $owner['deny_gid'];
104                                 } else {
105                                         $item['allow_cid'] = '';
106                                         $item['allow_gid'] = [Group::FOLLOWERS];
107                                         $item['deny_cid']  = '';
108                                         $item['deny_gid']  = '';
109                                 }
110                                 $item['private'] = Item::PRIVATE;
111                                 break;
112                         case 'direct':
113                                 // Direct messages are currently unsupported
114                                 DI::mstdnError()->InternalError('Direct messages are currently unsupported');
115                                 break;          
116                         default:
117                                 $item['allow_cid'] = $owner['allow_cid'];
118                                 $item['allow_gid'] = $owner['allow_gid'];
119                                 $item['deny_cid']  = $owner['deny_cid'];
120                                 $item['deny_gid']  = $owner['deny_gid'];
121
122                                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
123                                         $item['private'] = Item::PRIVATE;
124                                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
125                                         $item['private'] = Item::UNLISTED;
126                                 } else {
127                                         $item['private'] = Item::PUBLIC;
128                                 }
129                                 break;
130                 }
131
132                 if (!empty($language)) {
133                         $item['language'] = json_encode([$language => 1]);
134                 }
135
136                 if ($in_reply_to_id) {
137                         $parent = Post::selectFirst(['uri'], ['uri-id' => $in_reply_to_id, 'uid' => [0, $uid]]);
138                         $item['thr-parent']  = $parent['uri'];
139                         $item['gravity']     = GRAVITY_COMMENT;
140                         $item['object-type'] = Activity\ObjectType::COMMENT;
141                 } else {
142                         $item['gravity']     = GRAVITY_PARENT;
143                         $item['object-type'] = Activity\ObjectType::NOTE;
144                 }
145
146                 if (!empty($media_ids)) {
147                         $item['object-type'] = Activity\ObjectType::IMAGE;
148                         $item['post-type']   = Item::PT_IMAGE;
149                         $item['attachments'] = [];
150
151                         foreach ($media_ids as $id) {
152                                 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
153                                                 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
154                                                 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
155                                         
156                                 if (empty($media)) {
157                                         continue;
158                                 }
159
160                                 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
161
162                                 $ressources[] = $media[0]['resource-id'];
163                                 $phototypes = Images::supportedTypes();
164                                 $ext = $phototypes[$media[0]['type']];
165                         
166                                 $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
167                                         'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
168                                         'size' => $media[0]['datasize'],
169                                         'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
170                                         'description' => $media[0]['desc'] ?? '',
171                                         'width' => $media[0]['width'],
172                                         'height' => $media[0]['height']];
173                         
174                                 if (count($media) > 1) {
175                                         $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
176                                         $attachment['preview-width'] = $media[1]['width'];
177                                         $attachment['preview-height'] = $media[1]['height'];
178                                 }
179                                 $item['attachments'][] = $attachment;
180                         }
181                 }
182
183                 $id = Item::insert($item, true);
184                 if (!empty($id)) {
185                         $item = Post::selectFirst(['uri-id'], ['id' => $id]);
186                         if (!empty($item['uri-id'])) {
187                                 System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid));            
188                         }
189                 }
190
191                 DI::mstdnError()->InternalError();
192         }
193
194         public static function delete(array $parameters = [])
195         {
196                 self::login(self::SCOPE_READ);
197                 $uid = self::getCurrentUserID();
198
199                 if (empty($parameters['id'])) {
200                         DI::mstdnError()->UnprocessableEntity();
201                 }
202
203                 $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $parameters['id'], 'uid' => $uid]);
204                 if (empty($item['id'])) {
205                         DI::mstdnError()->RecordNotFound();
206                 }
207
208                 if (!Item::markForDeletionById($item['id'])) {
209                         DI::mstdnError()->RecordNotFound();
210                 }
211
212                 System::jsonExit([]);
213         }
214
215         /**
216          * @param array $parameters
217          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
218          */
219         public static function rawContent(array $parameters = [])
220         {
221                 if (empty($parameters['id'])) {
222                         DI::mstdnError()->UnprocessableEntity();
223                 }
224
225                 System::jsonExit(DI::mstdnStatus()->createFromUriId($parameters['id'], self::getCurrentUserID()));
226         }
227 }