diff options
Diffstat (limited to 'lib/Controller/ItemController.php')
-rw-r--r-- | lib/Controller/ItemController.php | 164 |
1 files changed, 110 insertions, 54 deletions
diff --git a/lib/Controller/ItemController.php b/lib/Controller/ItemController.php index 96ebcbaec..02a308d87 100644 --- a/lib/Controller/ItemController.php +++ b/lib/Controller/ItemController.php @@ -13,20 +13,31 @@ namespace OCA\News\Controller; +use OCA\News\Db\FeedType; +use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\FeedServiceV2; +use OCP\AppFramework\Http\JSONResponse; use \OCP\IRequest; use \OCP\IConfig; use \OCP\AppFramework\Http; use \OCA\News\Service\Exceptions\ServiceException; use \OCA\News\Service\Exceptions\ServiceNotFoundException; -use \OCA\News\Service\ItemService; +use \OCA\News\Service\ItemServiceV2; use OCP\IUserSession; +/** + * Class ItemController + * + * @package OCA\News\Controller + */ class ItemController extends Controller { use JSONHttpErrorTrait; + /** + * @var ItemServiceV2 + */ private $itemService; /** * @var FeedServiceV2 @@ -40,7 +51,7 @@ class ItemController extends Controller public function __construct( IRequest $request, FeedServiceV2 $feedService, - ItemService $itemService, + ItemServiceV2 $itemService, IConfig $settings, ?IUserSession $userSession ) { @@ -71,7 +82,7 @@ class ItemController extends Controller ?bool $showAll = null, ?bool $oldestFirst = null, string $search = '' - ) { + ): array { // in case this is called directly and not from the website use the // internal state @@ -104,15 +115,14 @@ class ItemController extends Controller $type ); - $params = []; + $return = []; // split search parameter on url space - $search = trim(urldecode($search)); - $search = preg_replace('/\s+/', ' ', $search); // remove multiple ws - if ($search === '') { - $search = []; - } else { - $search = explode(' ', $search); + $search_string = trim(urldecode($search)); + $search_string = preg_replace('/\s+/', ' ', $search_string); // remove multiple ws + $search_items = []; + if ($search !== '') { + $search_items = explode(' ', $search_string); } try { @@ -120,30 +130,54 @@ class ItemController extends Controller // we need to pass the newest feeds to not let the unread count get // out of sync if ($offset === 0) { - $params['newestItemId'] = - $this->itemService->getNewestItemId($this->getUserId()); - $params['feeds'] = $this->feedService->findAllForUser($this->getUserId()); - $params['starred'] = - $this->itemService->starredCount($this->getUserId()); + $return['newestItemId'] = $this->itemService->newest($this->getUserId())->getId(); + $return['feeds'] = $this->feedService->findAllForUser($this->getUserId()); + $return['starred'] = count($this->itemService->starred($this->getUserId())); } - $params['items'] = $this->itemService->findAllItems( - $id, - $type, - $limit, - $offset, - $showAll, - $oldestFirst, - $this->getUserId(), - $search - ); + switch ($type) { + case FeedType::FEED: + $items = $this->itemService->findAllInFeedWithFilters( + $this->getUserId(), + $id, + $limit, + $offset, + !$showAll, + $oldestFirst, + $search_items + ); + break; + case FeedType::FOLDER: + $items = $this->itemService->findAllInFolderWithFilters( + $this->getUserId(), + $id, + $limit, + $offset, + !$showAll, + $oldestFirst, + $search_items + ); + break; + default: + $items = $this->itemService->findAllWithFilters( + $this->getUserId(), + $type, + $limit, + $offset, + $oldestFirst, + $search_items + ); + break; + } + $return['items'] = $items; // this gets thrown if there are no items // in that case just return an empty array } catch (ServiceException $ex) { + //NO-OP } - return $params; + return $return; } @@ -155,7 +189,7 @@ class ItemController extends Controller * @param int $lastModified * @return array */ - public function newItems($type, $id, $lastModified = 0) + public function newItems(int $type, int $id, $lastModified = 0): array { $showAll = $this->settings->getUserValue( $this->getUserId(), @@ -163,28 +197,47 @@ class ItemController extends Controller 'showAll' ) === '1'; - $params = []; + $return = []; try { - $params['newestItemId'] = - $this->itemService->getNewestItemId($this->getUserId()); - $params['feeds'] = $this->feedService->findAllForUser($this->getUserId()); - $params['starred'] = - $this->itemService->starredCount($this->getUserId()); - $params['items'] = $this->itemService->findAllNew( - $id, - $type, - $lastModified, - $showAll, - $this->getUserId() - ); + switch ($type) { + case FeedType::FEED: + $items = $this->itemService->findAllInFeedAfter( + $this->getUserId(), + $id, + $lastModified, + !$showAll + ); + break; + case FeedType::FOLDER: + $items = $this->itemService->findAllInFolderAfter( + $this->getUserId(), + $id, + $lastModified, + !$showAll + ); + break; + default: + $items = $this->itemService->findAllAfter( + $this->getUserId(), + $type, + $lastModified + ); + break; + } + + $return['newestItemId'] = $this->itemService->newest($this->getUserId())->getId(); + $return['feeds'] = $this->feedService->findAllForUser($this->getUserId()); + $return['starred'] = count($this->itemService->starred($this->getUserId())); + $return['items'] = $items; // this gets thrown if there are no items // in that case just return an empty array } catch (ServiceException $ex) { + //NO-OP } - return $params; + return $return; } @@ -194,16 +247,17 @@ class ItemController extends Controller * @param int $feedId * @param string $guidHash * @param bool $isStarred - * @return array|\OCP\AppFramework\Http\JSONResponse + * + * @return array|JSONResponse */ - public function star($feedId, $guidHash, $isStarred) + public function star(int $feedId, string $guidHash, bool $isStarred) { try { - $this->itemService->star( + $this->itemService->starByGuid( + $this->getUserId(), $feedId, $guidHash, - $isStarred, - $this->getUserId() + $isStarred ); } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); @@ -218,12 +272,13 @@ class ItemController extends Controller * * @param int $itemId * @param bool $isRead - * @return array|\OCP\AppFramework\Http\JSONResponse + * + * @return array|JSONResponse */ - public function read($itemId, $isRead = true) + public function read(int $itemId, $isRead = true) { try { - $this->itemService->read($itemId, $isRead, $this->getUserId()); + $this->itemService->read($this->getUserId(), $itemId, $isRead); } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -236,11 +291,12 @@ class ItemController extends Controller * @NoAdminRequired * * @param int $highestItemId + * * @return array */ - public function readAll($highestItemId) + public function readAll(int $highestItemId): array { - $this->itemService->readAll($highestItemId, $this->getUserId()); + $this->itemService->readAll($this->getUserId(), $highestItemId); return ['feeds' => $this->feedService->findAllForUser($this->getUserId())]; } @@ -252,12 +308,12 @@ class ItemController extends Controller * * @return void */ - public function readMultiple($itemIds): void + public function readMultiple(array $itemIds): void { foreach ($itemIds as $id) { try { - $this->itemService->read($id, true, $this->getUserId()); - } catch (ServiceNotFoundException $ex) { + $this->itemService->read($this->getUserId(), $id, true); + } catch (ServiceNotFoundException | ServiceConflictException $ex) { continue; } } |