Merge pull request #12269 from annando/api-edit

Issue 12192: API: Enable edit
This commit is contained in:
Hypolite Petovan 2022-11-25 19:22:27 -05:00 committed by GitHub
commit 29ee2473d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 239 additions and 29 deletions

View File

@ -104,24 +104,26 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/en
- [`DELETE /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) - [`DELETE /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/)
- [`GET /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/) - [`GET /api/v1/scheduled_statuses/:id`](https://docs.joinmastodon.org/methods/statuses/scheduled_statuses/)
- [`GET /api/v1/search`](https://docs.joinmastodon.org/methods/search/) - [`GET /api/v1/search`](https://docs.joinmastodon.org/methods/search/)
- [`POST /api/v1/statuses`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses`](https://docs.joinmastodon.org/methods/statuses/#create)
- Additionally to the static values `public`, `unlisted` and `private`, the `visibility` parameter can contain a numeric value with a group id. - Additionally to the static values `public`, `unlisted` and `private`, the `visibility` parameter can contain a numeric value with a group id.
- [`GET /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/) - [`GET /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/#get)
- [`DELETE /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/) - [`DELETE /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/#delete)
- [`GET /api/v1/statuses/:id/card`](https://docs.joinmastodon.org/methods/statuses/) - [`GET /api/v1/statuses/:id/context`](https://docs.joinmastodon.org/methods/statuses/#context)
- [`GET /api/v1/statuses/:id/context`](https://docs.joinmastodon.org/methods/statuses/) - [`GET /api/v1/statuses/:id/reblogged_by`](https://docs.joinmastodon.org/methods/statuses/#reblogged_by)
- [`GET /api/v1/statuses/:id/reblogged_by`](https://docs.joinmastodon.org/methods/statuses/) - [`GET /api/v1/statuses/:id/favourited_by`](https://docs.joinmastodon.org/methods/statuses/#favourited_by)
- [`GET /api/v1/statuses/:id/favourited_by`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/favourite`](https://docs.joinmastodon.org/methods/statuses/#favourite)
- [`POST /api/v1/statuses/:id/favourite`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/unfavourite`](https://docs.joinmastodon.org/methods/statuses/#unfavourite)
- [`POST /api/v1/statuses/:id/unfavourite`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/reblog`](https://docs.joinmastodon.org/methods/statuses/#boost)
- [`POST /api/v1/statuses/:id/reblog`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/unreblog`](https://docs.joinmastodon.org/methods/statuses/#unreblog)
- [`POST /api/v1/statuses/:id/unreblog`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/bookmark`](https://docs.joinmastodon.org/methods/statuses/#bookmark)
- [`POST /api/v1/statuses/:id/bookmark`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/unbookmark`](https://docs.joinmastodon.org/methods/statuses/#unbookmark)
- [`POST /api/v1/statuses/:id/unbookmark`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/mute`](https://docs.joinmastodon.org/methods/statuses/#mute)
- [`POST /api/v1/statuses/:id/mute`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/unmute`](https://docs.joinmastodon.org/methods/statuses/#unmute)
- [`POST /api/v1/statuses/:id/unmute`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/pin`](https://docs.joinmastodon.org/methods/statuses/#pin)
- [`POST /api/v1/statuses/:id/pin`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id/unpin`](https://docs.joinmastodon.org/methods/statuses/#unpin)
- [`POST /api/v1/statuses/:id/unpin`](https://docs.joinmastodon.org/methods/statuses/) - [`POST /api/v1/statuses/:id`](https://docs.joinmastodon.org/methods/statuses/#edit)
- [`GET /api/v1/statuses/:id/source`](https://docs.joinmastodon.org/methods/statuses/#source)
- [`GET /api/v1/statuses/:id/card`](https://docs.joinmastodon.org/methods/statuses/#card)
- [`GET /api/v1/suggestions`](https://docs.joinmastodon.org/methods/accounts/suggestions/) - [`GET /api/v1/suggestions`](https://docs.joinmastodon.org/methods/accounts/suggestions/)
- [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/direct`](https://docs.joinmastodon.org/methods/timelines/)
- [`GET /api/v1/timelines/home`](https://docs.joinmastodon.org/methods/timelines/) - [`GET /api/v1/timelines/home`](https://docs.joinmastodon.org/methods/timelines/)
@ -144,7 +146,6 @@ These emdpoints are planned to be implemented somewhere in the future.
- [`GET /api/v1/trends/statuses`](https://github.com/mastodon/mastodon/pull/17431) - [`GET /api/v1/trends/statuses`](https://github.com/mastodon/mastodon/pull/17431)
- [`GET /api/v1/trends/tags`](https://github.com/mastodon/mastodon/pull/16917) - [`GET /api/v1/trends/tags`](https://github.com/mastodon/mastodon/pull/16917)
- [`POST /api/v1/polls/:id/votes`](https://docs.joinmastodon.org/methods/statuses/polls/) - [`POST /api/v1/polls/:id/votes`](https://docs.joinmastodon.org/methods/statuses/polls/)
- [`GET /api/v1/statuses/{id:\d+}/source`](https://github.com/mastodon/mastodon/pull/16697)
- [`GET /api/v1/featured_tags`](https://docs.joinmastodon.org/methods/accounts/featured_tags/) - [`GET /api/v1/featured_tags`](https://docs.joinmastodon.org/methods/accounts/featured_tags/)
- [`POST /api/v1/featured_tags`](https://docs.joinmastodon.org/methods/accounts/featured_tags/) - [`POST /api/v1/featured_tags`](https://docs.joinmastodon.org/methods/accounts/featured_tags/)
- [`DELETE /api/v1/featured_tags/:id`](https://docs.joinmastodon.org/methods/accounts/featured_tags/) - [`DELETE /api/v1/featured_tags/:id`](https://docs.joinmastodon.org/methods/accounts/featured_tags/)

View File

@ -379,6 +379,14 @@ abstract class DI
return self::$dice->create(Factory\Api\Mastodon\Status::class); return self::$dice->create(Factory\Api\Mastodon\Status::class);
} }
/**
* @return Factory\Api\Mastodon\StatusSource
*/
public static function mstdnStatusSource()
{
return self::$dice->create(Factory\Api\Mastodon\StatusSource::class);
}
/** /**
* @return Factory\Api\Mastodon\ScheduledStatus * @return Factory\Api\Mastodon\ScheduledStatus
*/ */

View File

@ -0,0 +1,48 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Mastodon;
use Friendica\BaseFactory;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Protocol;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
class StatusSource extends BaseFactory
{
/**
* @param int $uriId Uri-ID of the item
*
* @return \Friendica\Object\Api\Mastodon\StatusSource
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException*@throws \Exception
*/
public function createFromUriId(int $uriId, int $uid): \Friendica\Object\Api\Mastodon\StatusSource
{
$post = Post::selectFirst(['uri-id', 'raw-body', 'body', 'title'], ['uri-id' => $uriId, 'uid' => [0, $uid]]);
$spoiler_text = $post['title'] ?: BBCode::toPlaintext(BBCode::getAbstract($post['body'], Protocol::ACTIVITYPUB));
$body = BBCode::toMarkdown($post['body']);
return new \Friendica\Object\Api\Mastodon\StatusSource($post['uri-id'], $body, $spoiler_text);
}
}

View File

@ -21,7 +21,6 @@
namespace Friendica\Module\Api\Mastodon; namespace Friendica\Module\Api\Mastodon;
use Friendica\App\Router;
use Friendica\Content\Text\Markdown; use Friendica\Content\Text\Markdown;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\System; use Friendica\Core\System;
@ -35,6 +34,7 @@ use Friendica\Model\Photo;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Util\Images; use Friendica\Util\Images;
@ -48,7 +48,47 @@ class Statuses extends BaseApi
self::checkAllowedScope(self::SCOPE_WRITE); self::checkAllowedScope(self::SCOPE_WRITE);
$uid = self::getCurrentUserID(); $uid = self::getCurrentUserID();
$this->response->unsupported(Router::PUT, $request); $request = $this->getRequest([
'status' => '', // Text content of the status. If media_ids is provided, this becomes optional. Attaching a poll is optional while status is provided.
'in_reply_to_id' => 0, // ID of the status being replied to, if status is a reply
'spoiler_text' => '', // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
'language' => '', // ISO 639 language code for this status.
], $request);
$owner = User::getOwnerDataById($uid);
$condition = [
'uid' => $uid,
'uri-id' => $this->parameters['id'],
'contact-id' => $owner['id'],
'author-id' => Contact::getPublicIdByUserId($uid),
'origin' => true,
];
$post = Post::selectFirst(['uri-id', 'id'], $condition);
if (empty($post['id'])) {
throw new HTTPException\NotFoundException('Item with URI ID ' . $this->parameters['id'] . ' not found for user ' . $uid . '.');
}
// The imput is defined as text. So we can use Markdown for some enhancements
$item = ['body' => Markdown::toBBCode($request['status']), 'app' => $this->getApp()];
if (!empty($request['language'])) {
$item['language'] = json_encode([$request['language'] => 1]);
}
if (!empty($request['spoiler_text'])) {
if ($request['in_reply_to_id'] != $post['uri-id']) {
$item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $request['spoiler_text'] . "[/abstract]\n" . $item['body'];
} else {
$item['title'] = $request['spoiler_text'];
}
}
Item::update($item, ['id' => $post['id']]);
Item::updateDisplayCache($post['uri-id']);
System::jsonExit(DI::mstdnStatus()->createFromUriId($post['uri-id'], $uid));
} }
protected function post(array $request = []) protected function post(array $request = [])
@ -80,14 +120,7 @@ class Statuses extends BaseApi
$item['contact-id'] = $owner['id']; $item['contact-id'] = $owner['id'];
$item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid); $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
$item['body'] = $body; $item['body'] = $body;
$item['app'] = $this->getApp();
if (!empty(self::getCurrentApplication()['name'])) {
$item['app'] = self::getCurrentApplication()['name'];
}
if (empty($item['app'])) {
$item['app'] = 'API';
}
switch ($request['visibility']) { switch ($request['visibility']) {
case 'public': case 'public':
@ -257,4 +290,13 @@ class Statuses extends BaseApi
System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid)); System::jsonExit(DI::mstdnStatus()->createFromUriId($this->parameters['id'], $uid));
} }
private function getApp(): string
{
if (!empty(self::getCurrentApplication()['name'])) {
return self::getCurrentApplication()['name'];
} else {
return 'API';
}
}
} }

View File

@ -0,0 +1,57 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Mastodon\Statuses;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
/**
* @see https://docs.joinmastodon.org/methods/statuses/#source
*/
class Source extends BaseApi
{
/**
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
protected function rawContent(array $request = [])
{
self::checkAllowedScope(self::SCOPE_READ);
$uid = self::getCurrentUserID();
if (empty($this->parameters['id'])) {
DI::mstdnError()->UnprocessableEntity();
}
$id = $this->parameters['id'];
if (!Post::exists(['uri-id' => $id, 'uid' => [0, $uid]])) {
throw new HTTPException\NotFoundException('Item with URI ID ' . $id . ' not found' . ($uid ? ' for user ' . $uid : '.'));
}
$source = DI::mstdnStatusSource()->createFromUriId($id, $uid);
System::jsonExit($source->toArray());
}
}

View File

@ -0,0 +1,54 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Object\Api\Mastodon;
use Friendica\BaseDataTransferObject;
/**
* Class StatusSource
*
* @see https://docs.joinmastodon.org/entities/StatusSource/
*/
class StatusSource extends BaseDataTransferObject
{
/** @var string */
protected $id;
/** @var string */
protected $text;
/** @var string */
protected $spoiler_text = "";
/**
* Creates a source record from an post array.
*
* @param integer $id
* @param string $text
* @param string $spoiler_text
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(int $id, string $text, string $spoiler_text)
{
$this->id = (string)$id;
$this->text = $text;
$this->spoiler_text = $spoiler_text;
}
}

View File

@ -277,7 +277,7 @@ return [
'/statuses/{id:\d+}/pin' => [Module\Api\Mastodon\Statuses\Pin::class, [ R::POST]], '/statuses/{id:\d+}/pin' => [Module\Api\Mastodon\Statuses\Pin::class, [ R::POST]],
'/statuses/{id:\d+}/unpin' => [Module\Api\Mastodon\Statuses\Unpin::class, [ R::POST]], '/statuses/{id:\d+}/unpin' => [Module\Api\Mastodon\Statuses\Unpin::class, [ R::POST]],
'/statuses/{id:\d+}/history' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented '/statuses/{id:\d+}/history' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented
'/statuses/{id:\d+}/source' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented '/statuses/{id:\d+}/source' => [Module\Api\Mastodon\Statuses\Source::class, [R::GET ]],
'/streaming/direct' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented '/streaming/direct' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented
'/streaming/hashtag' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented '/streaming/hashtag' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented
'/streaming/hashtag/local' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented '/streaming/hashtag/local' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not implemented