diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2021-01-02 17:57:17 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2021-02-13 13:22:57 +0100 |
commit | b4fa772bc5f23f84fc292f5d6bf884543d2bfe51 (patch) | |
tree | 8576ad3ea145f3644804e2fd93de462cfc2c2578 /lib | |
parent | ceba81060303e49b2617397397f2804516052ec9 (diff) |
Remove V1 item API
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'lib')
30 files changed, 1024 insertions, 1376 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index a262e2a03..079b32fe8 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -31,9 +31,6 @@ use OCP\AppFramework\App; use OCP\Files\IRootFolder; use OCP\Files\Node; - -use OCA\News\Db\MapperFactory; -use OCA\News\Db\ItemMapper; use OCA\News\Fetcher\FeedFetcher; use OCA\News\Fetcher\Fetcher; use OCP\User\Events\BeforeUserDeletedEvent; @@ -90,11 +87,6 @@ class Application extends App implements IBootstrap $context->registerParameter('exploreDir', __DIR__ . '/../Explore/feeds'); $context->registerParameter('configFile', 'config.ini'); - // factories - $context->registerService(ItemMapper::class, function (ContainerInterface $c): ItemMapper { - return $c->get(MapperFactory::class)->build(); - }); - $context->registerService(HTMLPurifier::class, function (ContainerInterface $c): HTMLPurifier { $directory = $c->get(ITempManager::class)->getTempBaseDir() . '/news/cache/purifier'; diff --git a/lib/Command/Config/FolderDelete.php b/lib/Command/Config/FolderDelete.php index a80875682..8d7722b3b 100644 --- a/lib/Command/Config/FolderDelete.php +++ b/lib/Command/Config/FolderDelete.php @@ -2,7 +2,7 @@ namespace OCA\News\Command\Config; -use OCA\News\Service\Exceptions\ServiceException; +use OCA\News\Service\Exceptions\ServiceValidationException; use OCA\News\Service\FolderServiceV2; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -50,7 +50,7 @@ class FolderDelete extends Command $id = $input->getArgument('folder-id'); if ($id === null) { - throw new ServiceException('Can not remove root folder!'); + throw new ServiceValidationException('Can not remove root folder!'); } $this->folderService->delete($user, intval($id)); diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index a434f8de7..e6a83b21a 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -77,7 +77,7 @@ class ApiController extends BaseApiController * * @return array */ - public function index() + public function index(): array { return [ 'apiLevels' => ['v1-2'] diff --git a/lib/Controller/FeedApiController.php b/lib/Controller/FeedApiController.php index f247a4e66..43d92b7ca 100644 --- a/lib/Controller/FeedApiController.php +++ b/lib/Controller/FeedApiController.php @@ -19,12 +19,12 @@ use Exception; use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\Exceptions\ServiceNotFoundException; use OCA\News\Service\FeedServiceV2; +use OCA\News\Service\ItemServiceV2; use OCP\AppFramework\Http\JSONResponse; use \OCP\IRequest; use \OCP\IUserSession; use \OCP\AppFramework\Http; -use \OCA\News\Service\ItemService; use Psr\Log\LoggerInterface; class FeedApiController extends ApiController @@ -32,10 +32,9 @@ class FeedApiController extends ApiController use JSONHttpErrorTrait, ApiPayloadTrait; /** - * TODO: Remove - * @var ItemService + * @var ItemServiceV2 */ - private $oldItemService; + private $itemService; /** * @var FeedServiceV2 @@ -51,12 +50,12 @@ class FeedApiController extends ApiController IRequest $request, ?IUserSession $userSession, FeedServiceV2 $feedService, - ItemService $oldItemService, + ItemServiceV2 $itemService, LoggerInterface $logger ) { parent::__construct($request, $userSession); $this->feedService = $feedService; - $this->oldItemService = $oldItemService; + $this->itemService = $itemService; $this->logger = $logger; } @@ -70,12 +69,12 @@ class FeedApiController extends ApiController { $result = [ - 'starredCount' => $this->oldItemService->starredCount($this->getUserId()), + 'starredCount' => count($this->itemService->starred($this->getUserId())), 'feeds' => $this->serialize($this->feedService->findAllForUser($this->getUserId())) ]; try { - $result['newestItemId'] = $this->oldItemService->getNewestItemId($this->getUserId()); + $result['newestItemId'] = $this->itemService->newest($this->getUserId())->getId(); } catch (ServiceNotFoundException $ex) { // in case there are no items, ignore } @@ -96,9 +95,7 @@ class FeedApiController extends ApiController */ public function create(string $url, ?int $folderId = null) { - if ($folderId === 0) { - $folderId = null; - } + $folderId = $folderId === 0 ? null : $folderId; try { $this->feedService->purgeDeleted($this->getUserId(), time() - 600); @@ -109,7 +106,7 @@ class FeedApiController extends ApiController $this->feedService->fetch($feed); try { - $result['newestItemId'] = $this->oldItemService->getNewestItemId($this->getUserId()); + $result['newestItemId'] = $this->itemService->newest($this->getUserId())->getId(); } catch (ServiceNotFoundException $ex) { // in case there are no items, ignore } @@ -154,7 +151,7 @@ class FeedApiController extends ApiController */ public function read(int $feedId, int $newestItemId): void { - $this->oldItemService->readFeed($feedId, $newestItemId, $this->getUserId()); + $this->itemService->read($this->getUserId(), $feedId, $newestItemId); } @@ -170,9 +167,7 @@ class FeedApiController extends ApiController */ public function move(int $feedId, ?int $folderId) { - if ($folderId === 0) { - $folderId = null; - } + $folderId = $folderId === 0 ? null : $folderId; try { $feed = $this->feedService->find($this->getUserId(), $feedId); diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index 9f7c9b0d6..681dda4bc 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -18,12 +18,12 @@ use OCA\News\Service\Exceptions\ServiceNotFoundException; use OCA\News\Service\FeedServiceV2; use OCA\News\Service\FolderServiceV2; use OCA\News\Service\ImportService; +use OCA\News\Service\ItemServiceV2; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; use OCP\IConfig; use OCP\AppFramework\Http; -use OCA\News\Service\ItemService; use OCA\News\Db\FeedType; use OCP\IUserSession; @@ -35,7 +35,9 @@ class FeedController extends Controller * @var FeedServiceV2 */ private $feedService; - //TODO: Remove + /** + * @var ItemServiceV2 + */ private $itemService; /** * @var FolderServiceV2 @@ -54,7 +56,7 @@ class FeedController extends Controller IRequest $request, FolderServiceV2 $folderService, FeedServiceV2 $feedService, - ItemService $itemService, + ItemServiceV2 $itemService, ImportService $importService, IConfig $settings, ?IUserSession $userSession @@ -79,11 +81,11 @@ class FeedController extends Controller // item id which will be used for marking feeds read $params = [ 'feeds' => $this->feedService->findAllForUser($this->getUserId()), - 'starred' => $this->itemService->starredCount($this->getUserId()) + 'starred' => count($this->itemService->starred($this->getUserId())) ]; try { - $id = $this->itemService->getNewestItemId($this->getUserId()); + $id = $this->itemService->newest($this->getUserId())->getId(); // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId @@ -183,7 +185,7 @@ class FeedController extends Controller $this->feedService->fetch($feed); try { - $id = $this->itemService->getNewestItemId($this->getUserId()); + $id = $this->itemService->newest($this->getUserId())->getId(); // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId $params['newestItemId'] = $id; @@ -261,7 +263,7 @@ class FeedController extends Controller $feed = $this->importService->importArticles($this->getUserId(), $json); $params = [ - 'starred' => $this->itemService->starredCount($this->getUserId()) + 'starred' => count($this->itemService->starred($this->getUserId())) ]; if ($feed) { @@ -281,7 +283,7 @@ class FeedController extends Controller */ public function read(int $feedId, int $highestItemId): array { - $this->itemService->readFeed($feedId, $highestItemId, $this->getUserId()); + $this->feedService->read($this->getUserId(), $feedId, $highestItemId); return [ 'feeds' => [ diff --git a/lib/Controller/FolderApiController.php b/lib/Controller/FolderApiController.php index 8de4b9e69..71fc503e2 100644 --- a/lib/Controller/FolderApiController.php +++ b/lib/Controller/FolderApiController.php @@ -20,7 +20,6 @@ use \OCP\IRequest; use \OCP\IUserSession; use \OCP\AppFramework\Http; -use \OCA\News\Service\ItemService; use \OCA\News\Service\FolderServiceV2; use \OCA\News\Service\Exceptions\ServiceNotFoundException; use \OCA\News\Service\Exceptions\ServiceConflictException; @@ -30,20 +29,19 @@ class FolderApiController extends ApiController { use JSONHttpErrorTrait, ApiPayloadTrait; + /** + * @var FolderServiceV2 + */ private $folderService; - //TODO: Remove - private $itemService; public function __construct( IRequest $request, ?IUserSession $userSession, - FolderServiceV2 $folderService, - ItemService $itemService + FolderServiceV2 $folderService ) { parent::__construct($request, $userSession); $this->folderService = $folderService; - $this->itemService = $itemService; } @@ -52,7 +50,7 @@ class FolderApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function index() + public function index(): array { $folders = $this->folderService->findAllForUser($this->getUserId()); return ['folders' => $this->serialize($folders)]; @@ -142,14 +140,13 @@ class FolderApiController extends ApiController * @NoCSRFRequired * @CORS * - * @param int|null $folderId - * @param int $newestItemId + * @param int|null $folderId ID of the folder + * @param int $maxItemId The newest read item */ - public function read(?int $folderId, int $newestItemId): void + public function read(?int $folderId, int $maxItemId): void { - if ($folderId === 0) { - $folderId = null; - } - $this->itemService->readFolder($folderId, $newestItemId, $this->getUserId()); + $folderId = $folderId === 0 ? null : $folderId; + + $this->folderService->read($this->getUserId(), $folderId, $maxItemId); } } diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index 9dc13b309..da03f9863 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -14,13 +14,11 @@ namespace OCA\News\Controller; use OCA\News\Service\Exceptions\ServiceException; -use OCA\News\Service\FeedServiceV2; use OCP\AppFramework\Http\JSONResponse; use \OCP\IRequest; use \OCP\AppFramework\Http; use \OCA\News\Service\FolderServiceV2; -use \OCA\News\Service\ItemService; use \OCA\News\Service\Exceptions\ServiceNotFoundException; use \OCA\News\Service\Exceptions\ServiceConflictException; use OCP\IUserSession; @@ -33,24 +31,14 @@ class FolderController extends Controller * @var FolderServiceV2 */ private $folderService; - /** - * @var FeedServiceV2 - */ - private $feedService; - //TODO: Remove - private $itemService; public function __construct( IRequest $request, FolderServiceV2 $folderService, - FeedServiceV2 $feedService, - ItemService $itemService, ?IUserSession $userSession ) { parent::__construct($request, $userSession); $this->folderService = $folderService; - $this->feedService = $feedService; - $this->itemService = $itemService; } @@ -134,12 +122,12 @@ class FolderController extends Controller /** * @NoAdminRequired * - * @param string $folderName - * @param int|null $folderId + * @param int|null $folderId The ID of the folder + * @param string $folderName The new name of the folder * * @return array|JSONResponse */ - public function rename(string $folderName, ?int $folderId) + public function rename(?int $folderId, string $folderName) { if (empty($folderId)) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); @@ -159,21 +147,18 @@ class FolderController extends Controller * @NoAdminRequired * * @param int|null $folderId - * @param int $highestItemId + * @param int $maxItemId + * + * @return void * - * @return array + * @throws ServiceConflictException + * @throws ServiceNotFoundException */ - public function read(?int $folderId, int $highestItemId): array + public function read(?int $folderId, int $maxItemId): void { $folderId = $folderId === 0 ? null : $folderId; - $this->itemService->readFolder( - $folderId, - $highestItemId, - $this->getUserId() - ); - $feeds = $this->feedService->findAllForUser($this->getUserId()); - return ['feeds' => $this->serialize($feeds)]; + $this->folderService->read($this->getUserId(), $folderId, $maxItemId); } diff --git a/lib/Controller/ItemApiController.php b/lib/Controller/ItemApiController.php index 7ec43bc5c..003c61fa2 100644 --- a/lib/Controller/ItemApiController.php +++ b/lib/Controller/ItemApiController.php @@ -15,7 +15,9 @@ namespace OCA\News\Controller; -use OCA\News\Service\ItemService; +use OCA\News\Db\FeedType; +use OCA\News\Service\Exceptions\ServiceConflictException; +use OCA\News\Service\Exceptions\ServiceValidationException; use OCA\News\Service\ItemServiceV2; use OCP\AppFramework\Http\JSONResponse; use \OCP\IRequest; @@ -24,22 +26,27 @@ use \OCP\AppFramework\Http; use \OCA\News\Service\Exceptions\ServiceNotFoundException; +/** + * Class ItemApiController + * + * @package OCA\News\Controller + */ class ItemApiController extends ApiController { use JSONHttpErrorTrait, ApiPayloadTrait; - private $oldItemService; + /** + * @var ItemServiceV2 + */ private $itemService; public function __construct( IRequest $request, ?IUserSession $userSession, - ItemService $oldItemService, ItemServiceV2 $itemService ) { parent::__construct($request, $userSession); - $this->oldItemService = $oldItemService; $this->itemService = $itemService; } @@ -64,16 +71,38 @@ class ItemApiController extends ApiController int $batchSize = -1, int $offset = 0, bool $oldestFirst = false - ) { - $items = $this->oldItemService->findAllItems( - $id, - $type, - $batchSize, - $offset, - $getRead, - $oldestFirst, - $this->getUserId() - ); + ): array { + switch ($type) { + case FeedType::FEED: + $items = $this->itemService->findAllInFeedWithFilters( + $this->getUserId(), + $id, + $batchSize, + $offset, + !$getRead, + $oldestFirst + ); + break; + case FeedType::FOLDER: + $items = $this->itemService->findAllInFolderWithFilters( + $this->getUserId(), + $id, + $batchSize, + $offset, + !$getRead, + $oldestFirst + ); + break; + default: + $items = $this->itemService->findAllWithFilters( + $this->getUserId(), + $type, + $batchSize, + $offset, + $oldestFirst + ); + break; + } return ['items' => $this->serialize($items)]; } @@ -88,8 +117,10 @@ class ItemApiController extends ApiController * @param int $id * @param int $lastModified * @return array|JSONResponse + * + * @throws ServiceValidationException */ - public function updated(int $type = 3, int $id = 0, int $lastModified = 0) + public function updated(int $type = 3, int $id = 0, int $lastModified = 0): array { // needs to be turned into a millisecond timestamp to work properly if (strlen((string) $lastModified) <= 10) { @@ -97,27 +128,33 @@ class ItemApiController extends ApiController } else { $paddedLastModified = $lastModified; } - $items = $this->oldItemService->findAllNew( - $id, - $type, - (int) $paddedLastModified, - true, - $this->getUserId() - ); + + switch ($type) { + case FeedType::FEED: + $items = $this->itemService->findAllInFeedAfter($this->getUserId(), $id, $paddedLastModified, false); + break; + case FeedType::FOLDER: + $items = $this->itemService->findAllInFolderAfter($this->getUserId(), $id, $paddedLastModified, false); + break; + default: + $items = $this->itemService->findAllAfter($this->getUserId(), $type, $paddedLastModified); + break; + } return ['items' => $this->serialize($items)]; } - /** - * @return JSONResponse|array + * @param int $itemId + * @param bool $isRead * - * @psalm-return JSONResponse|array<empty, empty> + * @return array|JSONResponse + * @throws ServiceConflictException */ - private function setRead(bool $isRead, int $itemId) + private function setRead(int $itemId, bool $isRead) { try { - $this->oldItemService->read($itemId, $isRead, $this->getUserId()); + $this->itemService->read($this->getUserId(), $itemId, $isRead); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -134,10 +171,11 @@ class ItemApiController extends ApiController * @param int $itemId * * @return array|JSONResponse + * @throws ServiceConflictException */ public function read(int $itemId) { - return $this->setRead(true, $itemId); + return $this->setRead($itemId, true); } @@ -149,27 +187,25 @@ class ItemApiController extends ApiController * @param int $itemId * * @return array|JSONResponse + * @throws ServiceConflictException */ public function unread(int $itemId) { - return $this->setRead(false, $itemId); + return $this->setRead($itemId, false); } - /** - * @return JSONResponse|array + * @param int $feedId + * @param string $guidHash + * @param bool $isStarred * - * @psalm-return JSONResponse|array<empty, empty> + * @return array|JSONResponse + * @throws ServiceConflictException */ - private function setStarred(bool $isStarred, int $feedId, string $guidHash) + private function setStarred(int $feedId, string $guidHash, bool $isStarred) { try { - $this->oldItemService->star( - $feedId, - $guidHash, - $isStarred, - $this->getUserId() - ); + $this->itemService->starByGuid($this->getUserId(), $feedId, $guidHash, $isStarred); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -187,10 +223,11 @@ class ItemApiController extends ApiController * @param string $guidHash * * @return array|JSONResponse + * @throws ServiceConflictException */ public function star(int $feedId, string $guidHash) { - return $this->setStarred(true, $feedId, $guidHash); + return $this->setStarred($feedId, $guidHash, true); } @@ -203,10 +240,11 @@ class ItemApiController extends ApiController * @param string $guidHash * * @return array|JSONResponse + * @throws ServiceConflictException */ public function unstar(int $feedId, string $guidHash) { - return $this->setStarred(false, $feedId, $guidHash); + return $this->setStarred($feedId, $guidHash, false); } @@ -223,15 +261,20 @@ class ItemApiController extends ApiController */ public function readAll(int $newestItemId): void { - $this->oldItemService->readAll($newestItemId, $this->getUserId()); + $this->itemService->readAll($this->getUserId(), $newestItemId); } - - private function setMultipleRead(bool $isRead, array $items): void + /** + * @param array $items + * @param bool $isRead + * + * @throws ServiceConflictException + */ + private function setMultipleRead(array $items, bool $isRead): void { foreach ($items as $id) { try { - $this->oldItemService->read($id, $isRead, $this->getUserId()); + $this->itemService->read($this->getUserId(), $id, $isRead); } catch (ServiceNotFoundException $ex) { continue; } @@ - |