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

@ -21,7 +21,6 @@
namespace Friendica\Module\Api\Mastodon;
use Friendica\App\Router;
use Friendica\Content\Text\Markdown;
use Friendica\Core\Protocol;
use Friendica\Core\System;
@ -35,6 +34,7 @@ use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
use Friendica\Util\Images;
@ -48,7 +48,47 @@ class Statuses extends BaseApi
self::checkAllowedScope(self::SCOPE_WRITE);
$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 = [])
@ -80,14 +120,7 @@ class Statuses extends BaseApi
$item['contact-id'] = $owner['id'];
$item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
$item['body'] = $body;
if (!empty(self::getCurrentApplication()['name'])) {
$item['app'] = self::getCurrentApplication()['name'];
}
if (empty($item['app'])) {
$item['app'] = 'API';
}
$item['app'] = $this->getApp();
switch ($request['visibility']) {
case 'public':
@ -257,4 +290,13 @@ class Statuses extends BaseApi
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());
}
}