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));
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 'sensitive' => false, // Mark status and attached media as sensitive?
105 'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
106 'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct".
107 '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.
108 'language' => '', // ISO 639 language code for this status.
111 $owner = User::getOwnerDataById($uid);
113 // The imput is defined as text. So we can use Markdown for some enhancements
114 $body = Markdown::toBBCode($request['status']);
117 $item['network'] = Protocol::DFRN;
119 $item['verb'] = Activity::POST;
120 $item['contact-id'] = $owner['id'];
121 $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
123 $item['body'] = $body;
124 $item['app'] = $this->getApp();
126 switch ($request['visibility']) {
128 $item['allow_cid'] = '';
129 $item['allow_gid'] = '';
130 $item['deny_cid'] = '';
131 $item['deny_gid'] = '';
132 $item['private'] = Item::PUBLIC;
135 $item['allow_cid'] = '';
136 $item['allow_gid'] = '';
137 $item['deny_cid'] = '';
138 $item['deny_gid'] = '';
139 $item['private'] = Item::UNLISTED;
142 if (!empty($owner['allow_cid'] . $owner['allow_gid'] . $owner['deny_cid'] . $owner['deny_gid'])) {
143 $item['allow_cid'] = $owner['allow_cid'];
144 $item['allow_gid'] = $owner['allow_gid'];
145 $item['deny_cid'] = $owner['deny_cid'];
146 $item['deny_gid'] = $owner['deny_gid'];
148 $item['allow_cid'] = '';
149 $item['allow_gid'] = '<' . Group::FOLLOWERS . '>';
150 $item['deny_cid'] = '';
151 $item['deny_gid'] = '';
153 $item['private'] = Item::PRIVATE;
156 // The permissions are assigned in "expandTags"
159 if (is_numeric($request['visibility']) && Group::exists($request['visibility'], $uid)) {
160 $item['allow_cid'] = '';
161 $item['allow_gid'] = '<' . $request['visibility'] . '>';
162 $item['deny_cid'] = '';
163 $item['deny_gid'] = '';
165 $item['allow_cid'] = $owner['allow_cid'];
166 $item['allow_gid'] = $owner['allow_gid'];
167 $item['deny_cid'] = $owner['deny_cid'];
168 $item['deny_gid'] = $owner['deny_gid'];
171 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
172 $item['private'] = Item::PRIVATE;
173 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
174 $item['private'] = Item::UNLISTED;
176 $item['private'] = Item::PUBLIC;
181 if (!empty($request['language'])) {
182 $item['language'] = json_encode([$request['language'] => 1]);
185 if ($request['in_reply_to_id']) {
186 $parent = Post::selectFirst(['uri'], ['uri-id' => $request['in_reply_to_id'], 'uid' => [0, $uid]]);
188 $item['thr-parent'] = $parent['uri'];
189 $item['gravity'] = Item::GRAVITY_COMMENT;
190 $item['object-type'] = Activity\ObjectType::COMMENT;
192 self::checkThrottleLimit();
194 $item['gravity'] = Item::GRAVITY_PARENT;
195 $item['object-type'] = Activity\ObjectType::NOTE;
198 if (!empty($request['spoiler_text'])) {
199 if (!$request['in_reply_to_id'] && DI::pConfig()->get($uid, 'system', 'api_spoiler_title', true)) {
200 $item['title'] = $request['spoiler_text'];
202 $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $request['spoiler_text'] . "[/abstract]\n" . $item['body'];
206 $item = DI::contentItem()->expandTags($item, $request['visibility'] == 'direct');
208 if (!empty($request['media_ids'])) {
209 $item['object-type'] = Activity\ObjectType::IMAGE;
210 $item['post-type'] = Item::PT_IMAGE;
211 $item['attachments'] = [];
213 foreach ($request['media_ids'] as $id) {
214 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
215 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
216 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
222 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
224 $ressources[] = $media[0]['resource-id'];
225 $phototypes = Images::supportedTypes();
226 $ext = $phototypes[$media[0]['type']];
228 $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
229 'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
230 'size' => $media[0]['datasize'],
231 'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
232 'description' => $media[0]['desc'] ?? '',
233 'width' => $media[0]['width'],
234 'height' => $media[0]['height']];
236 if (count($media) > 1) {
237 $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
238 $attachment['preview-width'] = $media[1]['width'];
239 $attachment['preview-height'] = $media[1]['height'];
241 $item['attachments'][] = $attachment;
245 if (!empty($request['scheduled_at'])) {
246 $item['guid'] = Item::guid($item, true);
247 $item['uri'] = Item::newURI($item['guid']);
248 $id = Post\Delayed::add($item['uri'], $item, Worker::PRIORITY_HIGH, Post\Delayed::PREPARED, $request['scheduled_at']);
250 DI::mstdnError()->InternalError();
252 System::jsonExit(DI::mstdnScheduledStatus()->createFromDelayedPostId($id, $uid)->toArray());
255 $id = Item::insert($item, true);
257 $item = Post::selectFirst(['uri-id'], ['id' => $id]);
258 if (!empty($item['uri-id'])) {
259 System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid));
263 DI::mstdnError()->InternalError();
266 protected function delete(array $request = [])
268 self::checkAllowedScope(self::SCOPE_READ);
269 $uid = self::getCurrentUserID();
271 if (empty($this->parameters['id'])) {
272 DI::mstdnError()->UnprocessableEntity();
275 $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => $uid]);
276 if (empty($item['id'])) {
277 DI::mstdnError()->RecordNotFound();
280 if (!Item::markForDeletionById($item['id'])) {
281 DI::mstdnError()->RecordNotFound();
284 System::jsonExit([]);
288 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
290 protected function rawContent(array $request = [])
292 $uid = self::getCurrentUserID();
294 if (empty($this->parameters['id'])) {
295 DI::mstdnError()->UnprocessableEntity();
298 System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid, false));
301 private function getApp(): string
303 if (!empty(self::getCurrentApplication()['name'])) {
304 return self::getCurrentApplication()['name'];