3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon;
24 use Friendica\Content\Text\Markdown;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
27 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Group;
32 use Friendica\Model\Item;
33 use Friendica\Model\Photo;
34 use Friendica\Model\Post;
35 use Friendica\Model\User;
36 use Friendica\Module\BaseApi;
37 use Friendica\Network\HTTPException;
38 use Friendica\Protocol\Activity;
39 use Friendica\Util\Images;
42 * @see https://docs.joinmastodon.org/methods/statuses/
44 class Statuses extends BaseApi
46 public function put(array $request = [])
48 self::checkAllowedScope(self::SCOPE_WRITE);
49 $uid = self::getCurrentUserID();
51 $request = $this->getRequest([
52 'status' => '', // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
53 'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply
54 'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
55 'language' => '', // ISO 639 language code for this status.
58 $owner = User::getOwnerDataById($uid);
62 'uri-id' => $this->parameters['id'],
63 'contact-id' => $owner['id'],
64 'author-id' => Contact::getPublicIdByUserId($uid),
68 $post = Post::selectFirst(['uri-id', 'id'], $condition);
69 if (empty($post['id'])) {
70 throw new HTTPException\NotFoundException('Item with URI ID ' . $this->parameters['id'] . ' not found for user ' . $uid . '.');
73 // The imput is defined as text. So we can use Markdown for some enhancements
74 $item = ['body' => Markdown::toBBCode($request['status']), 'app' => $this->getApp(), 'title' => ''];
76 if (!empty($request['language'])) {
77 $item['language'] = json_encode([$request['language'] => 1]);
80 if (!empty($request['spoiler_text'])) {
81 if (($request['in_reply_to_id'] == $post['uri-id']) && DI::pConfig()->get($uid, 'system', 'api_spoiler_title', true)) {
82 $item['title'] = $request['spoiler_text'];
84 $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $request['spoiler_text'] . "[/abstract]\n" . $item['body'];
88 Item::update($item, ['id' => $post['id']]);
89 Item::updateDisplayCache($post['uri-id']);
91 System::jsonExit(DI::mstdnStatus()->createFromUriId($post['uri-id'], $uid, self::appSupportsQuotes()));
94 protected function post(array $request = [])
96 self::checkAllowedScope(self::SCOPE_WRITE);
97 $uid = self::getCurrentUserID();
99 $request = $this->getRequest([
100 'status' => '', // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
101 'media_ids' => [], // Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used.
102 'poll' => [], // Poll data. If provided, media_ids cannot be used, and poll[expires_in] must be provided.
103 'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply
104 'quote_id' => 0, // ID of the message to quote
105 'sensitive' => false, // Mark status and attached media as sensitive?
106 'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
107 'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct".
108 '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.
109 'language' => '', // ISO 639 language code for this status.
112 $owner = User::getOwnerDataById($uid);
114 // The imput is defined as text. So we can use Markdown for some enhancements
115 $body = Markdown::toBBCode($request['status']);
118 $item['network'] = Protocol::DFRN;
120 $item['verb'] = Activity::POST;
121 $item['contact-id'] = $owner['id'];
122 $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
124 $item['body'] = $body;
125 $item['app'] = $this->getApp();
127 switch ($request['visibility']) {
129 $item['allow_cid'] = '';
130 $item['allow_gid'] = '';
131 $item['deny_cid'] = '';
132 $item['deny_gid'] = '';
133 $item['private'] = Item::PUBLIC;
136 $item['allow_cid'] = '';
137 $item['allow_gid'] = '';
138 $item['deny_cid'] = '';
139 $item['deny_gid'] = '';
140 $item['private'] = Item::UNLISTED;
143 if (!empty($owner['allow_cid'] . $owner['allow_gid'] . $owner['deny_cid'] . $owner['deny_gid'])) {
144 $item['allow_cid'] = $owner['allow_cid'];
145 $item['allow_gid'] = $owner['allow_gid'];
146 $item['deny_cid'] = $owner['deny_cid'];
147 $item['deny_gid'] = $owner['deny_gid'];
149 $item['allow_cid'] = '';
150 $item['allow_gid'] = '<' . Group::FOLLOWERS . '>';
151 $item['deny_cid'] = '';
152 $item['deny_gid'] = '';
154 $item['private'] = Item::PRIVATE;
157 $item['private'] = Item::PRIVATE;
158 // The permissions are assigned in "expandTags"
161 if (is_numeric($request['visibility']) && Group::exists($request['visibility'], $uid)) {
162 $item['allow_cid'] = '';
163 $item['allow_gid'] = '<' . $request['visibility'] . '>';
164 $item['deny_cid'] = '';
165 $item['deny_gid'] = '';
167 $item['allow_cid'] = $owner['allow_cid'];
168 $item['allow_gid'] = $owner['allow_gid'];
169 $item['deny_cid'] = $owner['deny_cid'];
170 $item['deny_gid'] = $owner['deny_gid'];
173 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
174 $item['private'] = Item::PRIVATE;
175 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
176 $item['private'] = Item::UNLISTED;
178 $item['private'] = Item::PUBLIC;
183 if (!empty($request['language'])) {
184 $item['language'] = json_encode([$request['language'] => 1]);
187 if ($request['in_reply_to_id']) {
188 $parent = Post::selectFirst(['uri', 'private'], ['uri-id' => $request['in_reply_to_id'], 'uid' => [0, $uid]]);
190 $item['thr-parent'] = $parent['uri'];
191 $item['gravity'] = Item::GRAVITY_COMMENT;
192 $item['object-type'] = Activity\ObjectType::COMMENT;
194 if (in_array($parent['private'], [Item::UNLISTED, Item::PUBLIC]) && ($item['private'] == Item::PRIVATE)) {
195 throw new HTTPException\NotImplementedException('Private replies for public posts are not implemented.');
198 self::checkThrottleLimit();
200 $item['gravity'] = Item::GRAVITY_PARENT;
201 $item['object-type'] = Activity\ObjectType::NOTE;
204 if ($request['quote_id']) {
205 if (!Post::exists(['uri-id' => $request['quote_id'], 'uid' => [0, $uid]])) {
206 throw new HTTPException\NotFoundException('Item with URI ID ' . $request['quote_id'] . ' not found for user ' . $uid . '.');
208 $item['quote-uri-id'] = $request['quote_id'];
211 if (!empty($request['spoiler_text'])) {
212 if (!$request['in_reply_to_id'] && DI::pConfig()->get($uid, 'system', 'api_spoiler_title', true)) {
213 $item['title'] = $request['spoiler_text'];
215 $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $request['spoiler_text'] . "[/abstract]\n" . $item['body'];
219 $item = DI::contentItem()->expandTags($item, $request['visibility'] == 'direct');
221 if (!empty($request['media_ids'])) {
222 $item['object-type'] = Activity\ObjectType::IMAGE;
223 $item['post-type'] = Item::PT_IMAGE;
224 $item['attachments'] = [];
226 foreach ($request['media_ids'] as $id) {
227 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
228 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
229 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
235 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
237 $ressources[] = $media[0]['resource-id'];
238 $phototypes = Images::supportedTypes();
239 $ext = $phototypes[$media[0]['type']];
241 $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
242 'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
243 'size' => $media[0]['datasize'],
244 'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
245 'description' => $media[0]['desc'] ?? '',
246 'width' => $media[0]['width'],
247 'height' => $media[0]['height']];
249 if (count($media) > 1) {
250 $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
251 $attachment['preview-width'] = $media[1]['width'];
252 $attachment['preview-height'] = $media[1]['height'];
254 $item['attachments'][] = $attachment;
258 if (!empty($request['scheduled_at'])) {
259 $item['guid'] = Item::guid($item, true);
260 $item['uri'] = Item::newURI($item['guid']);
261 $id = Post\Delayed::add($item['uri'], $item, Worker::PRIORITY_HIGH, Post\Delayed::PREPARED, $request['scheduled_at']);
263 DI::mstdnError()->InternalError();
265 System::jsonExit(DI::mstdnScheduledStatus()->createFromDelayedPostId($id, $uid)->toArray());
268 $id = Item::insert($item, true);
270 $item = Post::selectFirst(['uri-id'], ['id' => $id]);
271 if (!empty($item['uri-id'])) {
272 System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, self::appSupportsQuotes()));
276 DI::mstdnError()->InternalError();
279 protected function delete(array $request = [])
281 self::checkAllowedScope(self::SCOPE_READ);
282 $uid = self::getCurrentUserID();
284 if (empty($this->parameters['id'])) {
285 DI::mstdnError()->UnprocessableEntity();
288 $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => $uid]);
289 if (empty($item['id'])) {
290 DI::mstdnError()->RecordNotFound();
293 if (!Item::markForDeletionById($item['id'])) {
294 DI::mstdnError()->RecordNotFound();
297 System::jsonExit([]);
301 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
303 protected function rawContent(array $request = [])
305 $uid = self::getCurrentUserID();
307 if (empty($this->parameters['id'])) {
308 DI::mstdnError()->UnprocessableEntity();
311 System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, self::appSupportsQuotes(), false));
314 private function getApp(): string
316 if (!empty(self::getCurrentApplication()['name'])) {
317 return self::getCurrentApplication()['name'];