diff --git a/lib/REST/TinyTinyRSS/API.php b/lib/REST/TinyTinyRSS/API.php index 2fed94a..3efef8d 100644 --- a/lib/REST/TinyTinyRSS/API.php +++ b/lib/REST/TinyTinyRSS/API.php @@ -6,6 +6,7 @@ use JKingWeb\Arsse\Feed; use JKingWeb\Arsse\Arsse; use JKingWeb\Arsse\User; use JKingWeb\Arsse\Service; +use JKingWeb\Arsse\Misc\Date; use JKingWeb\Arsse\Misc\Context; use JKingWeb\Arsse\Misc\ValueInfo; use JKingWeb\Arsse\AbstractException; @@ -147,6 +148,80 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { return ['status' => true]; } + public function opGetCategories(array $data): array { + // normalize input + $all = isset($data['include_empty']) ? ValueInfo::bool($data['include_empty'], false) : false; + $read = !(isset($data['unread_only']) ? ValueInfo::bool($data['unread_only'], false) : false); + $deep = !(isset($data['enable_nested']) ? ValueInfo::bool($data['enable_nested'], false) : false); + $user = Arsse::$user->id; + // for each category, add the ID to a lookup table, set the number of unread and feeds to zero, and assign an increasing order index + $cats = Arsse::$db->folderList($user, null, $deep)->getAll(); + $map = []; + for ($a = 0; $a < sizeof($cats); $a++) { + $map[$cats[$a]['id']] = $a; + $cats[$a]['unread'] = 0; + $cats[$a]['feeds'] = 0; + $cats[$a]['order'] = $a + 1; + } + // add the "Uncategorized", "Special", and "Labels" virtual categories to the list + $map[0] = sizeof($cats); + $cats[] = ['id' => 0, 'name' => Arsse::$lang->msg("API.TTRSS.Category.Uncategorized"), 'children' => 0, 'unread' => 0, 'feeds' => 0]; + $map[-1] = sizeof($cats); + $cats[] = ['id' => -1, 'name' => Arsse::$lang->msg("API.TTRSS.Category.Special"), 'children' => 0, 'unread' => 0, 'feeds' => 6]; + $map[-2] = sizeof($cats); + $cats[] = ['id' => -2, 'name' => Arsse::$lang->msg("API.TTRSS.Category.Labels"), 'children' => 0, 'unread' => 0, 'feeds' => 0]; + // for each subscription, add the unread count to its category, and increment the category's feed count + $subs = Arsse::$db->subscriptionList($user); + foreach ($subs as $sub) { + // note we use top_folder if we're in "nested" mode + $f = $map[(int) ($deep ? $sub['folder'] : $sub['top_folder'])]; + $cats[$f]['unread'] += $sub['unread']; + $cats[$f]['feeds'] += 1; + } + // for each label, add the unread count to the labels category, and increment the labels category's feed count + $labels = Arsse::$db->labelList($user); + $f = $map[-2]; + foreach ($labels as $label) { + $cats[$f]['unread'] += $label['articles'] - $label['read']; + $cats[$f]['feeds'] += 1; + } + // get the unread counts for the special feeds + // FIXME: this is pretty inefficient + $f = $map[-1]; + $cats[$f]['unread'] += Arsse::$db->articleCount($user, (new Context)->unread(true)->starred(true)); // starred + $cats[$f]['unread'] += Arsse::$db->articleCount($user, (new Context)->unread(true)->modifiedSince(Date::sub("PT24H"))); // fresh + if (!$read) { + // if we're only including unread entries, remove any categories with zero unread items (this will by definition also exclude empties) + $count = sizeof($cats); + for ($a = 0; $a < $count; $a++) { + if (!$cats[$a]['unread']) { + unset($cats[$a]); + } + } + $cats = array_values($cats); + } elseif (!$all) { + // otherwise if we're not including empty entries, remove categories with no children and no feeds + $count = sizeof($cats); + for ($a = 0; $a < $count; $a++) { + if (($cats[$a]['children'] + $cats[$a]['feeds']) < 1) { + unset($cats[$a]); + } + } + $cats = array_values($cats); + } + // transform the result and return + $out = []; + for ($a = 0; $a < sizeof($cats); $a++) { + $out[] = $this->fieldMapNames($cats[$a], [ + 'id' => "id", + 'title' => "name", + 'unread' => "unread", + 'order_id' => "order", + ]); + } + return $out; + } + public function opAddCategory(array $data) { $in = [ 'name' => isset($data['caption']) ? $data['caption'] : "", diff --git a/locale/en.php b/locale/en.php index 1600c7b..0539a0f 100644 --- a/locale/en.php +++ b/locale/en.php @@ -1,5 +1,9 @@ 'Uncategorized', + 'API.TTRSS.Category.Special' => 'Special', + 'API.TTRSS.Category.Labels' => 'Labels', + 'Driver.Db.SQLite3.Name' => 'SQLite 3', 'Driver.Service.Curl.Name' => 'HTTP (curl)', 'Driver.Service.Internal.Name' => 'Internal', diff --git a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php index 0294b1d..ad383b7 100644 --- a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php +++ b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php @@ -15,134 +15,6 @@ use Phake; * @covers \JKingWeb\Arsse\REST\TinyTinyRSS\Exception */ class TestTinyTinyAPI extends Test\AbstractTest { protected $h; - protected $feeds = [ // expected sample output of a feed list from the database, and the resultant expected transformation by the REST handler - 'db' => [ - [ - 'id' => 2112, - 'url' => 'http://example.com/news.atom', - 'favicon' => 'http://example.com/favicon.png', - 'source' => 'http://example.com/', - 'folder' => null, - 'top_folder' => null, - 'pinned' => 0, - 'err_count' => 0, - 'err_msg' => '', - 'order_type' => 0, - 'added' => '2017-05-20 13:35:54', - 'title' => 'First example feed', - 'unread' => 50048, - ], - [ - 'id' => 42, - 'url' => 'http://example.org/news.atom', - 'favicon' => 'http://example.org/favicon.png', - 'source' => 'http://example.org/', - 'folder' => 12, - 'top_folder' => 8, - 'pinned' => 1, - 'err_count' => 0, - 'err_msg' => '', - 'order_type' => 2, - 'added' => '2017-05-20 13:35:54', - 'title' => 'Second example feed', - 'unread' => 23, - ], - ], - ]; - protected $articles = [ - 'db' => [ - [ - 'id' => 101, - 'url' => 'http://example.com/1', - 'title' => 'Article title 1', - 'author' => '', - 'content' => '

Article content 1

', - 'guid' => 'e433653cef2e572eee4215fa299a4a5af9137b2cefd6283c85bd69a32915beda', - 'published_date' => '2000-01-01 00:00:00', - 'edited_date' => '2000-01-01 00:00:01', - 'modified_date' => '2000-01-01 01:00:00', - 'unread' => 1, - 'starred' => 0, - 'edition' => 101, - 'subscription' => 8, - 'fingerprint' => 'f5cb8bfc1c7396dc9816af212a3e2ac5221585c2a00bf7ccb6aabd95dcfcd6a6:fb0bc8f8cb08913dc5a497db700e327f1d34e4987402687d494a5891f24714d4:18fdd4fa93d693128c43b004399e5c9cea6c261ddfa002518d3669f55d8c2207', - 'media_url' => null, - 'media_type' => null, - ], - [ - 'id' => 102, - 'url' => 'http://example.com/2', - 'title' => 'Article title 2', - 'author' => '', - 'content' => '

Article content 2

', - 'guid' => '5be8a5a46ecd52ed132191c8d27fb1af6b3d4edc00234c5d9f8f0e10562ed3b7', - 'published_date' => '2000-01-02 00:00:00', - 'edited_date' => '2000-01-02 00:00:02', - 'modified_date' => '2000-01-02 02:00:00', - 'unread' => 0, - 'starred' => 0, - 'edition' => 202, - 'subscription' => 8, - 'fingerprint' => '0e86d2de822a174fe3c44a466953e63ca1f1a58a19cbf475fce0855d4e3d5153:13075894189c47ffcfafd1dfe7fbb539f7c74a69d35a399b3abf8518952714f9:2abd0a8cba83b8214a66c8f0293ba63e467d720540e29ff8ddcdab069d4f1c9e', - 'media_url' => "http://example.com/text", - 'media_type' => "text/plain", - ], - [ - 'id' => 103, - 'url' => 'http://example.com/3', - 'title' => 'Article title 3', - 'author' => '', - 'content' => '

Article content 3

', - 'guid' => '31a6594500a48b59fcc8a075ce82b946c9c3c782460d088bd7b8ef3ede97ad92', - 'published_date' => '2000-01-03 00:00:00', - 'edited_date' => '2000-01-03 00:00:03', - 'modified_date' => '2000-01-03 03:00:00', - 'unread' => 1, - 'starred' => 1, - 'edition' => 203, - 'subscription' => 9, - 'fingerprint' => 'f74b06b240bd08abf4d3fdfc20dba6a6f6eb8b4f1a00e9a617efd63a87180a4b:b278380e984cefe63f0e412b88ffc9cb0befdfa06fdc00bace1da99a8daff406:ad622b31e739cd3a3f3c788991082cf4d2f7a8773773008e75f0572e58cd373b', - 'media_url' => "http://example.com/video", - 'media_type' => "video/webm", - ], - [ - 'id' => 104, - 'url' => 'http://example.com/4', - 'title' => 'Article title 4', - 'author' => '', - 'content' => '

Article content 4

', - 'guid' => '804e517d623390e71497982c77cf6823180342ebcd2e7d5e32da1e55b09dd180', - 'published_date' => '2000-01-04 00:00:00', - 'edited_date' => '2000-01-04 00:00:04', - 'modified_date' => '2000-01-04 04:00:00', - 'unread' => 0, - 'starred' => 1, - 'edition' => 204, - 'subscription' => 9, - 'fingerprint' => 'f3615c7f16336d3ea242d35cf3fc17dbc4ee3afb78376bf49da2dd7a5a25dec8:f11c2b4046f207579aeb9c69a8c20ca5461cef49756ccfa5ba5e2344266da3b3:ab2da63276acce431250b18d3d49b988b226a99c7faadf275c90b751aee05be9', - 'media_url' => "http://example.com/image", - 'media_type' => "image/svg+xml", - ], - [ - 'id' => 105, - 'url' => 'http://example.com/5', - 'title' => 'Article title 5', - 'author' => '', - 'content' => '

Article content 5

', - 'guid' => 'db3e736c2c492f5def5c5da33ddcbea1824040e9ced2142069276b0a6e291a41', - 'published_date' => '2000-01-05 00:00:00', - 'edited_date' => '2000-01-05 00:00:05', - 'modified_date' => '2000-01-05 05:00:00', - 'unread' => 1, - 'starred' => 0, - 'edition' => 305, - 'subscription' => 10, - 'fingerprint' => 'd40da96e39eea6c55948ccbe9b3d275b5f931298288dbe953990c5f496097022:834240f84501b5341d375414718204ec421561f3825d34c22bf9182203e42900:43b970ac6ec5f8a9647b2c7e4eed8b1d7f62e154a95eed748b0294c1256764ba', - 'media_url' => "http://example.com/audio", - 'media_type' => "audio/ogg", - ], - ] - ]; protected function respGood($content = null, $seq = 0): Response { return new Response(200, [ @@ -748,4 +620,102 @@ class TestTinyTinyAPI extends Test\AbstractTest { $this->assertEquals($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[8])))); Phake::verify(Arsse::$db, Phake::times(6))->labelPropertiesSet(Arsse::$user->id, $this->anything(), $this->anything()); } + + public function testRetrieveCategoryLists() { + $folders = [ + ['id' => 5, 'parent' => 3, 'children' => 0, 'name' => "Local"], + ['id' => 6, 'parent' => 3, 'children' => 0, 'name' => "National"], + ['id' => 4, 'parent' => null, 'children' => 0, 'name' => "Photography"], + ['id' => 3, 'parent' => null, 'children' => 2, 'name' => "Politics"], + ['id' => 2, 'parent' => 1, 'children' => 0, 'name' => "Rocketry"], + ['id' => 1, 'parent' => null, 'children' => 1, 'name' => "Science"], + ]; + $topFolders = [ + ['id' => 4, 'parent' => null, 'children' => 0, 'name' => "Photography"], + ['id' => 3, 'parent' => null, 'children' => 2, 'name' => "Politics"], + ['id' => 1, 'parent' => null, 'children' => 1, 'name' => "Science"], + ]; + $subscriptions = [ + ['folder' => null, 'top_folder' => null, 'unread' => 0], + ['folder' => 1, 'top_folder' => 1, 'unread' => 2], + ['folder' => 2, 'top_folder' => 1, 'unread' => 5], + ['folder' => 5, 'top_folder' => 3, 'unread' => 10], + ['folder' => 6, 'top_folder' => 3, 'unread' => 12], + ['folder' => 6, 'top_folder' => 3, 'unread' => 6], + ]; + $labels = [ + ['articles' => 0, 'read' => 0], + ['articles' => 100, 'read' => 94], + ['articles' => 2, 'read' => 0], + ]; + Phake::when(Arsse::$db)->folderList($this->anything(), null, true)->thenReturn(new Result($folders)); + Phake::when(Arsse::$db)->folderList($this->anything(), null, false)->thenReturn(new Result($topFolders)); + Phake::when(Arsse::$db)->subscriptionList($this->anything())->thenReturn(new Result($subscriptions)); + Phake::when(Arsse::$db)->labelList($this->anything())->thenReturn(new Result($labels)); + Phake::when(Arsse::$db)->articleCount($this->anything(), $this->anything())->thenReturn(7); // FIXME: this should check an unread+modifiedSince context + Phake::when(Arsse::$db)->articleCount($this->anything(), (new Context)->unread(true)->starred(true))->thenReturn(4); + $in = [ + ['op' => "getCategories", 'sid' => "PriestsOfSyrinx", 'include_empty' => true], + ['op' => "getCategories", 'sid' => "PriestsOfSyrinx"], + ['op' => "getCategories", 'sid' => "PriestsOfSyrinx", 'unread_only' => true], + ['op' => "getCategories", 'sid' => "PriestsOfSyrinx", 'enable_nested' => true, 'include_empty' => true], + ['op' => "getCategories", 'sid' => "PriestsOfSyrinx", 'enable_nested' => true], + ['op' => "getCategories", 'sid' => "PriestsOfSyrinx", 'enable_nested' => true, 'unread_only' => true], + ]; + $exp = [ + [ + ['id' => 5, 'title' => "Local", 'unread' => 10, 'order_id' => 1], + ['id' => 6, 'title' => "National", 'unread' => 18, 'order_id' => 2], + ['id' => 4, 'title' => "Photography", 'unread' => 0, 'order_id' => 3], + ['id' => 3, 'title' => "Politics", 'unread' => 0, 'order_id' => 4], + ['id' => 2, 'title' => "Rocketry", 'unread' => 5, 'order_id' => 5], + ['id' => 1, 'title' => "Science", 'unread' => 2, 'order_id' => 6], + ['id' => 0, 'title' => "Uncategorized", 'unread' => 0], + ['id' => -1, 'title' => "Special", 'unread' => 11], + ['id' => -2, 'title' => "Labels", 'unread' => 8], + ], + [ + ['id' => 5, 'title' => "Local", 'unread' => 10, 'order_id' => 1], + ['id' => 6, 'title' => "National", 'unread' => 18, 'order_id' => 2], + ['id' => 3, 'title' => "Politics", 'unread' => 0, 'order_id' => 4], + ['id' => 2, 'title' => "Rocketry", 'unread' => 5, 'order_id' => 5], + ['id' => 1, 'title' => "Science", 'unread' => 2, 'order_id' => 6], + ['id' => 0, 'title' => "Uncategorized", 'unread' => 0], + ['id' => -1, 'title' => "Special", 'unread' => 11], + ['id' => -2, 'title' => "Labels", 'unread' => 8], + ], + [ + ['id' => 5, 'title' => "Local", 'unread' => 10, 'order_id' => 1], + ['id' => 6, 'title' => "National", 'unread' => 18, 'order_id' => 2], + ['id' => 2, 'title' => "Rocketry", 'unread' => 5, 'order_id' => 5], + ['id' => 1, 'title' => "Science", 'unread' => 2, 'order_id' => 6], + ['id' => -1, 'title' => "Special", 'unread' => 11], + ['id' => -2, 'title' => "Labels", 'unread' => 8], + ], + [ + ['id' => 4, 'title' => "Photography", 'unread' => 0, 'order_id' => 1], + ['id' => 3, 'title' => "Politics", 'unread' => 28, 'order_id' => 2], + ['id' => 1, 'title' => "Science", 'unread' => 7, 'order_id' => 3], + ['id' => 0, 'title' => "Uncategorized", 'unread' => 0], + ['id' => -1, 'title' => "Special", 'unread' => 11], + ['id' => -2, 'title' => "Labels", 'unread' => 8], + ], + [ + ['id' => 3, 'title' => "Politics", 'unread' => 28, 'order_id' => 2], + ['id' => 1, 'title' => "Science", 'unread' => 7, 'order_id' => 3], + ['id' => 0, 'title' => "Uncategorized", 'unread' => 0], + ['id' => -1, 'title' => "Special", 'unread' => 11], + ['id' => -2, 'title' => "Labels", 'unread' => 8], + ], + [ + ['id' => 3, 'title' => "Politics", 'unread' => 28, 'order_id' => 2], + ['id' => 1, 'title' => "Science", 'unread' => 7, 'order_id' => 3], + ['id' => -1, 'title' => "Special", 'unread' => 11], + ['id' => -2, 'title' => "Labels", 'unread' => 8], + ], + ]; + for ($a = 0; $a < sizeof($in); $a++) { + $this->assertEquals($this->respGood($exp[$a]), $this->h->dispatch(new Request("POST", "", json_encode($in[$a]))), "Test $a failed"); + } + } } diff --git a/tests/lib/Database/SeriesFolder.php b/tests/lib/Database/SeriesFolder.php index 5c0d15b..2258393 100644 --- a/tests/lib/Database/SeriesFolder.php +++ b/tests/lib/Database/SeriesFolder.php @@ -109,16 +109,16 @@ trait SeriesFolder { public function testListRootFolders() { $exp = [ - ['id' => 5, 'name' => "Politics", 'parent' => null], - ['id' => 1, 'name' => "Technology", 'parent' => null], + ['id' => 5, 'name' => "Politics", 'parent' => null, 'children' => 0], + ['id' => 1, 'name' => "Technology", 'parent' => null, 'children' => 2], ]; - $this->assertSame($exp, Arsse::$db->folderList("john.doe@example.com", null, false)->getAll()); + $this->assertResult($exp, Arsse::$db->folderList("john.doe@example.com", null, false)); $exp = [ - ['id' => 4, 'name' => "Politics", 'parent' => null], + ['id' => 4, 'name' => "Politics", 'parent' => null, 'children' => 0], ]; - $this->assertSame($exp, Arsse::$db->folderList("jane.doe@example.com", null, false)->getAll()); + $this->assertResult($exp, Arsse::$db->folderList("jane.doe@example.com", null, false)); $exp = []; - $this->assertSame($exp, Arsse::$db->folderList("admin@example.net", null, false)->getAll()); + $this->assertResult($exp, Arsse::$db->folderList("admin@example.net", null, false)); Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "folderList"); Phake::verify(Arsse::$user)->authorize("jane.doe@example.com", "folderList"); Phake::verify(Arsse::$user)->authorize("admin@example.net", "folderList"); @@ -126,21 +126,21 @@ trait SeriesFolder { public function testListFoldersRecursively() { $exp = [ - ['id' => 5, 'name' => "Politics", 'parent' => null], - ['id' => 6, 'name' => "Politics", 'parent' => 2], - ['id' => 3, 'name' => "Rocketry", 'parent' => 1], - ['id' => 2, 'name' => "Software", 'parent' => 1], - ['id' => 1, 'name' => "Technology", 'parent' => null], + ['id' => 5, 'name' => "Politics", 'parent' => null, 'children' => 0], + ['id' => 6, 'name' => "Politics", 'parent' => 2, 'children' => 0], + ['id' => 3, 'name' => "Rocketry", 'parent' => 1, 'children' => 0], + ['id' => 2, 'name' => "Software", 'parent' => 1, 'children' => 1], + ['id' => 1, 'name' => "Technology", 'parent' => null, 'children' => 2], ]; - $this->assertSame($exp, Arsse::$db->folderList("john.doe@example.com", null, true)->getAll()); + $this->assertResult($exp, Arsse::$db->folderList("john.doe@example.com", null, true)); $exp = [ - ['id' => 6, 'name' => "Politics", 'parent' => 2], - ['id' => 3, 'name' => "Rocketry", 'parent' => 1], - ['id' => 2, 'name' => "Software", 'parent' => 1], + ['id' => 6, 'name' => "Politics", 'parent' => 2, 'children' => 0], + ['id' => 3, 'name' => "Rocketry", 'parent' => 1, 'children' => 0], + ['id' => 2, 'name' => "Software", 'parent' => 1, 'children' => 1], ]; - $this->assertSame($exp, Arsse::$db->folderList("john.doe@example.com", 1, true)->getAll()); + $this->assertResult($exp, Arsse::$db->folderList("john.doe@example.com", 1, true)); $exp = []; - $this->assertSame($exp, Arsse::$db->folderList("jane.doe@example.com", 4, true)->getAll()); + $this->assertResult($exp, Arsse::$db->folderList("jane.doe@example.com", 4, true)); Phake::verify(Arsse::$user, Phake::times(2))->authorize("john.doe@example.com", "folderList"); Phake::verify(Arsse::$user)->authorize("jane.doe@example.com", "folderList"); } diff --git a/tests/lib/Database/SeriesLabel.php b/tests/lib/Database/SeriesLabel.php index 9e4f0a4..f787ed6 100644 --- a/tests/lib/Database/SeriesLabel.php +++ b/tests/lib/Database/SeriesLabel.php @@ -358,13 +358,13 @@ trait SeriesLabel { ['id' => 2, 'name' => "Fascinating", 'articles' => 0], ['id' => 1, 'name' => "Interesting", 'articles' => 0], ]; - $this->assertSame($exp, Arsse::$db->labelList("john.doe@example.com")->getAll()); + $this->assertResult($exp, Arsse::$db->labelList("john.doe@example.com")); $exp = [ ['id' => 3, 'name' => "Boring", 'articles' => 0], ]; - $this->assertSame($exp, Arsse::$db->labelList("jane.doe@example.com")->getAll()); + $this->assertResult($exp, Arsse::$db->labelList("jane.doe@example.com")); $exp = []; - $this->assertSame($exp, Arsse::$db->labelList("admin@example.net")->getAll()); + $this->assertResult($exp, Arsse::$db->labelList("admin@example.net")); Phake::verify(Arsse::$user)->authorize("john.doe@example.com", "labelList"); Phake::verify(Arsse::$user)->authorize("jane.doe@example.com", "labelList"); Phake::verify(Arsse::$user)->authorize("admin@example.net", "labelList");