. * */ namespace OCA\News\API; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Http\Request; use \OCA\AppFramework\Http\JSONResponse; use \OCA\AppFramework\Http\Http; use \OCA\News\BusinessLayer\ItemBusinessLayer; use \OCA\News\BusinessLayer\BusinessLayerException; class ItemAPI extends Controller { private $itemBusinessLayer; public function __construct(API $api, Request $request, ItemBusinessLayer $itemBusinessLayer){ parent::__construct($api, $request); $this->itemBusinessLayer = $itemBusinessLayer; } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function getAll() { $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); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @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); } } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function read() { return $this->setRead(true); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function unread() { return $this->setRead(false); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function star() { return $this->setStarred(true); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function unstar() { return $this->setStarred(false); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @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(); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function readMultiple() { return $this->setMultipleRead(true); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @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(); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function starMultiple() { return $this->setMultipleStarred(true); } /** * @IsAdminExemption * @IsSubAdminExemption * @CSRFExemption * @Ajax * @API */ public function unstarMultiple() { return $this->setMultipleStarred(false); } }