From cbe82c57cd0d11d1d2207db81e2d193919d8538c Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 31 Oct 2017 18:09:16 -0400 Subject: [PATCH] Allow subscriptions to be listed non-recursively This is used in multiple TTRSS operations (at least catchupFeed and getFeeds, and so is a useful optimization --- lib/Database.php | 11 +++++++---- tests/lib/Database/SeriesSubscription.php | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index 16b2ab2..f661e14 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -498,7 +498,7 @@ class Database { return $this->db->prepare('INSERT INTO arsse_subscriptions(owner,feed) values(?,?)', 'str', 'int')->run($user, $feedID)->lastId(); } - public function subscriptionList(string $user, $folder = null, int $id = null): Db\Result { + public function subscriptionList(string $user, $folder = null, bool $recursive = true, int $id = null): Db\Result { if (!Arsse::$user->authorize($user, __FUNCTION__)) { throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]); } @@ -527,11 +527,14 @@ class Database { // this condition facilitates the implementation of subscriptionPropertiesGet, which would otherwise have to duplicate the complex query; it takes precedence over a specified folder // if an ID is specified, add a suitable WHERE condition and bindings $q->setWhere("arsse_subscriptions.id is ?", "int", $id); - } elseif ($folder) { - // if it does exist, add a common table expression to list it and its children so that we select from the entire subtree + } elseif ($folder && $recursive) { + // if a folder is specified and we're listing recursively, add a common table expression to list it and its children so that we select from the entire subtree $q->setCTE("folders(folder)", "SELECT ? union select id from arsse_folders join folders on parent is folder", "int", $folder); // add a suitable WHERE condition $q->setWhere("folder in (select folder from folders)"); + } elseif (!$recursive) { + // if we're not listing recursively, match against only the specified folder (even if it is null) + $q->setWhere("folder is ?", "int", $folder); } return $this->db->prepare($q->getQuery(), $q->getTypes())->run($q->getValues()); } @@ -577,7 +580,7 @@ class Database { } // disable authorization checks for the list call Arsse::$user->authorizationEnabled(false); - $sub = $this->subscriptionList($user, null, (int) $id)->getRow(); + $sub = $this->subscriptionList($user, null, true, (int) $id)->getRow(); Arsse::$user->authorizationEnabled(true); if (!$sub) { throw new Db\ExceptionInput("subjectMissing", ["action" => __FUNCTION__, "field" => "feed", 'id' => $id]); diff --git a/tests/lib/Database/SeriesSubscription.php b/tests/lib/Database/SeriesSubscription.php index 3a5a159..f25def9 100644 --- a/tests/lib/Database/SeriesSubscription.php +++ b/tests/lib/Database/SeriesSubscription.php @@ -257,6 +257,21 @@ trait SeriesSubscription { } public function testListSubscriptionsInAFolder() { + $exp = [ + [ + 'url' => "http://example.com/feed2", + 'title' => "Eek", + 'folder' => null, + 'top_folder' => null, + 'unread' => 4, + 'pinned' => 1, + 'order_type' => 2, + ], + ]; + $this->assertResult($exp, Arsse::$db->subscriptionList($this->user, null, false)); + } + + public function testListSubscriptionsWithoutRecursion() { $exp = [ [ 'url' => "http://example.com/feed3", @@ -269,6 +284,7 @@ trait SeriesSubscription { ], ]; $this->assertResult($exp, Arsse::$db->subscriptionList($this->user, 2)); + } public function testListSubscriptionsInAMissingFolder() {