From d00d1ab2a28f428223e52b17052c072c64784016 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sat, 29 Aug 2020 23:39:35 +0200 Subject: Create V2 mapper, Service and management commands Signed-off-by: Sean Molenaar --- lib/Command/Config/FeedAdd.php | 72 +++++ lib/Command/Config/FeedDelete.php | 56 ++++ lib/Command/Config/FeedList.php | 61 ++++ lib/Command/Config/FolderAdd.php | 58 ++++ lib/Command/Config/FolderDelete.php | 61 ++++ lib/Command/Config/FolderList.php | 62 ++++ lib/Command/ExploreGenerator.php | 3 +- lib/Command/ShowFeed.php | 3 +- lib/Command/Updater/AfterUpdate.php | 36 ++- lib/Command/Updater/AllFeeds.php | 20 +- lib/Command/Updater/BeforeUpdate.php | 20 +- lib/Command/Updater/UpdateFeed.php | 21 +- lib/Controller/ApiPayloadTrait.php | 36 +++ lib/Controller/EntityApiSerializer.php | 6 + lib/Controller/FeedApiController.php | 35 ++- lib/Controller/FeedController.php | 12 +- lib/Controller/FolderApiController.php | 6 +- lib/Controller/FolderController.php | 6 +- lib/Controller/ItemApiController.php | 10 +- lib/Controller/ItemController.php | 6 +- lib/Controller/JSONHttpError.php | 7 +- lib/Controller/UtilityApiController.php | 21 +- lib/Cron/Updater.php | 69 ----- lib/Cron/UpdaterJob.php | 71 +++++ lib/Db/Feed.php | 115 +++++-- lib/Db/FeedMapper.php | 29 +- lib/Db/FeedMapperV2.php | 141 +++++++++ lib/Db/Folder.php | 7 +- lib/Db/FolderMapper.php | 38 ++- lib/Db/FolderMapperV2.php | 87 ++++++ lib/Db/Item.php | 6 + lib/Db/ItemMapper.php | 52 +++- lib/Db/ItemMapperV2.php | 124 ++++++++ lib/Db/MapperFactory.php | 6 + lib/Db/Mysql/ItemMapper.php | 6 + lib/Db/NewsMapper.php | 84 +++++- lib/Db/NewsMapperV2.php | 108 +++++++ lib/Fetcher/FeedFetcher.php | 2 +- lib/Fetcher/IFeedFetcher.php | 2 +- lib/Fetcher/YoutubeFetcher.php | 8 +- .../Exceptions/ServiceConflictException.php | 28 ++ lib/Service/Exceptions/ServiceException.php | 28 ++ .../Exceptions/ServiceNotFoundException.php | 28 ++ .../Exceptions/ServiceValidationException.php | 28 ++ lib/Service/FeedService.php | 61 ++-- lib/Service/FeedServiceV2.php | 336 +++++++++++++++++++++ lib/Service/FolderService.php | 97 +++--- lib/Service/FolderServiceV2.php | 102 +++++++ lib/Service/ItemService.php | 30 +- lib/Service/ItemServiceV2.php | 104 +++++++ lib/Service/Service.php | 70 ++++- lib/Service/ServiceConflictException.php | 28 -- lib/Service/ServiceException.php | 28 -- lib/Service/ServiceNotFoundException.php | 28 -- lib/Service/ServiceValidationException.php | 28 -- lib/Service/StatusService.php | 6 +- lib/Service/UpdaterService.php | 71 +++++ lib/Utility/OPMLExporter.php | 15 +- lib/Utility/Time.php | 4 +- lib/Utility/Updater.php | 67 ---- 60 files changed, 2264 insertions(+), 496 deletions(-) create mode 100644 lib/Command/Config/FeedAdd.php create mode 100644 lib/Command/Config/FeedDelete.php create mode 100644 lib/Command/Config/FeedList.php create mode 100644 lib/Command/Config/FolderAdd.php create mode 100644 lib/Command/Config/FolderDelete.php create mode 100644 lib/Command/Config/FolderList.php create mode 100644 lib/Controller/ApiPayloadTrait.php delete mode 100644 lib/Cron/Updater.php create mode 100644 lib/Cron/UpdaterJob.php create mode 100644 lib/Db/FeedMapperV2.php create mode 100644 lib/Db/FolderMapperV2.php create mode 100644 lib/Db/ItemMapperV2.php create mode 100644 lib/Db/NewsMapperV2.php create mode 100644 lib/Service/Exceptions/ServiceConflictException.php create mode 100644 lib/Service/Exceptions/ServiceException.php create mode 100644 lib/Service/Exceptions/ServiceNotFoundException.php create mode 100644 lib/Service/Exceptions/ServiceValidationException.php create mode 100644 lib/Service/FeedServiceV2.php create mode 100644 lib/Service/FolderServiceV2.php create mode 100644 lib/Service/ItemServiceV2.php delete mode 100644 lib/Service/ServiceConflictException.php delete mode 100644 lib/Service/ServiceException.php delete mode 100644 lib/Service/ServiceNotFoundException.php delete mode 100644 lib/Service/ServiceValidationException.php create mode 100644 lib/Service/UpdaterService.php delete mode 100644 lib/Utility/Updater.php (limited to 'lib') diff --git a/lib/Command/Config/FeedAdd.php b/lib/Command/Config/FeedAdd.php new file mode 100644 index 000000000..d21f448c0 --- /dev/null +++ b/lib/Command/Config/FeedAdd.php @@ -0,0 +1,72 @@ +feedService = $feedService; + } + + /** + * Configure command + */ + protected function configure() + { + $this->setName('news:feed:add') + ->setDescription('Add a feed') + ->addArgument('userID', InputArgument::REQUIRED, 'User to add the feed for') + ->addArgument('feed', InputArgument::REQUIRED, 'Feed to parse') + ->addOption('folder', null, InputOption::VALUE_OPTIONAL, 'Folder ID') + ->addOption('title', null, InputOption::VALUE_OPTIONAL, 'Feed title') + ->addOption('full-text', null, InputOption::VALUE_OPTIONAL, 'Scrape item URLs', false) + ->addOption('username', null, InputOption::VALUE_OPTIONAL, 'Basic auth username') + ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Basic auth password'); + } + + /** + * Execute command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $url = $input->getArgument('feed'); + $user = $input->getArgument('userID'); + $folder = (int) $input->getOption('folder') ?? 0; + $title = $input->getOption('title'); + $username = $input->getOption('username'); + $full_text = $input->getOption('full-text'); + $password = $input->getOption('password'); + + $feed = $this->feedService->create($user, $url, $folder, $full_text, $title, $username, $password); + $this->feedService->fetch($feed, true); + + $output->writeln(json_encode($feed->toAPI(), JSON_PRETTY_PRINT)); + + return 0; + } +} diff --git a/lib/Command/Config/FeedDelete.php b/lib/Command/Config/FeedDelete.php new file mode 100644 index 000000000..c848b1fd4 --- /dev/null +++ b/lib/Command/Config/FeedDelete.php @@ -0,0 +1,56 @@ +feedService = $feedService; + } + + /** + * Configure command + */ + protected function configure() + { + $this->setName('news:feed:delete') + ->setDescription('Remove a feed') + ->addArgument('userID', InputArgument::REQUIRED, 'User to remove the feed from') + ->addArgument('id', InputArgument::REQUIRED, 'Feed ID', null); + } + + /** + * Execute command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $user = $input->getArgument('userID'); + $id = $input->getArgument('id'); + + $this->feedService->delete($user, $id); + + return 0; + } +} diff --git a/lib/Command/Config/FeedList.php b/lib/Command/Config/FeedList.php new file mode 100644 index 000000000..57e14d339 --- /dev/null +++ b/lib/Command/Config/FeedList.php @@ -0,0 +1,61 @@ +feedService = $feedService; + } + + /** + * Configure command + */ + protected function configure() + { + $this->setName('news:feed:list') + ->setDescription('List all feeds') + ->addArgument('userID', InputArgument::REQUIRED, 'User to list the feeds for') + ->addOption('recursive', null, InputOption::VALUE_NONE, 'Fetch the feed recursively'); + } + + /** + * Execute command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int|void + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $user = $input->getArgument('userID'); + $recursive = $input->getOption('recursive'); + + if ($recursive !== false) { + $feeds = $this->feedService->findAllForUserRecursive($user); + } else { + $feeds = $this->feedService->findAllForUser($user); + } + + $output->writeln(json_encode($this->serialize($feeds), JSON_PRETTY_PRINT)); + } +} diff --git a/lib/Command/Config/FolderAdd.php b/lib/Command/Config/FolderAdd.php new file mode 100644 index 000000000..5a7155e0c --- /dev/null +++ b/lib/Command/Config/FolderAdd.php @@ -0,0 +1,58 @@ +folderService = $folderService; + } + + /** + * Configure command + */ + protected function configure() + { + $this->setName('news:folder:add') + ->setDescription('Add a folder') + ->addArgument('userID', InputArgument::REQUIRED, 'User to add the folder for') + ->addArgument('name', InputArgument::REQUIRED, 'Folder name', null) + ->addOption('parent', null, InputOption::VALUE_OPTIONAL, 'Parent folder'); + } + + /** + * Execute command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $user = $input->getArgument('userID'); + $name = $input->getArgument('name'); + $parent = $input->getOption('parent') ?? 0; + + $this->folderService->create($user, $name, $parent); + + return 0; + } +} diff --git a/lib/Command/Config/FolderDelete.php b/lib/Command/Config/FolderDelete.php new file mode 100644 index 000000000..c441bd615 --- /dev/null +++ b/lib/Command/Config/FolderDelete.php @@ -0,0 +1,61 @@ +folderService = $folderService; + } + + /** + * Configure command + */ + protected function configure() + { + $this->setName('news:folder:delete') + ->setDescription('Remove a folder') + ->addArgument('userID', InputArgument::REQUIRED, 'User to remove the folder from') + ->addArgument('id', InputArgument::REQUIRED, 'Folder ID', null); + } + + /** + * Execute command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $user = $input->getArgument('userID'); + $id = $input->getArgument('id'); + + if ($id === '0') { + throw new ServiceException('Can not remove root folder!'); + } + + $this->folderService->delete($user, $id); + + return 0; + } +} diff --git a/lib/Command/Config/FolderList.php b/lib/Command/Config/FolderList.php new file mode 100644 index 000000000..7a2d33ab5 --- /dev/null +++ b/lib/Command/Config/FolderList.php @@ -0,0 +1,62 @@ +folderService = $folderService; + } + + /** + * Configure command + */ + protected function configure() + { + $this->setName('news:folder:list') + ->setDescription('List all folders') + ->addArgument('userID', InputArgument::REQUIRED, 'User to list the folders for') + ->addOption('recursive', null, InputOption::VALUE_NONE, 'Fetch the folder recursively'); + } + + /** + * Execute command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int|void + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $user = $input->getArgument('userID'); + $recursive = $input->getOption('recursive'); + + if ($recursive !== false) { + $folders = $this->folderService->findAllForUserRecursive($user); + } else { + $folders = $this->folderService->findAllForUser($user); + } + + $output->writeln(json_encode($this->serialize($folders), JSON_PRETTY_PRINT)); + } +} diff --git a/lib/Command/ExploreGenerator.php b/lib/Command/ExploreGenerator.php index 2e1b38e91..65c206c5e 100644 --- a/lib/Command/ExploreGenerator.php +++ b/lib/Command/ExploreGenerator.php @@ -64,7 +64,7 @@ class ExploreGenerator extends Command ->addOption('votes', null, InputOption::VALUE_OPTIONAL, 'Votes for the feed, defaults to 100'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $url = $input->getArgument('feed'); $votes = $input->getOption('votes'); @@ -85,6 +85,7 @@ class ExploreGenerator extends Command ]; $output->writeln(json_encode($result, JSON_PRETTY_PRINT)); + return 0; } catch (\Throwable $ex) { $output->writeln('Failed to fetch feed info:'); $output->writeln($ex->getMessage()); diff --git a/lib/Command/ShowFeed.php b/lib/Command/ShowFeed.php index 878b71123..1218279e6 100644 --- a/lib/Command/ShowFeed.php +++ b/lib/Command/ShowFeed.php @@ -60,7 +60,7 @@ class ShowFeed extends Command ->addOption('full-text', 'f', InputOption::VALUE_NONE, 'Usa a scraper to get full text'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $url = $input->getArgument('feed'); $user = $input->getOption('user'); @@ -71,6 +71,7 @@ class ShowFeed extends Command list($feed, $items) = $this->feedFetcher->fetch($url, true, null, $fullTextEnabled, $user, $password); $output->writeln("Feed: " . json_encode($feed, JSON_PRETTY_PRINT)); $output->writeln("Items: " . json_encode($items, JSON_PRETTY_PRINT)); + return 0; } catch (\Throwable $ex) { $output->writeln('Failed to fetch feed info:'); $output->writeln($ex->getMessage()); diff --git a/lib/Command/Updater/AfterUpdate.php b/lib/Command/Updater/AfterUpdate.php index c80913fab..307dece99 100644 --- a/lib/Command/Updater/AfterUpdate.php +++ b/lib/Command/Updater/AfterUpdate.php @@ -11,35 +11,43 @@ namespace OCA\News\Command\Updater; -use Exception; - +use OCA\News\Service\ItemServiceV2; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use \OCA\News\Utility\Updater; - class AfterUpdate extends Command { - private $updater; - - public function __construct(Updater $updater) + /** + * @var ItemServiceV2 + */ + private $itemService; + + /** + * AfterUpdate constructor. + * + * @param ItemServiceV2 $itemService + */ + public function __construct(ItemServiceV2 $itemService) { parent::__construct(); - $this->updater = $updater; + $this->itemService = $itemService; } protected function configure() { $this->setName('news:updater:after-update') - ->setDescription( - 'This is used to clean up the database. It ' . - 'removes old read articles which are not starred' - ); + ->setDescription('removes old read articles which are not starred') + ->addArgument('purge_count', InputArgument::OPTIONAL, 'The amount of items to purge'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { - $this->updater->afterUpdate(); + $count = $input->getArgument('id'); + + echo $this->itemService->purgeOverThreshold($count); + + return 0; } } diff --git a/lib/Command/Updater/AllFeeds.php b/lib/Command/Updater/AllFeeds.php index 93ef4e59a..6993d51ea 100644 --- a/lib/Command/Updater/AllFeeds.php +++ b/lib/Command/Updater/AllFeeds.php @@ -11,19 +11,24 @@ namespace OCA\News\Command\Updater; -use Exception; - +use OCA\News\Service\FeedServiceV2; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use OCA\News\Service\FeedService; - class AllFeeds extends Command { + /** + * @var FeedServiceV2 Feed service + */ private $feedService; - public function __construct(FeedService $feedService) + /** + * AllFeeds constructor. + * + * @param FeedServiceV2 $feedService + */ + public function __construct(FeedServiceV2 $feedService) { parent::__construct(); $this->feedService = $feedService; @@ -35,6 +40,7 @@ class AllFeeds extends Command $this->setName('news:updater:all-feeds') ->setDescription( + 'DEPRECATED: use news:feed:list instead.' . PHP_EOL . 'Prints a JSON string which contains all feed ' . 'ids and user ids, e.g.: ' . $json ); @@ -42,7 +48,7 @@ class AllFeeds extends Command protected function execute(InputInterface $input, OutputInterface $output) { - $feeds = $this->feedService->findAllFromAllUsers(); + $feeds = $this->feedService->findAll(); $result = ['feeds' => []]; foreach ($feeds as $feed) { @@ -53,6 +59,6 @@ class AllFeeds extends Command ]; } - print(json_encode($result)); + $output->write(json_encode($result)); } } diff --git a/lib/Command/Updater/BeforeUpdate.php b/lib/Command/Updater/BeforeUpdate.php index 3a0b1ca72..787125c32 100644 --- a/lib/Command/Updater/BeforeUpdate.php +++ b/lib/Command/Updater/BeforeUpdate.php @@ -11,22 +11,22 @@ namespace OCA\News\Command\Updater; -use Exception; - +use OCA\News\Service\UpdaterService; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use \OCA\News\Utility\Updater; - class BeforeUpdate extends Command { - private $updater; + /** + * @var UpdaterService Updater + */ + private $updaterService; - public function __construct(Updater $updater) + public function __construct(UpdaterService $updater) { parent::__construct(); - $this->updater = $updater; + $this->updaterService = $updater; } protected function configure() @@ -39,8 +39,10 @@ class BeforeUpdate extends Command ); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { - $this->updater->beforeUpdate(); + $this->updaterService->beforeUpdate(); + + return 0; } } diff --git a/lib/Command/Updater/UpdateFeed.php b/lib/Command/Updater/UpdateFeed.php index f5cda22ad..5078e92a4 100644 --- a/lib/Command/Updater/UpdateFeed.php +++ b/lib/Command/Updater/UpdateFeed.php @@ -13,6 +13,7 @@ namespace OCA\News\Command\Updater; use Exception; +use OCA\News\Service\FeedServiceV2; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -22,9 +23,12 @@ use OCA\News\Service\FeedService; class UpdateFeed extends Command { + /** + * @var FeedServiceV2 Feed service + */ private $feedService; - public function __construct(FeedService $feedService) + public function __construct(FeedServiceV2 $feedService) { parent::__construct(); $this->feedService = $feedService; @@ -34,24 +38,25 @@ class UpdateFeed extends Command { $this->setName('news:updater:update-feed') ->addArgument( - 'feed-id', + 'user-id', InputArgument::REQUIRED, - 'feed id, integer' + 'user id of a user, string' ) ->addArgument( - 'user-id', + 'feed-id', InputArgument::REQUIRED, - 'user id of a user, string' + 'feed id, integer' ) ->setDescription('Console API for updating a single user\'s feed'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $feedId = $input->getArgument('feed-id'); $userId = $input->getArgument('user-id'); try { - $this->feedService->update($feedId, $userId); + $feed = $this->feedService->findForUser($userId, $feedId); + $this->feedService->fetch($feed); } catch (Exception $e) { $output->writeln( 'Could not update feed with id ' . $feedId . @@ -59,5 +64,7 @@ class UpdateFeed extends Command ' ' ); } + + return 0; } } diff --git a/lib/Controller/ApiPayloadTrait.php b/lib/Controller/ApiPayloadTrait.php new file mode 100644 index 000000000..2bb31784e --- /dev/null +++ b/lib/Controller/ApiPayloadTrait.php @@ -0,0 +1,36 @@ +toAPI()]; + } + + if (!is_array($data)) { + return $return; + } + + foreach ($data as $entity) { + if ($entity instanceof IAPI) { + $return[] = $entity->toAPI(); + } + } + return $return; + } +} diff --git a/lib/Controller/EntityApiSerializer.php b/lib/Controller/EntityApiSerializer.php index 78a9b1031..c7fdb84e5 100644 --- a/lib/Controller/EntityApiSerializer.php +++ b/lib/Controller/EntityApiSerializer.php @@ -13,6 +13,12 @@ namespace OCA\News\Controller; use \OCA\News\Db\IAPI; +/** + * Class EntityApiSerializer + * + * @package OCA\News\Controller + * @deprecated use ApiPayloadTrait + */ class EntityApiSerializer { diff --git a/lib/Controller/FeedApiController.php b/lib/Controller/FeedApiController.php index 160a8bcd6..f2b77c72b 100644 --- a/lib/Controller/FeedApiController.php +++ b/lib/Controller/FeedApiController.php @@ -15,6 +15,9 @@ namespace OCA\News\Controller; +use OCA\News\Service\Exceptions\ServiceConflictException; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Utility\PsrLogger; use \OCP\IRequest; use \OCP\ILogger; use \OCP\IUserSession; @@ -22,17 +25,30 @@ use \OCP\AppFramework\Http; use \OCA\News\Service\FeedService; use \OCA\News\Service\ItemService; -use \OCA\News\Service\ServiceNotFoundException; -use \OCA\News\Service\ServiceConflictException; +use Psr\Log\LoggerInterface; class FeedApiController extends ApiController { use JSONHttpError; + /** + * @var ItemService + */ private $itemService; + + /** + * @var FeedService + */ private $feedService; + + /** + * @var LoggerInterface + */ private $logger; - private $loggerParams; + + /** + * @var EntityApiSerializer + */ private $serializer; public function __construct( @@ -41,14 +57,12 @@ class FeedApiController extends ApiController IUserSession $userSession, FeedService $feedService, ItemService $itemService, - ILogger $logger, - $LoggerParameters + LoggerInterface $logger ) { parent::__construct($appName, $request, $userSession); $this->feedService = $feedService; $this->itemService = $itemService; $this->logger = $logger; - $this->loggerParams = $LoggerParameters; $this->serializer = new EntityApiSerializer('feeds'); } @@ -63,7 +77,7 @@ class FeedApiController extends ApiController $result = [ 'starredCount' => $this->itemService->starredCount($this->getUserId()), - 'feeds' => $this->feedService->findAll($this->getUserId()) + 'feeds' => $this->feedService->findAllForUser($this->getUserId()) ]; @@ -226,13 +240,10 @@ class FeedApiController extends ApiController public function update($userId, $feedId) { try { - $this->feedService->update($feedId, $userId); + $this->feedService->update($userId, $feedId); // ignore update failure } catch (\Exception $ex) { - $this->logger->debug( - 'Could not update feed ' . $ex->getMessage(), - $this->loggerParams - ); + $this->logger->debug('Could not update feed ' . $ex->getMessage()); } } } diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index 32e9bd232..40aef909a 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -13,6 +13,8 @@ namespace OCA\News\Controller; +use OCA\News\Service\Exceptions\ServiceConflictException; +use OCA\News\Service\Exceptions\ServiceNotFoundException; use OCP\IRequest; use OCP\IConfig; use OCP\AppFramework\Controller; @@ -21,8 +23,6 @@ use OCP\AppFramework\Http; use OCA\News\Service\ItemService; use OCA\News\Service\FeedService; use OCA\News\Service\FolderService; -use OCA\News\Service\ServiceNotFoundException; -use OCA\News\Service\ServiceConflictException; use OCA\News\Db\FeedType; class FeedController extends Controller @@ -63,7 +63,7 @@ class FeedController extends Controller // because of this we also pass the starred count and the newest // item id which will be used for marking feeds read $params = [ - 'feeds' => $this->feedService->findAll($this->userId), + 'feeds' => $this->feedService->findAllForUser($this->userId), 'starred' => $this->itemService->starredCount($this->userId) ]; @@ -104,9 +104,9 @@ class FeedController extends Controller // check if feed or folder exists try { if ($feedType === FeedType::FOLDER) { - $this->folderService->find($feedId, $this->userId); + $this->folderService->find($this->userId, $feedId); } elseif ($feedType === FeedType::FEED) { - $this->feedService->find($feedId, $this->userId); + $this->feedService->find($this->userId, $feedId); // if its the first launch, those values will be null } elseif ($feedType === null) { @@ -203,7 +203,7 @@ class FeedController extends Controller public function update($feedId) { try { - $feed = $this->feedService->update($feedId, $this->userId); + $feed = $this->feedService->update($this->userId, $feedId); return [ 'feeds' => [ diff --git a/lib/Controller/FolderApiController.php b/lib/Controller/FolderApiController.php index eb98b8107..3bafd81a0 100644 --- a/lib/Controller/FolderApiController.php +++ b/lib/Controller/FolderApiController.php @@ -21,9 +21,9 @@ use \OCP\AppFramework\Http; use \OCA\News\Service\FolderService; use \OCA\News\Service\ItemService; -use \OCA\News\Service\ServiceNotFoundException; -use \OCA\News\Service\ServiceConflictException; -use \OCA\News\Service\ServiceValidationException; +use \OCA\News\Service\Exceptions\ServiceNotFoundException; +use \OCA\News\Service\Exceptions\ServiceConflictException; +use \OCA\News\Service\Exceptions\ServiceValidationException; class FolderApiController extends ApiController { diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index d3089178d..22baf8db6 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -20,9 +20,9 @@ use \OCP\AppFramework\Http; use \OCA\News\Service\FolderService; use \OCA\News\Service\FeedService; use \OCA\News\Service\ItemService; -use \OCA\News\Service\ServiceNotFoundException; -use \OCA\News\Service\ServiceConflictException; -use \OCA\News\Service\ServiceValidationException; +use \OCA\News\Service\Exceptions\ServiceNotFoundException; +use \OCA\News\Service\Exceptions\ServiceConflictException; +use \OCA\News\Service\Exceptions\ServiceValidationException; class FolderController extends Controller { diff --git a/lib/Controller/ItemApiController.php b/lib/Controller/ItemApiController.php index cf4c7c730..d5b1de680 100644 --- a/lib/Controller/ItemApiController.php +++ b/lib/Controller/ItemApiController.php @@ -20,7 +20,7 @@ use \OCP\IUserSession; use \OCP\AppFramework\Http; use \OCA\News\Service\ItemService; -use \OCA\News\Service\ServiceNotFoundException; +use \OCA\News\Service\Exceptions\ServiceNotFoundException; class ItemApiController extends ApiController { @@ -223,7 +223,7 @@ class ItemApiController extends ApiController * @NoCSRFRequired * @CORS * - * @param int[] item ids + * @param int[] $items item ids */ public function readMultiple($items) { @@ -236,7 +236,7 @@ class ItemApiController extends ApiController * @NoCSRFRequired * @CORS * - * @param int[] item ids + * @param int[] $items item ids */ public function unreadMultiple($items) { @@ -266,7 +266,7 @@ class ItemApiController extends ApiController * @NoCSRFRequired * @CORS * - * @param int[] item ids + * @param int[] $items item ids */ public function starMultiple($items) { @@ -279,7 +279,7 @@ class ItemApiController extends ApiController * @NoCSRFRequired * @CORS * - * @param int[] item ids + * @param int[] $items item ids */ public function unstarMultiple($items) { diff --git a/lib/Controller/ItemController.php b/lib/Controller/ItemController.php index 156f4d1d4..658e92883 100644 --- a/lib/Controller/ItemController.php +++ b/lib/Controller/ItemController.php @@ -18,8 +18,8 @@ use \OCP\IConfig; use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; -use \OCA\News\Service\ServiceException; -use \OCA\News\Service\ServiceNotFoundException; +use \OCA\News\Service\Exceptions\ServiceException; +use \OCA\News\Service\Exceptions\ServiceNotFoundException; use \OCA\News\Service\ItemService; use \OCA\News\Service\FeedService; @@ -245,7 +245,7 @@ class ItemController extends Controller /** * @NoAdminRequired * - * @param int[] item ids + * @param int[] $itemIds item ids */ public function readMultiple($itemIds) { diff --git a/lib/Controller/JSONHttpError.php b/lib/Controller/JSONHttpError.php index 16d80f857..6a66f9c29 100644 --- a/lib/Controller/JSONHttpError.php +++ b/lib/Controller/JSONHttpError.php @@ -18,10 +18,9 @@ trait JSONHttpError /** - * @param \Exception $exception the message that is returned taken from the - * exception - * @param int $code the http error code - * @return \OCP\AppFramework\Http\JSONResponse + * @param \Exception $exception The exception to report + * @param int $code The http error code + * @return JSONResponse */ public function error(\Exception $exception, $code) { diff --git a/lib/Controller/UtilityApiController.php b/lib/Controller/UtilityApiController.php index ee9ca0900..23956f149 100644 --- a/lib/Controller/UtilityApiController.php +++ b/lib/Controller/UtilityApiController.php @@ -15,18 +15,17 @@ namespace OCA\News\Controller; +use OCA\News\Service\UpdaterService; use \OCP\IRequest; use \OCP\IConfig; use \OCP\IUserSession; -use \OCP\AppFramework\Http; -use \OCA\News\Utility\Updater; use \OCA\News\Service\StatusService; class UtilityApiController extends ApiController { - private $updater; + private $updaterService; private $settings; private $statusService; @@ -34,12 +33,12 @@ class UtilityApiController extends ApiController $appName, IRequest $request, IUserSession $userSession, - Updater $updater, + UpdaterService $updater, IConfig $settings, StatusService $statusService ) { parent::__construct($appName, $request, $userSession); - $this->updater = $updater; + $this->updaterService = $updater; $this->settings = $settings; $this->statusService = $statusService; } @@ -50,7 +49,7 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function version() + public function version(): array { $version = $this->settings->getAppValue( $this->appName, @@ -64,9 +63,9 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function beforeUpdate() + public function beforeUpdate(): void { - $this->updater->beforeUpdate(); + $this->updaterService->beforeUpdate(); } @@ -74,9 +73,9 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @CORS */ - public function afterUpdate() + public function afterUpdate(): void { - $this->updater->afterUpdate(); + $this->updaterService->afterUpdate(); } @@ -85,7 +84,7 @@ class UtilityApiController extends ApiController * @NoCSRFRequired * @NoAdminRequired */ - public function status() + public function status(): array { return $this->statusService->getStatus(); } diff --git a/lib/Cron/Updater.php b/lib/Cron/Updater.php deleted file mode 100644 index 3d9336df7..000000000 --- a/lib/Cron/Updater.php +++ /dev/null @@ -1,69 +0,0 @@ - - * @copyright 2012-2014 Bernhard Posselt - */ - -namespace OCA\News\Cron; - -use OC\BackgroundJob\TimedJob; - -use OCA\News\AppInfo\Application; -use OCA\News\Service\StatusService; -use OCA\News\Utility\Updater as UpdaterService; -use OCP\IConfig; - -class Updater extends TimedJob -{ - - /** - * @var IConfig - */ - private $config; - /** - * @var StatusService - */ - private $status; - /** - * @var UpdaterService - */ - private $updaterService; - - public function __construct( - IConfig $config, - StatusService $status, - UpdaterService $updaterService - ) { - $this->config = $config; - $this->status = $status; - $this->updaterService = $updaterService; - - $interval = $this->config->getAppValue( - Application::NAME, - 'updateInterval', - Application::DEFAULT_SETTINGS['updateInterval'] - ); - - parent::setInterval($interval); - } - - protected function run($argument) - { - $uses_cron = $this->config->getAppValue( - Application::NAME, - 'useCronUpdates', - Application::DEFAULT_SETTINGS['useCronUpdates'] - ); - - if ($uses_cron && $this->status->isProperlyConfigured()) { - $this->updaterService->beforeUpdate(); - $this->updaterService->update(); - $this->updaterService->afterUpdate(); - } - } -} diff --git a/lib/Cron/UpdaterJob.php b/lib/Cron/UpdaterJob.php new file mode 100644 index 000000000..d2dbde149 --- /dev/null +++ b/lib/Cron/UpdaterJob.php @@ -0,0 +1,71 @@ + + * @copyright 2012-2014 Bernhard Posselt + */ + +namespace OCA\News\Cron; + +use OC\BackgroundJob\TimedJob; + +use OCA\News\AppInfo\Application; +use OCA\News\Service\StatusService; +use OCA\News\Service\UpdaterService; +use OCP\IConfig; + +class UpdaterJob extends TimedJob +{ + + /** + * @var IConfig + */ + private $config; + /** + * @var StatusService + */ + private $statusService; + /** + * @var UpdaterService + */ + private $updaterService; + + public function __construct( + IConfig $config, + StatusService $status, + UpdaterService $updaterService + ) { + $this->config = $config; + $this->statusService = $status; + $this->updaterService = $updaterService; + + $interval = $this->config->getAppValue( + Application::NAME, + 'updateInterval', + Application::DEFAULT_SETTINGS['updateInterval'] + ); + + parent::setInterval($interval); + } + + protected function run($argument) + { + $uses_cron = $this->config->getAppValue( + Application::NAME, + 'useCronUpdates', + Application::DEFAULT_SETTINGS['useCronUpdates'] + ); + + if (!$uses_cron || !$this->statusService->isProperlyConfigured()) { + return; + } + + $this->updaterService->beforeUpdate(); + $this->updaterService->update(); + $this->updaterService->afterUpdate(); + } +} diff --git a/lib/Db/Feed.php b/lib/Db/Feed.php index 246ae9886..ae01c3d6c 100644 --- a/lib/Db/Feed.php +++ b/lib/Db/Feed.php @@ -15,6 +15,12 @@ namespace OCA\News\Db; use OCP\AppFramework\Db\Entity; +/** + * Class Feed + * + * @package OCA\News\Db + * @Embeddable + */ class Feed extends Entity implements IAPI, \JsonSerializable { use EntityJSONSerializer; @@ -67,6 +73,8 @@ class Feed extends Entity implements IAPI, \JsonSerializable protected $basicAuthUser = ''; /** @var string|null */ protected $basicAuthPassword = ''; + /** @var Item[] */ + public $items = []; /** * @return int|null @@ -135,7 +143,7 @@ class Feed extends Entity implements IAPI, \JsonSerializable /** * @return string|null */ - public function getHttpEtag() + public function getHttpEtag(): ?string { return $this->httpEtag; } @@ -143,7 +151,7 @@ class Feed extends Entity implements IAPI, \JsonSerializable /** * @return string|null */ - public function getHttpLastModified() + public function getHttpLastModified(): ?string { return $this->httpLastModified; } @@ -313,250 +321,294 @@ class Feed extends Entity implements IAPI, \JsonSerializable /** * @param int|null $added */ - public function setAdded(int $added = null) + public function setAdded(int $added = null): Feed { if ($this->added !== $added) { $this->added = $added; $this->markFieldUpdated('added'); } + + return $this; } /** * @param int $articlesPerUpdate */ - public function setArticlesPerUpdate(int $articlesPerUpdate) + public function setArticlesPerUpdate(int $articlesPerUpdate): Feed { if ($this->articlesPerUpdate !== $articlesPerUpdate) { $this->articlesPerUpdate = $articlesPerUpdate; $this->markFieldUpdated('articlesPerUpdate'); } + + return $this; } /** * @param string|null $basicAuthPassword */ - public function setBasicAuthPassword(string $basicAuthPassword = null) + public function setBasicAuthPassword(string $basicAuthPassword = null): Feed { if ($this->basicAuthPassword !== $basicAuthPassword) { $this->basicAuthPassword = $basicAuthPassword; $this->markFieldUpdated('basicAuthPassword'); } + + return $this; } /** * @param string|null $basicAuthUser */ - public function setBasicAuthUser(string $basicAuthUser = null) + public function setBasicAuthUser(string $basicAuthUser = null): Feed { if ($this->basicAuthUser !== $basicAuthUser) { $this->basicAuthUser = $basicAuthUser; $this->markFieldUpdated('basicAuthUser'); } + + return $this; } /** * @param int|null $deletedAt */ - public function setDeletedAt(int $deletedAt = null) + public function setDeletedAt(int $deletedAt = null): Feed { if ($this->deletedAt !== $deletedAt) { $this->deletedAt = $deletedAt; $this->markFieldUpdated('deletedAt'); } + + return $this; } /** * @param string|null $faviconLink */ - public function setFaviconLink(string $faviconLink = null) + public function setFaviconLink(string $faviconLink = null): Feed { if ($this->faviconLink !== $faviconLink) { $this->faviconLink = $faviconLink; $this->markFieldUpdated('faviconLink'); } + + return $this; } /** * @param int $folderId */ - public function setFolderId(int $folderId) + public function setFolderId(int $folderId): Feed { if ($this->folderId !== $folderId) { $this->folderId = $folderId; $this->markFieldUpdated('folderId'); } + + return $this; } /** * @param bool $fullTextEnabled */ - public function setFullTextEnabled(bool $fullTextEnabled) + public function setFullTextEnabled(bool $fullTextEnabled): Feed { if ($this->fullTextEnabled !== $fullTextEnabled) { $this->fullTextEnabled = $fullTextEnabled; $this->markFieldUpdated('fullTextEnabled'); } + + return $this; } /** * @param string|null $httpEtag */ - public function setHttpEtag(string $httpEtag = null) + public function setHttpEtag(string $httpEtag = null): Feed { if ($this->httpEtag !== $httpEtag) { $this->httpEtag = $httpEtag; $this->markFieldUpdated('httpEtag'); } + + return $this; } /** * @param string|null $httpLastModified */ - public function setHttpLastModified(string $httpLastModified = null) + public function setHttpLastModified(string $httpLastModified = null): Feed { if ($this->httpLastModified !== $httpLastModified) { $this->httpLastModified = $httpLastModified; $this->markFieldUpdated('httpLastModified'); } + + return $this; } /** * @param int $id */ - public function setId(int $id) + public function setId(int $id): Feed { if ($this->id !== $id) { $this->id = $id; $this->markFieldUpdated('id'); } + + return $this; } /** * @param string|null $lastModified */ - public function setLastModified(string $lastModified = null) + public function setLastModified(string $lastModified = null): Feed { if ($this->lastModified !== $lastModified) { $this->lastModified = $lastModified; $this->markFieldUpdated('lastModified'); } + + return $this; } /** * @param string|null $lastUpdateError */ - public function setLastUpdateError(string $lastUpdateError = null) + public function setLastUpdateError(string $lastUpdateError = null): Feed { if ($this->lastUpdateError !== $lastUpdateError) { $this->lastUpdateError = $lastUpdateError; $this->markFieldUpdated('lastUpdateError'); } + + return $this; } /** * @param string|null $link */ - public function setLink(string $link = null) + public function setLink(string $link = null): Feed { $link = trim($link); if (strpos($link, 'http') === 0 && $this->link !== $link) { $this->link = $link; $this->markFieldUpdated('link'); } + + return $this; } /** * @param string|null $location */ - public function setLocation(string $location = null) + public function setLocation(string $location = null): Feed { if ($this->location !== $location) { $this->location = $location; $this->markFieldUpdated('location'); } + + return $this; } /** * @param int $ordering */ - public function setOrdering(int $ordering) + public function setOrdering(int $ordering): Feed { if ($this->ordering !== $ordering) { $this->ordering = $ordering; $this->markFieldUpdated('ordering'); } + + return $this; } /** * @param bool $pinned */ - public function setPinned(bool $pinned) + public function setPinned(bool $pinned): Feed { if ($this->pinned !== $pinned) { $this->pinned = $pinned; $this->markFieldUpdated('pinned'); } + + return $this; } /** * @param bool $preventUpdate */ - public function setPreventUpdate(bool $preventUpdate) + public function setPreventUpdate(bool $preventUpdate): Feed { if ($this->preventUpdate !== $preventUpdate) { $this->preventUpdate = $preventUpdate; $this->markFieldUpdated('preventUpdate'); } + + return $this; } /** * @param string $title */ - public function setTitle(string $title) + public function setTitle(string $title): Feed { if ($this->title !== $title) { $this->title = $title; $this->markFieldUpdated('title'); } + + return $this; } /** * @param int $unreadCount */ - public function setUnreadCount(int $unreadCount) + public function setUnreadCount(int $unreadCount): Feed { if ($this->unreadCount !== $unreadCount) { $this->unreadCount = $unreadCount; $this->markFieldUpdated('unreadCount'); } + + return $this; } /** * @param int $updateErrorCount */ - public function setUpdateErrorCount(int $updateErrorCount) + public function setUpdateErrorCount(int $updateErrorCount): Feed { if ($this->updateErrorCount !== $updateErrorCount) { $this->updateErrorCount = $updateErrorCount; $this->markFieldUpdated('updateErrorCount'); } + + return $this; } /** * @param int $updateMode */ - public function setUpdateMode(int $updateMode) + public function setUpdateMode(int $updateMode): Feed { if ($this->updateMode !== $updateMode) { $this->updateMode = $updateMode; $this->markFieldUpdated('updateMode'); } + + return $this; } /** * @param string $url */ - public function setUrl(string $url) + public function setUrl(string $url): Feed { $url = trim($url); if (strpos($url, 'http') === 0 && $this->url !== $url) { @@ -564,28 +616,34 @@ class Feed extends Entity implements IAPI, \JsonSerializable $this->setUrlHash(md5($url)); $this->markFieldUpdated('url'); } + + return $this; } /** * @param string $urlHash */ - public function setUrlHash(string $urlHash) + public function setUrlHash(string $urlHash): Feed { if ($this->urlHash !== $urlHash) { $this->urlHash = $urlHash; $this->markFieldUpdated('urlHash'); } + + return $this; } /** * @param string $userId */ - public function setUserId(string $userId) + public function setUserId(string $userId): Feed { if ($this->userId !== $userId) { $this->userId = $userId; $this->markFieldUpdated('userId'); } + + return $this; } public function toAPI(): array @@ -603,7 +661,8 @@ class Feed extends Entity implements IAPI, \JsonSerializable 'link', 'pinned', 'updateErrorCount', - 'lastUpdateError' + 'lastUpdateError', + 'items' ] ); } diff --git a/lib/Db/FeedMapper.php b/lib/Db/FeedMapper.php index b23ced239..867ba982d 100644 --- a/lib/Db/FeedMapper.php +++ b/lib/Db/FeedMapper.php @@ -14,20 +14,28 @@ namespace OCA\News\Db; use OCA\News\Utility\Time; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\IDBConnection; use OCP\AppFramework\Db\Entity; +/** + * Class LegacyFeedMapper + * + * @package OCA\News\Db + * @deprecated use FeedMapper + */ class FeedMapper extends NewsMapper { - + const TABLE_NAME = 'news_feeds'; public function __construct(IDBConnection $db, Time $time) { - parent::__construct($db, 'news_feeds', Feed::class, $time); + parent::__construct($db, $time, Feed::class); } - public function find($id, $userId) + public function find(string $userId, int $id) { $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . @@ -52,7 +60,7 @@ class FeedMapper extends NewsMapper } - public function findAllFromUser($userId) + public function findAllFromUser(string $userId): array { $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . @@ -82,7 +90,7 @@ class FeedMapper extends NewsMapper } - public function findAll() + public function findAll(): array { $sql = 'SELECT `feeds`.*, `item_numbers`.`unread_count` ' . 'FROM `*PREFIX*news_feeds` `feeds` ' . @@ -135,15 +143,15 @@ class FeedMapper extends NewsMapper } - public function delete(Entity $entity) + public function delete(Entity $entity): Entity { - parent::delete($entity); - // someone please slap me for doing this manually :P // we needz CASCADE + FKs please $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` = ?'; $params = [$entity->getId()]; $this->execute($sql, $params); + + return parent::delete($entity); } @@ -186,4 +194,9 @@ class FeedMapper extends NewsMapper $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?'; $this->execute($sql, [$userId]); } + + public function findFromUser(string $userId, int $id): Entity + { + return $this->find($id, $userId); + } } diff --git a/lib/Db/FeedMapperV2.php b/lib/Db/FeedMapperV2.php new file mode 100644 index 000000000..2b4ff8f10 --- /dev/null +++ b/lib/Db/FeedMapperV2.php @@ -0,0 +1,141 @@ + + * @author Bernhard Posselt + * @copyright 2012 Alessandro Cosentino + * @copyright 2012-2014 Bernhard Posselt + */ + +namespace OCA\News\Db; + +use OCA\News\Utility\Time; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; +use OCP\IDBConnection; +use OCP\AppFramework\Db\Entity; + +/** + * Class FeedMapper + * + * @package OCA\News\Db + */ +class FeedMapperV2 extends NewsMapperV2 +{ + const TABLE_NAME = 'news_feeds'; + + /** + * FeedMapper constructor. + * + * @param IDBConnection $db + * @param Time $time + */ + public function __construct(IDBConnection $db, Time $time) + { + parent::__construct($db, $time, Feed::class); + } + + /** + * Find all feeds for a user. + * + * @param string $userId The user identifier + * + * @return Entity[] + */ + public function findAllFromUser(string $userId): array + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->andWhere('deleted_at = 0') + ->setParameter(':user_id', $userId); + + return $this->findEntities($builder); + } + + /** + * Find all feeds for a user. + * + * @param string $userId The user identifier + * @param int $id The feed identifier + * + * @return Entity + * + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + */ + public function findFromUser(string $userId, int $id): Entity + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->where('id = :id') + ->setParameter(':user_id', $userId) + ->setParameter(':id', $id); + + return $this->findEntity($builder); + } + + /** + * Find all items + * + * @return Entity[] + */ + public function findAll(): array + { + $builder = $this->db->getQueryBuilder(); + $builder->select('*') + ->from($this->tableName) + ->where('deleted_at = 0'); + + return $this->findEntities($builder); + } + + /** + * Find feed by URL + * + * @param string $userId The user to find in. + * @param string $url The URL to find + * + * @return Entity + * + * @throws DoesNotExistException If not found + * @throws MultipleObjectsReturnedException If multiple found + */ + public function findByURL(string $userId, string $url): Entity + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('user_id = :user_id') + ->andWhere('url = :url') + ->setParameter(':user_id', $userId) + ->setParameter(':url', $url); + + return $this->findEntity($builder); + } + + /** + * Find all feeds in a folder + * + * @param int $id ID of the folder + * + * @return Feed[] + */ + public function findAllFromFolder(int $id): array + { + $builder = $this->db->getQueryBuilder(); + $builder->addSelect('*') + ->from($this->tableName) + ->where('folder_id = :folder_id') + ->setParameter(':folder_id', $id); + + return $this->findEntities($builder); + } +} diff --git a/lib/Db/Folder.php b/lib/Db/Folder.php index 4f54524a8..674c9fabc 100644 --- a/lib/Db/Folder.php +++ b/lib/Db/Folder.php @@ -31,6 +31,8 @@ class Folder extends Entity implements IAPI, \JsonSerializable protected $deletedAt = 0; /** @var string|null */ protected $lastModified = '0'; + /** @var Feed[] */ + public $feeds = []; /** * @return int|null @@ -134,7 +136,7 @@ class Folder extends Entity implements IAPI, \JsonSerializable } } - public function setParentId(int $parentId = null) + public function setParentId(int $parentId = 0) { if ($this->parentId !== $parentId) { $this->parentId = $parentId; @@ -155,7 +157,8 @@ class Folder extends Entity implements IAPI, \JsonSerializable return $this->serializeFields( [ 'id', - 'name' + 'name', + 'feeds' ] ); } diff --git a/lib/Db/FolderMapper.php b/lib/Db/FolderMapper.php index fe73093a9..75b749974 100644 --- a/lib/Db/FolderMapper.php +++ b/lib/Db/FolderMapper.php @@ -14,18 +14,28 @@ namespace OCA\News\Db; use OCA\News\Utility\Time; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\IDBConnection; use OCP\AppFramework\Db\Entity; +/** + * Class LegacyFolderMapper + * + * @package OCA\News\Db + * @deprecated use FolderMapper + */ class FolderMapper extends NewsMapper { + const TABLE_NAME = 'news_folders'; + public function __construct(IDBConnection $db, Time $time) { - parent::__construct($db, 'news_folders', Folder::class, $time); + parent::__construct($db, $time, Folder::class); } - public function find($id, $userId) + public function find(string $userId, int $id) { $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `id` = ? ' . @@ -35,7 +45,7 @@ class FolderMapper extends NewsMapper } - public function findAllFromUser($userId) + public function findAllFromUser(string $userId): array { $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `user_id` = ? ' . @@ -46,7 +56,7 @@ class FolderMapper extends NewsMapper } - public function findByName($folderName, $userId) + public function findByName(string $folderName, string $userId) { $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `name` = ? ' . @@ -57,7 +67,7 @@ class FolderMapper extends NewsMapper } - public function delete(Entity $entity) + public function delete(Entity $entity): Entity { parent::delete($entity); @@ -73,6 +83,8 @@ class FolderMapper extends NewsMapper $stmt = $this->execute($sql); $stmt->closeCursor(); + + return $entity; } @@ -109,9 +121,23 @@ class FolderMapper extends NewsMapper * * @param string $userId the name of the user */ - public function deleteUser($userId) + public function deleteUser(string $userId) { $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; $this->execute($sql, [$userId]); } + + /** + * NO-OP + * @return array + */ + public function findAll(): array + { + return []; + } + + public function findFromUser(string $userId, int $id): Entity + { + return $this->find($id, $userId); + } } diff --git a/lib/Db/FolderMapperV2.php b/lib/Db/FolderMapperV2.php new file mode 100644 index 000000000..d684e5af2 --- /dev/null +++ b/lib/Db/FolderMapperV2.php @@ -0,0 +1,87 @@ + + * @author Bernhard Posselt + * @copyright 2012 Alessandro Cosentino + * @copyright 2012-2014 Bernhard Posselt + */ + +namespace OCA\News\Db; + +use OCA\News\Utility\Time; +use OCP\AppFramework\Db\Entity; +use OCP\IDBConnection; + +/** + * Class FolderMapper + * + * @package OCA\News\Db + */ +class FolderMapperV2 extends NewsMapperV2 +{ + const TABLE_NAME = 'news_folders'; + + /** + * FolderMapper constructor. + * + * @param IDBConnection $db + * @param Time $time + */ + public function __construct(IDBConnection $db, Time $time) + { +