From 69b34a4e5a1601852df2442234ae16117cdd79ac Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 3 Oct 2017 16:14:37 -0400 Subject: [PATCH] Implement TTRSS feed updating; fixes #86 --- lib/Database.php | 2 +- lib/REST/TinyTinyRSS/API.php | 15 ++++++++++++++- tests/REST/TinyTinyRSS/TestTinyTinyAPI.php | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index ca98b3d5..47c74deb 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -497,7 +497,7 @@ class Database { $q = new Query( "SELECT arsse_subscriptions.id as id, - url,favicon,source,folder,pinned,err_count,err_msg,order_type,added, + feed,url,favicon,source,folder,pinned,err_count,err_msg,order_type,added, topmost.top as top_folder, coalesce(arsse_subscriptions.title, arsse_feeds.title) as title, (SELECT count(*) from arsse_articles where feed is arsse_subscriptions.feed) - (SELECT count(*) from arsse_marks where subscription is arsse_subscriptions.id and read is 1) as unread diff --git a/lib/REST/TinyTinyRSS/API.php b/lib/REST/TinyTinyRSS/API.php index 6e1a71d3..c50703e8 100644 --- a/lib/REST/TinyTinyRSS/API.php +++ b/lib/REST/TinyTinyRSS/API.php @@ -342,7 +342,7 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { public function opRenameFeed(array $data) { if (!isset($data['feed_id']) || !ValueInfo::id($data['feed_id']) || !isset($data['caption'])) { - // if the feed is invalid, throw an error + // if the feed is invalid or there is no caption, throw an error throw new Exception("INCORRECT_USAGE"); } $info = ValueInfo::str($data['caption']); @@ -379,4 +379,17 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { 'num_feeds' => Arsse::$db->subscriptionCount(Arsse::$user->id), ]; } + + public function opUpdateFeed(array $data): array { + if (!isset($data['feed_id']) || !ValueInfo::id($data['feed_id'])) { + // if the feed is invalid, throw an error + throw new Exception("INCORRECT_USAGE"); + } + try { + Arsse::$db->feedUpdate(Arsse::$db->subscriptionPropertiesGet(Arsse::$user->id, (int) $data['feed_id'])['feed']); + } catch(ExceptionInput $e) { + throw new Exception("FEED_NOT_FOUND"); + } + return ['status' => "OK"]; + } } diff --git a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php index 467e3590..b96bc010 100644 --- a/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php +++ b/tests/REST/TinyTinyRSS/TestTinyTinyAPI.php @@ -609,4 +609,24 @@ class TestTinyTinyAPI extends Test\AbstractTest { $this->assertEquals($this->respGood($exp[0]), $this->h->dispatch(new Request("POST", "", json_encode($in)))); $this->assertEquals($this->respGood($exp[1]), $this->h->dispatch(new Request("POST", "", json_encode($in)))); } + + public function testUpdateAFeed() { + $in = [ + ['op' => "updateFeed", 'sid' => "PriestsOfSyrinx", 'feed_id' => 1], + ['op' => "updateFeed", 'sid' => "PriestsOfSyrinx", 'feed_id' => 2], + ['op' => "updateFeed", 'sid' => "PriestsOfSyrinx", 'feed_id' => -1], + ['op' => "updateFeed", 'sid' => "PriestsOfSyrinx"], + ]; + Phake::when(Arsse::$db)->feedUpdate(11)->thenReturn(true); + Phake::when(Arsse::$db)->subscriptionPropertiesGet(Arsse::$user->id, 1)->thenReturn(['id' => 1, 'feed' => 11]); + Phake::when(Arsse::$db)->subscriptionPropertiesGet(Arsse::$user->id, 2)->thenThrow(new ExceptionInput("subjectMissing")); + $exp = $this->respGood(['status' => "OK"]); + $this->assertEquals($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[0])))); + Phake::verify(Arsse::$db)->feedUpdate(11); + $exp = $this->respErr("FEED_NOT_FOUND"); + $this->assertEquals($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[1])))); + $exp = $this->respErr("INCORRECT_USAGE"); + $this->assertEquals($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[2])))); + $this->assertEquals($exp, $this->h->dispatch(new Request("POST", "", json_encode($in[3])))); + } }