3 * @copyright Copyright (C) 2010-2022, 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\App\Router;
25 use Friendica\Content\Text\Markdown;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\System;
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\Protocol\Activity;
38 use Friendica\Util\Images;
41 * @see https://docs.joinmastodon.org/methods/statuses/
43 class Statuses extends BaseApi
45 public function put(array $request = [])
47 self::checkAllowedScope(self::SCOPE_WRITE);
48 $uid = self::getCurrentUserID();
50 $this->response->unsupported(Router::PUT, $request);
53 protected function post(array $request = [])
55 self::checkAllowedScope(self::SCOPE_WRITE);
56 $uid = self::getCurrentUserID();
58 $request = $this->getRequest([
59 'status' => '', // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
60 'media_ids' => [], // Array of Attachment ids to be attached as media. If provided, status becomes optional, and poll cannot be used.
61 'poll' => [], // Poll data. If provided, media_ids cannot be used, and poll[expires_in] must be provided.
62 'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply
63 'sensitive' => false, // Mark status and attached media as sensitive?
64 'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
65 'visibility' => '', // Visibility of the posted status. One of: "public", "unlisted", "private" or "direct".
66 '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.
67 'language' => '', // ISO 639 language code for this status.
70 $owner = User::getOwnerDataById($uid);
72 // The imput is defined as text. So we can use Markdown for some enhancements
73 $body = Markdown::toBBCode($request['status']);
76 $item['network'] = Protocol::DFRN;
78 $item['verb'] = Activity::POST;
79 $item['contact-id'] = $owner['id'];
80 $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
81 $item['body'] = $body;
83 if (!empty(self::getCurrentApplication()['name'])) {
84 $item['app'] = self::getCurrentApplication()['name'];
87 if (empty($item['app'])) {
91 switch ($request['visibility']) {
93 $item['allow_cid'] = '';
94 $item['allow_gid'] = '';
95 $item['deny_cid'] = '';
96 $item['deny_gid'] = '';
97 $item['private'] = Item::PUBLIC;
100 $item['allow_cid'] = '';
101 $item['allow_gid'] = '';
102 $item['deny_cid'] = '';
103 $item['deny_gid'] = '';
104 $item['private'] = Item::UNLISTED;
107 if (!empty($owner['allow_cid'] . $owner['allow_gid'] . $owner['deny_cid'] . $owner['deny_gid'])) {
108 $item['allow_cid'] = $owner['allow_cid'];
109 $item['allow_gid'] = $owner['allow_gid'];
110 $item['deny_cid'] = $owner['deny_cid'];
111 $item['deny_gid'] = $owner['deny_gid'];
113 $item['allow_cid'] = '';
114 $item['allow_gid'] = '<' . Group::FOLLOWERS . '>';
115 $item['deny_cid'] = '';
116 $item['deny_gid'] = '';
118 $item['private'] = Item::PRIVATE;
121 // The permissions are assigned in "expandTags"
124 if (is_numeric($request['visibility']) && Group::exists($request['visibility'], $uid)) {
125 $item['allow_cid'] = '';
126 $item['allow_gid'] = '<' . $request['visibility'] . '>';
127 $item['deny_cid'] = '';
128 $item['deny_gid'] = '';
130 $item['allow_cid'] = $owner['allow_cid'];
131 $item['allow_gid'] = $owner['allow_gid'];
132 $item['deny_cid'] = $owner['deny_cid'];
133 $item['deny_gid'] = $owner['deny_gid'];
136 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
137 $item['private'] = Item::PRIVATE;
138 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
139 $item['private'] = Item::UNLISTED;
141 $item['private'] = Item::PUBLIC;
146 if (!empty($request['language'])) {
147 $item['language'] = json_encode([$request['language'] => 1]);
150 if ($request['in_reply_to_id']) {
151 $parent = Post::selectFirst(['uri'], ['uri-id' => $request['in_reply_to_id'], 'uid' => [0, $uid]]);
153 $item['thr-parent'] = $parent['uri'];
154 $item['gravity'] = GRAVITY_COMMENT;
155 $item['object-type'] = Activity\ObjectType::COMMENT;
156 $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $request['spoiler_text'] . "[/abstract]\n" . $item['body'];
158 self::checkThrottleLimit();
160 $item['gravity'] = GRAVITY_PARENT;
161 $item['object-type'] = Activity\ObjectType::NOTE;
162 $item['title'] = $request['spoiler_text'];
165 $item = DI::contentItem()->expandTags($item, $request['visibility'] == 'direct');
167 if (!empty($request['media_ids'])) {
168 $item['object-type'] = Activity\ObjectType::IMAGE;
169 $item['post-type'] = Item::PT_IMAGE;
170 $item['attachments'] = [];
172 foreach ($request['media_ids'] as $id) {
173 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
174 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
175 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
181 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
183 $ressources[] = $media[0]['resource-id'];
184 $phototypes = Images::supportedTypes();
185 $ext = $phototypes[$media[0]['type']];
187 $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
188 'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
189 'size' => $media[0]['datasize'],
190 'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
191 'description' => $media[0]['desc'] ?? '',
192 'width' => $media[0]['width'],
193 'height' => $media[0]['height']];
195 if (count($media) > 1) {
196 $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
197 $attachment['preview-width'] = $media[1]['width'];
198 $attachment['preview-height'] = $media[1]['height'];
200 $item['attachments'][] = $attachment;
204 if (!empty($request['scheduled_at'])) {
205 $item['guid'] = Item::guid($item, true);
206 $item['uri'] = Item::newURI($item['uid'], $item['guid']);
207 $id = Post\Delayed::add($item['uri'], $item, PRIORITY_HIGH, Post\Delayed::PREPARED, $request['scheduled_at']);
209 DI::mstdnError()->InternalError();
211 System::jsonExit(DI::mstdnScheduledStatus()->createFromDelayedPostId($id, $uid)->toArray());
214 $id = Item::insert($item, true);
216 $item = Post::selectFirst(['uri-id'], ['id' => $id]);
217 if (!empty($item['uri-id'])) {
218 System::jsonExit(DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid));
222 DI::mstdnError()->InternalError();
225 protected function delete(array $request = [])
227 self::checkAllowedScope(self::SCOPE_READ);
228 $uid = self::getCurrentUserID();
230 if (empty($this->parameters['id'])) {
231 DI::mstdnError()->UnprocessableEntity();
234 $item = Post::selectFirstForUser($uid, ['id'], ['uri-id' => $this->parameters['id'], 'uid' => $uid]);
235 if (empty($item['id'])) {
236 DI::mstdnError()->RecordNotFound();
239 if (!Item::markForDeletionById($item['id'])) {
240 DI::mstdnError()->RecordNotFound();
243 System::jsonExit([]);
247 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
249 protected function rawContent(array $request = [])
251 $uid = self::getCurrentUserID();
253 if (empty($this->parameters['id'])) {
254 DI::mstdnError()->UnprocessableEntity();
257 System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid));