diff --git a/lib/REST/TinyTinyRSS/API.php b/lib/REST/TinyTinyRSS/API.php index 434c692..6442820 100644 --- a/lib/REST/TinyTinyRSS/API.php +++ b/lib/REST/TinyTinyRSS/API.php @@ -245,8 +245,9 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { $labels[] = ['id' => $this->labelOut($l['id']), 'counter' => $unread, 'auxcounter' => $l['articles']]; $categories[$catmap[self::CAT_LABELS]]['counter'] += $unread; } - // do a second pass on categories, summing descendant unread counts for ancestors and building a final list + // do a second pass on categories, summing descendant unread counts for ancestors $cats = $categories; + $catCounts = []; while ($cats) { foreach ($cats as $c) { if ($c['children']) { @@ -258,12 +259,15 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { $cats[$catmap[$c['parent']]]['counter'] += $c['counter']; $cats[$catmap[$c['parent']]]['children'] -= 1; } - // output the category - $cats[] = ['id' => $c['id'], 'kind' => "cat", 'counter' => $c['counter']]; + $catCounts[$c['id']] = $c['counter']; // remove the category from the input list unset($cats[$catmap[$c['id']]]); } } + // do a third pass on categories, building a final category list; this is done so that the original sort order is retained + foreach ($categories as $c) { + $cats[] = ['id' => $c['id'], 'kind' => "cat", 'counter' => $catCounts[$c['id']]]; + } // prepare data for the virtual feeds and other counters $special = [ ['id' => "global-unread", 'counter' => $countAll], //this should not count archived articles, but we do not have an archive diff --git a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php index 957703a..f31f757 100644 --- a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php +++ b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php @@ -896,14 +896,17 @@ LONG_STRING; ['id' => -1025, 'counter' => 0, 'auxcounter' => 2], ['id' => "3", 'updated' => "2016-05-23T06:40:02", 'counter' => 2, 'has_img' => 1], ['id' => "4", 'updated' => "2017-10-09T15:58:34", 'counter' => 6, 'has_img' => 1], + ['id' => "6", 'updated' => "2010-02-12T20:08:47", 'counter' => 0, 'has_img' => 1], ['id' => "1", 'updated' => "2017-09-15T22:54:16", 'counter' => 5, 'has_img' => 0], ['id' => "5", 'updated' => "2017-07-07T17:07:17", 'counter' => 12, 'has_img' => 0], ['id' => "2", 'updated' => "2011-11-11T11:11:11", 'counter' => 10, 'has_img' => 1], ['id' => 5, 'kind' => "cat", 'counter' => 10], ['id' => 6, 'kind' => "cat", 'counter' => 18], + ['id' => 4, 'kind' => "cat", 'counter' => 0], ['id' => 3, 'kind' => "cat", 'counter' => 28], ['id' => 2, 'kind' => "cat", 'counter' => 5], ['id' => 1, 'kind' => "cat", 'counter' => 7], + ['id' => 0, 'kind' => "cat", 'counter' => 0], ['id' => -2, 'kind' => "cat", 'counter' => 6], ]; $this->assertResponse($this->respGood($exp), $this->req($in));