]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Statuses.php
Merge pull request #10251 from nupplaphil/feat/drone_phptest
[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\Post;
33 use Friendica\Model\User;
34 use Friendica\Module\BaseApi;
35 use Friendica\Protocol\Activity;
36 use Friendica\Util\Images;
37
38 /**
39  * @see https://docs.joinmastodon.org/methods/statuses/
40  */
41 class Statuses extends BaseApi
42 {
43         public static function post(array $parameters = [])
44         {
45                 self::login(self::SCOPE_WRITE);
46                 $uid = self::getCurrentUserID();
47
48                 $data = self::getJsonPostData();
49
50                 $status         = $data['status'] ?? '';
51                 $media_ids      = $data['media_ids'] ?? [];
52                 $in_reply_to_id = $data['in_reply_to_id'] ?? 0;
53                 $sensitive      = $data['sensitive'] ?? false; // @todo Possibly trigger "nsfw" flag?
54                 $spoiler_text   = $data['spoiler_text'] ?? '';
55                 $visibility     = $data['visibility'] ?? '';
56                 $scheduled_at   = $data['scheduled_at'] ?? ''; // Currently unsupported, but maybe in the future
57                 $language       = $data['language'] ?? '';
58
59                 $owner = User::getOwnerDataById($uid);
60
61                 // The imput is defined as text. So we can use Markdown for some enhancements
62                 $body = Markdown::toBBCode($status);
63
64                 $body = BBCode::expandTags($body);
65
66                 $item = [];
67                 $item['uid']        = $uid;
68                 $item['verb']       = Activity::POST;
69                 $item['contact-id'] = $owner['id'];
70                 $item['author-id']  = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
71                 $item['title']      = $spoiler_text;
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 ($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                                 // Direct messages are currently unsupported
113                                 DI::mstdnError()->InternalError('Direct messages are currently unsupported');
114                                 break;          
115                         default:
116                                 $item['allow_cid'] = $owner['allow_cid'];
117                                 $item['allow_gid'] = $owner['allow_gid'];
118                                 $item['deny_cid']  = $owner['deny_cid'];
119                                 $item['deny_gid']  = $owner['deny_gid'];
120
121                                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
122                                         $item['private'] = Item::PRIVATE;
123                                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
124                                         $item['private'] = Item::UNLISTED;
125                                 } else {
126                                         $item['private'] = Item::PUBLIC;
127                                 }
128                                 break;
129                 }
130
131                 if (!empty($language)) {
132                         $item['language'] = json_encode([$language => 1]);
133                 }
134
135                 if ($in_reply_to_id) {
136                         $parent = Post::selectFirst(['uri'], ['uri-id' => $in_reply_to_id, 'uid' => [0, $uid]]);
137                         $item['thr-parent']  = $parent['uri'];
138                         $item['gravity']     = GRAVITY_COMMENT;
139                         $item['object-type'] = Activity\ObjectType::COMMENT;
140                 } else {
141                         $item['gravity']     = GRAVITY_PARENT;
142                         $item['object-type'] = Activity\ObjectType::NOTE;
143                 }
144
145                 if (!empty($media_ids)) {
146                         $item['object-type'] = Activity\ObjectType::IMAGE;
147                         $item['post-type']   = Item::PT_IMAGE;
148                         $item['attachments'] = [];
149
150                         foreach ($media_ids as $id) {
151                                 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
152                                                 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
153                                                 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
154                                         
155                                 if (empty($media)) {
156                                         continue;
157                                 }
158
159                                 $ressources[] = $media[0]['resource-id'];
160                                 $phototypes = Images::supportedTypes();
161                                 $ext = $phototypes[$media[0]['type']];
162                         
163                                 $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
164                                         'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
165                                         'size' => $media[0]['datasize'],
166                                         'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
167                                         'description' => $media[0]['desc'] ?? '',
168                                         'width' => $media[0]['width'],
169                                         'height' => $media[0]['height']];
170                         
171                                 if (count($media) > 1) {
172                                         $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
173                                         $attachment['preview-width'] = $media[1]['width'];
174                                         $attachment['preview-height'] = $media[1]['height'];
175                                 }
176                                 $item['attachments'][] = $attachment;
177                         }
178                 }
179
180                 $id = Item::insert($item, true);
181                 if (!empty($id)) {
182                         $item = Post::selectFirst(['uri-id'], ['id' => $id]);
183                         if (!empty($item['uri-id'])) {
184                                 System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid));            
185                         }
186                 }
187
188                 DI::mstdnError()->InternalError();
189         }
190
191         public static function delete(array $parameters = [])
192         {
193                 self::login(self::SCOPE_READ);
194                 $uid = self::getCurrentUserID();
195
196                 if (empty($parameters['id'])) {
197                         DI::mstdnError()->UnprocessableEntity();
198                 }
199
200                 $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $parameters['id'], 'uid' => $uid]);
201                 if (empty($item['id'])) {
202                         DI::mstdnError()->RecordNotFound();
203                 }
204
205                 if (!Item::markForDeletionById($item['id'])) {
206                         DI::mstdnError()->RecordNotFound();
207                 }
208
209                 System::jsonExit([]);
210         }
211
212         /**
213          * @param array $parameters
214          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
215          */
216         public static function rawContent(array $parameters = [])
217         {
218                 if (empty($parameters['id'])) {
219                         DI::mstdnError()->UnprocessableEntity();
220                 }
221
222                 System::jsonExit(DI::mstdnStatus()->createFromUriId($parameters['id'], self::getCurrentUserID()));
223         }
224 }