From 2e6c5d2ad23e41269762fd356df9ce1f0740e1f8 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 11 Dec 2020 13:31:35 -0500 Subject: [PATCH] Query Miniflux categories --- lib/REST/Miniflux/V1.php | 25 +++++++++++++++++++------ lib/User.php | 21 +++++++++++---------- locale/en.php | 13 +++++++------ tests/cases/REST/Miniflux/TestV1.php | 4 ++-- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/lib/REST/Miniflux/V1.php b/lib/REST/Miniflux/V1.php index 1107b600..ec82dcaa 100644 --- a/lib/REST/Miniflux/V1.php +++ b/lib/REST/Miniflux/V1.php @@ -128,7 +128,7 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { $data = @json_decode((string) $req->getBody(), true); if (json_last_error() !== \JSON_ERROR_NONE) { // if the body could not be parsed as JSON, return "400 Bad Request" - return new ErrorResponse(["invalidBodyJSON", json_last_error_msg()], 400); + return new ErrorResponse(["InvalidBodyJSON", json_last_error_msg()], 400); } $data = $this->normalizeBody((array) $data); if ($data instanceof ResponseInterface) { @@ -215,7 +215,7 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { if (!isset($body[$k])) { $body[$k] = null; } elseif (gettype($body[$k]) !== $t) { - return new ErrorResponse(["invalidInputType", 'field' => $k, 'expected' => $t, 'actual' => gettype($body[$k])]); + return new ErrorResponse(["InvalidInputType", 'field' => $k, 'expected' => $t, 'actual' => gettype($body[$k])]); } } return $body; @@ -260,10 +260,10 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { $list = Feed::discoverAll((string) $data['url'], (string) $data['username'], (string) $data['password']); } catch (FeedException $e) { $msg = [ - 10502 => "fetch404", - 10506 => "fetch403", - 10507 => "fetch401", - ][$e->getCode()] ?? "fetchOther"; + 10502 => "Fetch404", + 10506 => "Fetch403", + 10507 => "Fetch401", + ][$e->getCode()] ?? "FetchOther"; return new ErrorResponse($msg, 500); } $out = []; @@ -299,6 +299,19 @@ class V1 extends \JKingWeb\Arsse\REST\AbstractHandler { return new Response($this->listUsers([Arsse::$user->id], false)[0] ?? new \stdClass); } + protected function getCategories(array $path, array $query, array $data) { + $out = []; + $meta = Arsse::$user->propertiesGet(Arsse::$user->id, false); + // add the root folder as a category + $out[] = ['id' => 1, 'title' => $meta['root_folder_name'] ?? Arsse::$lang->msg("API.Miniflux.DefaultCategoryName"), 'user_id' => $meta['num']]; + // add other top folders as categories + foreach (Arsse::$db->folderList(Arsse::$user->id, null, false) as $f) { + // always add 1 to the ID since the root folder will always be 1 instead of 0. + $out[] = ['id' => $f['id'] + 1, 'title' => $f['name'], 'user_id' => $meta['num']]; + } + return new Response($out); + } + public static function tokenGenerate(string $user, string $label): string { // Miniflux produces tokens in base64url alphabet $t = str_replace(["+", "/"], ["-", "_"], base64_encode(random_bytes(self::TOKEN_LENGTH))); diff --git a/lib/User.php b/lib/User.php index 5ab1b111..df9a49d9 100644 --- a/lib/User.php +++ b/lib/User.php @@ -15,16 +15,17 @@ class User { 'internal' => \JKingWeb\Arsse\User\Internal\Driver::class, ]; public const PROPERTIES = [ - 'admin' => V::T_BOOL, - 'lang' => V::T_STRING, - 'tz' => V::T_STRING, - 'sort_asc' => V::T_BOOL, - 'theme' => V::T_STRING, - 'page_size' => V::T_INT, // greater than zero - 'shortcuts' => V::T_BOOL, - 'gestures' => V::T_BOOL, - 'stylesheet' => V::T_STRING, - 'reading_time' => V::T_BOOL, + 'admin' => V::T_BOOL, + 'lang' => V::T_STRING, + 'tz' => V::T_STRING, + 'sort_asc' => V::T_BOOL, + 'theme' => V::T_STRING, + 'page_size' => V::T_INT, // greater than zero + 'shortcuts' => V::T_BOOL, + 'gestures' => V::T_BOOL, + 'stylesheet' => V::T_STRING, + 'reading_time' => V::T_BOOL, + 'root_folder_name' => V::T_STRING, ]; public const PROPERTIES_LARGE = ["stylesheet"]; diff --git a/locale/en.php b/locale/en.php index 75b52e5f..0e03fd97 100644 --- a/locale/en.php +++ b/locale/en.php @@ -7,15 +7,16 @@ return [ 'CLI.Auth.Success' => 'Authentication successful', 'CLI.Auth.Failure' => 'Authentication failed', + 'API.Miniflux.DefaultCategoryName' => "All", 'API.Miniflux.Error.401' => 'Access Unauthorized', 'API.Miniflux.Error.403' => 'Access Forbidden', 'API.Miniflux.Error.404' => 'Resource Not Found', - 'API.Miniflux.Error.invalidBodyJSON' => 'Invalid JSON payload: {0}', - 'API.Miniflux.Error.invalidInputType' => 'Input key "{field}" of type {actual} was expected as {expected}', - 'API.Miniflux.Error.fetch404' => 'Resource not found (404), this feed doesn\'t exists anymore, check the feed URL', - 'API.Miniflux.Error.fetch401' => 'You are not authorized to access this resource (invalid username/password)', - 'API.Miniflux.Error.fetch403' => 'Unable to fetch this resource (Status Code = 403)', - 'API.Miniflux.Error.fetchOther' => 'Unable to fetch this resource', + 'API.Miniflux.Error.InvalidBodyJSON' => 'Invalid JSON payload: {0}', + 'API.Miniflux.Error.InvalidInputType' => 'Input key "{field}" of type {actual} was expected as {expected}', + 'API.Miniflux.Error.Fetch404' => 'Resource not found (404), this feed doesn\'t exists anymore, check the feed URL', + 'API.Miniflux.Error.Fetch401' => 'You are not authorized to access this resource (invalid username/password)', + 'API.Miniflux.Error.Fetch403' => 'Unable to fetch this resource (Status Code = 403)', + 'API.Miniflux.Error.FetchOther' => 'Unable to fetch this resource', 'API.TTRSS.Category.Uncategorized' => 'Uncategorized', 'API.TTRSS.Category.Special' => 'Special', diff --git a/tests/cases/REST/Miniflux/TestV1.php b/tests/cases/REST/Miniflux/TestV1.php index 89316153..4ac27344 100644 --- a/tests/cases/REST/Miniflux/TestV1.php +++ b/tests/cases/REST/Miniflux/TestV1.php @@ -163,7 +163,7 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { } public function testRejectBadlyTypedData(): void { - $exp = new ErrorResponse(["invalidInputType", 'field' => "url", 'expected' => "string", 'actual' => "integer"], 400); + $exp = new ErrorResponse(["InvalidInputType", 'field' => "url", 'expected' => "string", 'actual' => "integer"], 400); $this->assertMessage($exp, $this->req("POST", "/discover", ['url' => 2112])); } @@ -175,7 +175,7 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest { $this->assertMessage($exp, $this->req("POST", "/discover", ['url' => "http://localhost:8000/Feed/Discovery/Valid"])); $exp = new Response([]); $this->assertMessage($exp, $this->req("POST", "/discover", ['url' => "http://localhost:8000/Feed/Discovery/Invalid"])); - $exp = new ErrorResponse("fetch404", 500); + $exp = new ErrorResponse("Fetch404", 500); $this->assertMessage($exp, $this->req("POST", "/discover", ['url' => "http://localhost:8000/Feed/Discovery/Missing"])); }