From 643fa4624dd7ba2db1349f16131bf330aeee3387 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 9 Apr 2014 16:10:48 +0200 Subject: port to internal controller, some routes are still broken --- controller/apicontroller.php | 106 ++++++++++++++ controller/feedapicontroller.php | 236 +++++++++++++++++++++++++++++++ controller/feedcontroller.php | 4 +- controller/folderapicontroller.php | 170 +++++++++++++++++++++++ controller/foldercontroller.php | 2 +- controller/itemapicontroller.php | 276 +++++++++++++++++++++++++++++++++++++ controller/itemcontroller.php | 2 +- 7 files changed, 792 insertions(+), 4 deletions(-) create mode 100644 controller/apicontroller.php create mode 100644 controller/feedapicontroller.php create mode 100644 controller/folderapicontroller.php create mode 100644 controller/itemapicontroller.php (limited to 'controller') diff --git a/controller/apicontroller.php b/controller/apicontroller.php new file mode 100644 index 000000000..9e0ff2401 --- /dev/null +++ b/controller/apicontroller.php @@ -0,0 +1,106 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; +use \OCP\AppFramework\Http\Response; + +use \OCA\News\Utility\Updater; +use \OCA\News\Core\API; + +class ApiController extends Controller { + + private $updater; + private $api; + + public function __construct(API $api, IRequest $request, Updater $updater){ + parent::__construct($api->getAppName(), $request); + $this->updater = $updater; + $this->api = $api; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function version() { + $version = $this->api->getAppValue('installed_version'); + $response = new JSONResponse(array('version' => $version)); + return $response; + } + + + /** + * @NoCSRFRequired + * @API + */ + public function beforeUpdate() { + $this->updater->beforeUpdate(); + return new JSONResponse(); + } + + + /** + * @NoCSRFRequired + * @API + */ + public function afterUpdate() { + $this->updater->afterUpdate(); + return new JSONResponse(); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + */ + public function cors() { + // needed for webapps access due to cross origin request policy + if(isset($this->request->server['HTTP_ORIGIN'])) { + $origin = $this->request->server['HTTP_ORIGIN']; + } else { + $origin = '*'; + } + + $response = new Response(); + $response->addHeader('Access-Control-Allow-Origin', $origin); + $response->addHeader('Access-Control-Allow-Methods', + 'PUT, POST, GET, DELETE'); + $response->addHeader('Access-Control-Allow-Credentials', 'true'); + $response->addHeader('Access-Control-Max-Age', '1728000'); + $response->addHeader('Access-Control-Allow-Headers', + 'Authorization, Content-Type'); + return $response; + } + + +} diff --git a/controller/feedapicontroller.php b/controller/feedapicontroller.php new file mode 100644 index 000000000..6537e51df --- /dev/null +++ b/controller/feedapicontroller.php @@ -0,0 +1,236 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\News\Core\API; +use \OCA\News\BusinessLayer\FeedBusinessLayer; +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; + + +class FeedApiController extends Controller { + + private $itemBusinessLayer; + private $feedBusinessLayer; + private $folderBusinessLayer; + private $api; + + public function __construct(API $api, + IRequest $request, + FolderBusinessLayer $folderBusinessLayer, + FeedBusinessLayer $feedBusinessLayer, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api->getAppName(), $request); + $this->folderBusinessLayer = $folderBusinessLayer; + $this->feedBusinessLayer = $feedBusinessLayer; + $this->itemBusinessLayer = $itemBusinessLayer; + $this->api = $api; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function index() { + $userId = $this->api->getUserId(); + + $result = array( + 'feeds' => array(), + 'starredCount' => $this->itemBusinessLayer->starredCount($userId) + ); + + foreach ($this->feedBusinessLayer->findAll($userId) as $feed) { + array_push($result['feeds'], $feed->toAPI()); + } + + // check case when there are no items + try { + $result['newestItemId'] = + $this->itemBusinessLayer->getNewestItemId($userId); + } catch(BusinessLayerException $ex) {} + + return new JSONResponse($result); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function create() { + $userId = $this->api->getUserId(); + $feedUrl = $this->params('url'); + $folderId = (int) $this->params('folderId', 0); + + try { + $this->feedBusinessLayer->purgeDeleted($userId, false); + + $feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $userId); + $result = array( + 'feeds' => array($feed->toAPI()) + ); + + try { + $result['newestItemId'] = + $this->itemBusinessLayer->getNewestItemId($userId); + } catch(BusinessLayerException $ex) {} + + return new JSONResponse($result); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function delete() { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + + try { + $this->feedBusinessLayer->delete($feedId, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function read() { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readFeed($feedId, $newestItemId, $userId); + return new JSONResponse(); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function move() { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $folderId = (int) $this->params('folderId'); + + try { + $this->feedBusinessLayer->move($feedId, $folderId, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function rename() { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $feedTitle = $this->params('feedTitle'); + + try { + $this->feedBusinessLayer->rename($feedId, $feedTitle, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoCSRFRequired + * @API + */ + public function fromAllUsers() { + $feeds = $this->feedBusinessLayer->findAllFromAllUsers(); + $result = array('feeds' => array()); + + foreach ($feeds as $feed) { + array_push($result['feeds'], array( + 'id' => $feed->getId(), + 'userId' => $feed->getUserId() + )); + } + + return new JSONResponse($result); + } + + + /** + * @NoCSRFRequired + * @API + */ + public function update() { + $userId = $this->params('userId'); + $feedId = (int) $this->params('feedId'); + + try { + $this->feedBusinessLayer->update($feedId, $userId); + // ignore update failure (feed could not be reachable etc, we dont care) + } catch(\Exception $ex) { + $this->api->log('Could not update feed ' . $ex->getMessage(), + 'debug'); + } + return new JSONResponse(); + + } + + +} diff --git a/controller/feedcontroller.php b/controller/feedcontroller.php index 76a582139..cfd4458a9 100644 --- a/controller/feedcontroller.php +++ b/controller/feedcontroller.php @@ -61,7 +61,7 @@ class FeedController extends Controller { /** * @NoAdminRequired */ - public function feeds(){ + public function index(){ $userId = $this->api->getUserId(); // this method is also used to update the interface @@ -249,7 +249,7 @@ class FeedController extends Controller { /** * @NoAdminRequired */ - public function importArticles() { + public function import() { $json = $this->params('json'); $userId = $this->api->getUserId(); diff --git a/controller/folderapicontroller.php b/controller/folderapicontroller.php new file mode 100644 index 000000000..0954380c3 --- /dev/null +++ b/controller/folderapicontroller.php @@ -0,0 +1,170 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\News\Core\API; +use \OCA\News\BusinessLayer\FolderBusinessLayer; +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\BusinessLayer\BusinessLayerConflictException; +use \OCA\News\BusinessLayer\BusinessLayerValidationException; + + +class FolderApiController extends Controller { + + private $folderBusinessLayer; + private $itemBusinessLayer; + private $api; + + public function __construct(API $api, + IRequest $request, + FolderBusinessLayer $folderBusinessLayer, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api->getAppName(), $request); + $this->folderBusinessLayer = $folderBusinessLayer; + $this->itemBusinessLayer = $itemBusinessLayer; + $this->api = $api; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function index() { + $userId = $this->api->getUserId(); + $result = array( + 'folders' => array() + ); + + foreach ($this->folderBusinessLayer->findAll($userId) as $folder) { + array_push($result['folders'], $folder->toAPI()); + } + + return new JSONResponse($result); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function create() { + $userId = $this->api->getUserId(); + $folderName = $this->params('name'); + $result = array( + 'folders' => array() + ); + + try { + $this->folderBusinessLayer->purgeDeleted($userId, false); + $folder = $this->folderBusinessLayer->create($folderName, $userId); + array_push($result['folders'], $folder->toAPI()); + + return new JSONResponse($result); + + } catch(BusinessLayerValidationException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_UNPROCESSABLE_ENTITY); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function delete() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + + try { + $this->folderBusinessLayer->delete($folderId, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function update() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + $folderName = $this->params('name'); + + try { + $this->folderBusinessLayer->rename($folderId, $folderName, $userId); + return new JSONResponse(); + + } catch(BusinessLayerValidationException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_UNPROCESSABLE_ENTITY); + + } catch(BusinessLayerConflictException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_CONFLICT); + + } catch(BusinessLayerException $ex) { + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function read() { + $userId = $this->api->getUserId(); + $folderId = (int) $this->params('folderId'); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readFolder($folderId, $newestItemId, $userId); + return new JSONResponse(); + } + + +} diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index 332d09974..557315665 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -60,7 +60,7 @@ class FolderController extends Controller { /** * @NoAdminRequired */ - public function folders(){ + public function index(){ $folders = $this->folderBusinessLayer->findAll($this->api->getUserId()); $result = array( 'folders' => $folders diff --git a/controller/itemapicontroller.php b/controller/itemapicontroller.php new file mode 100644 index 000000000..ce3d915d7 --- /dev/null +++ b/controller/itemapicontroller.php @@ -0,0 +1,276 @@ +. +* +*/ + +namespace OCA\News\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\News\BusinessLayer\ItemBusinessLayer; +use \OCA\News\BusinessLayer\BusinessLayerException; +use \OCA\News\Core\API; + +class ItemApiController extends Controller { + + private $itemBusinessLayer; + private $api; + + public function __construct(API $api, + IRequest $request, + ItemBusinessLayer $itemBusinessLayer){ + parent::__construct($api->getAppName(), $request); + $this->itemBusinessLayer = $itemBusinessLayer; + $this->api = $api; + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function index() { + $result = array( + 'items' => array() + ); + + $userId = $this->api->getUserId(); + $batchSize = (int) $this->params('batchSize', 20); + $offset = (int) $this->params('offset', 0); + $type = (int) $this->params('type'); + $id = (int) $this->params('id'); + $showAll = $this->params('getRead'); + + if($showAll === 'true' || $showAll === true) { + $showAll = true; + } else { + $showAll = false; + } + + $items = $this->itemBusinessLayer->findAll( + $id, + $type, + $batchSize, + $offset, + $showAll, + $userId + ); + + foreach ($items as $item) { + array_push($result['items'], $item->toAPI()); + } + + return new JSONResponse($result); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function getUpdated() { + $result = array( + 'items' => array() + ); + + $userId = $this->api->getUserId(); + $lastModified = (int) $this->params('lastModified', 0); + $type = (int) $this->params('type'); + $id = (int) $this->params('id'); + + $items = $this->itemBusinessLayer->findAllNew( + $id, + $type, + $lastModified, + true, + $userId + ); + + foreach ($items as $item) { + array_push($result['items'], $item->toAPI()); + } + + return new JSONResponse($result); + } + + + private function setRead($isRead) { + $userId = $this->api->getUserId(); + $itemId = (int) $this->params('itemId'); + try { + $this->itemBusinessLayer->read($itemId, $isRead, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex){ + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + private function setStarred($isStarred) { + $userId = $this->api->getUserId(); + $feedId = (int) $this->params('feedId'); + $guidHash = $this->params('guidHash'); + try { + $this->itemBusinessLayer->star($feedId, $guidHash, $isStarred, $userId); + return new JSONResponse(); + } catch(BusinessLayerException $ex){ + return new JSONResponse(array('message' => $ex->getMessage()), + Http::STATUS_NOT_FOUND); + } + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function read() { + return $this->setRead(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unread() { + return $this->setRead(false); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function star() { + return $this->setStarred(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unstar() { + return $this->setStarred(false); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function readAll() { + $userId = $this->api->getUserId(); + $newestItemId = (int) $this->params('newestItemId'); + + $this->itemBusinessLayer->readAll($newestItemId, $userId); + return new JSONResponse(); + } + + + private function setMultipleRead($isRead) { + $userId = $this->api->getUserId(); + $items = $this->params('items'); + + foreach($items as $id) { + try { + $this->itemBusinessLayer->read($id, $isRead, $userId); + } catch(BusinessLayerException $ex) { + continue; + } + } + + return new JSONResponse(); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function readMultiple() { + return $this->setMultipleRead(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unreadMultiple() { + return $this->setMultipleRead(false); + } + + + private function setMultipleStarred($isStarred) { + $userId = $this->api->getUserId(); + $items = $this->params('items'); + + foreach($items as $item) { + try { + $this->itemBusinessLayer->star($item['feedId'], + $item['guidHash'], $isStarred, $userId); + } catch(BusinessLayerException $ex) { + continue; + } + } + + return new JSONResponse(); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function starMultiple() { + return $this->setMultipleStarred(true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @API + */ + public function unstarMultiple() { + return $this->setMultipleStarred(false); + } + + +} diff --git a/controller/itemcontroller.php b/controller/itemcontroller.php index 2dd919fed..1e5170ac8 100644 --- a/controller/itemcontroller.php +++ b/controller/itemcontroller.php @@ -55,7 +55,7 @@ class ItemController extends Controller { /** * @NoAdminRequired */ - public function items(){ + public function index(){ $userId = $this->api->getUserId(); $showAll = $this->api->getUserValue('showAll') === '1'; -- cgit v1.2.3