gnu-social/plugins/Pinboard/Controller/API.php

134 lines
4.3 KiB
PHP

<?php
declare(strict_types = 1);
namespace Plugin\Pinboard\Controller;
use App\Core\Controller;
use App\Core\DB;
use App\Core\Router;
use App\Core\VisibilityScope;
use App\Entity\Note;
use App\Util\Exception\ClientException;
use App\Util\Exception\ServerException;
use App\Util\Formatting;
use Component\Conversation\Conversation;
use Datetime;
use Plugin\Pinboard\Entity\Pin;
use Plugin\Pinboard\Util;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class API extends Controller
{
private function before()
{
$format = $this->string('format');
$auth_token = $this->string('auth_token');
if ($format !== 'json') {
throw new ServerException(_m('Only JSON is supported'));
}
if (\is_null($auth_token)) {
return new Response('API requires authentication', status: 401);
}
if (\is_null($user = Util::validateToken($auth_token))) {
return new Response('401 Forbidden', status: 401);
}
return $user;
}
/**
* Last post update
*/
public function posts_update(Request $request)
{
self::before();
return new JsonResponse(
['update_time' => date(\DATE_ISO8601)], // TODO fetch latest and reply accordingly
status: 200,
headers: ['content-type' => 'application/json'],
);
}
/**
* Add a pin
*/
public function posts_add(Request $request)
{
$user = self::before();
if (\is_null($url = $this->string('url'))) {
throw new ClientException('URL must be provided');
}
if (\is_null($title = $this->string('description'))) { // Logically.
throw new ClientException('Desciption must be provided');
}
$description = $this->string('extended') ?? '';
$tags = [];
if (!\is_null($tags_text = $this->string('tags'))) {
Formatting::toArray($tags_text, $tags, Formatting::SPLIT_BY_BOTH);
}
$modified = $this->string('dt') ?? new Datetime;
$replace = $this->bool('replace') ?? true;
$public = $this->bool('shared') ?? true;
$unread = $this->bool('toread') ?? false;
$result_code = 'something went wrong';
[$pin, $existed] = Pin::checkExistingAndCreateOrUpdate(
args: [
'actor_id' => $user->getId(),
'url_hash' => hash('sha256', $url),
'url' => $url,
'replace' => $replace,
'public' => $public,
'unread' => $unread,
'modified' => $modified,
],
find_by_keys: ['actor_id', 'url_hash'],
);
if ($existed) {
if (!$replace) {
$result_code = 'item already exists';
} else {
throw new ServerException('Updating is unimplemented'); // TODO delete old note, create new one
}
} else {
DB::persist($note = Note::create([
'actor_id' => $user->getId(),
'content' => $url,
'content_type' => 'text/uri-list',
'rendered' => Formatting::twigRenderFile('pinboard/render.html.twig', ['url' => $url, 'title' => $title, 'description' => $description]),
'reply_to' => null,
'is_local' => true,
'source' => 'Pinboard API',
'scope' => $public ? VisibilityScope::EVERYWHERE->value : VisibilityScope::ADDRESSEE->value,
'language_id' => $user->getActor()->getTopLanguage()->getId(),
'type' => 'pin',
'title' => $title,
]));
$note->setUrl(Router::url('note_view', ['id' => $note->getId()], Router::ABSOLUTE_URL));
$pin->setNoteId($note->getId());
Conversation::assignLocalConversation($note, null);
// TODO handle tags
DB::flush();
$result_code = 'done';
}
return new JsonResponse(
['result_code' => $result_code],
status: 200,
headers: ['content-type' => 'application/json'],
);
}
}