summaryrefslogtreecommitdiffstats
path: root/lib/Service
diff options
context:
space:
mode:
authorSean Molenaar <sean@seanmolenaar.eu>2020-11-14 00:09:38 +0100
committerSean Molenaar <SMillerDev@users.noreply.github.com>2020-12-08 13:58:12 +0100
commit8abddeab4f541883721d912f97dec07bffdfc6b8 (patch)
tree8c176f9ca2fd9e757807481997f265212f154eb2 /lib/Service
parent3d98707be95322d16f5883d7a945d658d6316146 (diff)
Remove usage of old Folder code
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'lib/Service')
-rw-r--r--lib/Service/FolderService.php227
-rw-r--r--lib/Service/FolderServiceV2.php139
-rw-r--r--lib/Service/Service.php4
-rw-r--r--lib/Service/StatusService.php16
-rw-r--r--lib/Service/UpdaterService.php4
5 files changed, 146 insertions, 244 deletions
diff --git a/lib/Service/FolderService.php b/lib/Service/FolderService.php
deleted file mode 100644
index db060b050..000000000
--- a/lib/Service/FolderService.php
+++ /dev/null
@@ -1,227 +0,0 @@
-<?php
-/**
- * Nextcloud - News
- *
- * This file is licensed under the Affero General Public License version 3 or
- * later. See the COPYING file.
- *
- * @author Alessandro Cosentino <cosenal@gmail.com>
- * @author Bernhard Posselt <dev@bernhard-posselt.com>
- * @copyright 2012 Alessandro Cosentino
- * @copyright 2012-2014 Bernhard Posselt
- */
-
-namespace OCA\News\Service;
-
-use OCA\News\AppInfo\Application;
-use OCP\IConfig;
-use OCA\News\Service\Exceptions\ServiceConflictException;
-use OCA\News\Service\Exceptions\ServiceException;
-use OCA\News\Service\Exceptions\ServiceNotFoundException;
-use OCA\News\Service\Exceptions\ServiceValidationException;
-use OCP\AppFramework\Db\Entity;
-use OCP\IL10N;
-use OCA\News\Db\Folder;
-use OCA\News\Db\FolderMapper;
-use OCA\News\Utility\Time;
-use Psr\Log\LoggerInterface;
-
-/**
- * Class LegacyFolderService
- *
- * @package OCA\News\Service
- * @deprecated use FolderServiceV2
- */
-class FolderService extends Service
-{
-
- private $l10n;
- private $timeFactory;
- private $autoPurgeMinimumInterval;
- private $folderMapper;
-
- public function __construct(
- FolderMapper $folderMapper,
- IL10N $l10n,
- Time $timeFactory,
- IConfig $config,
- LoggerInterface $logger
- ) {
- parent::__construct($folderMapper, $logger);
- $this->l10n = $l10n;
- $this->timeFactory = $timeFactory;
- $this->folderMapper = $folderMapper;
- $this->autoPurgeMinimumInterval = $config->getAppValue(
- Application::NAME,
- 'autoPurgeMinimumInterval',
- Application::DEFAULT_SETTINGS['autoPurgeMinimumInterval']
- );
- }
-
- /**
- * Finds all folders of a user
- *
- * @param string $userId the name of the user
- *
- * @return Folder[]
- */
- public function findAllForUser(string $userId, array $params = []): array
- {
- return $this->folderMapper->findAllFromUser($userId);
- }
-
-
- private function validateFolder(string $folderName, string $userId)
- {
- $existingFolders
- = $this->folderMapper->findByName($folderName, $userId);
- if (count($existingFolders) > 0) {
- throw new ServiceConflictException(
- $this->l10n->t('Can not add folder: Exists already')
- );
- }
-
- if (mb_strlen($folderName) === 0) {
- throw new ServiceValidationException(
- 'Folder name can not be empty'
- );
- }
- }
-
-
- /**
- * Creates a new folder
- *
- * @param string $folderName the name of the folder
- * @param string $userId the name of the user for whom it should be created
- * @param int $parentId the parent folder id, deprecated we don't nest
- * folders
- *
- * @return Folder|Entity the newly created folder
- * @throws ServiceValidationException if the folder has invalid parameters
- * @throws ServiceConflictException if name exists already
- */
- public function create(string $folderName, string $userId, ?int $parentId = null)
- {
- $this->validateFolder($folderName, $userId);
-
- $folder = new Folder();
- $folder->setName($folderName)
- ->setUserId($userId)
- ->setParentId($parentId)
- ->setOpened(true);
-
- return $this->folderMapper->insert($folder);
- }
-
-
- /**
- * @param int|null $folderId
- * @param bool $opened
- * @param string $userId
- *
- * @throws ServiceNotFoundException
- */
- public function open(?int $folderId, bool $opened, string $userId)
- {
- $folder = $this->find($userId, $folderId);
- $folder->setOpened($opened);
- $this->folderMapper->update($folder);
- }
-
-
- /**
- * Renames a folder
- *
- * @param int $folderId the id of the folder that should be deleted
- * @param string $folderName the new name of the folder
- * @param string $userId the name of the user for security reasons
- *
- * @return Folder the updated folder
- * @throws ServiceValidationException if the folder has invalid parameters
- * @throws ServiceNotFoundException if the folder does not exist
- * @throws ServiceConflictException if name exists already
- */
- public function rename(int $folderId, string $folderName, string $userId)
- {
- $this->validateFolder($folderName, $userId);
-
- $folder = $this->find($userId, $folderId);
- $folder->setName($folderName);
-
- return $this->folderMapper->update($folder);
- }
-
-
- /**
- * Use this to mark a folder as deleted. That way it can be un-deleted
- *
- * @param int $folderId the id of the folder that should be deleted
- * @param string $userId the name of the user for security reasons
- *
- * @throws ServiceNotFoundException when folder does not exist
- */
- public function markDeleted(int $folderId, string $userId)
- {
- $folder = $this->find($userId, $folderId);
- $folder->setDeletedAt($this->timeFactory->getTime());
- $this->folderMapper->update($folder);
- }
-
-
- /**
- * Use this to restore a folder
- *
- * @param int $folderId the id of the folder that should be restored
- * @param string $userId the name of the user for security reasons
- *
- * @throws ServiceNotFoundException when folder does not exist
- */
- public function unmarkDeleted(int $folderId, string $userId)
- {
- $folder = $this->find($userId, $folderId);
- $folder->setDeletedAt(0);
- $this->folderMapper->update($folder);
- }
-
-
- /**
- * Deletes all deleted folders
- *
- * @param ?string $userId if given it purges only folders of that user
- * @param boolean $useInterval defaults to true, if true it only purges
- * entries in a given interval to give the user a chance to undo the
- * deletion
- */
- public function purgeDeleted(?string $userId = null, bool $useInterval = true)
- {
- $deleteOlderThan = null;
-
- if ($useInterval) {
- $now = $this->timeFactory->getTime();
- $deleteOlderThan = $now - $this->autoPurgeMinimumInterval;
- }
-
- $toDelete = $this->folderMapper->getToDelete($deleteOlderThan, $userId);
-
- foreach ($toDelete as $folder) {
- $this->folderMapper->delete($folder);
- }
- }
-
-
- /**
- * Deletes all folders of a user
- *
- * @param string $userId the name of the user
- */
- public function deleteUser(string $userId)
- {
- $this->folderMapper->deleteUser($userId);
- }
-
- public function findAll(): array
- {
- return $this->mapper->findAll();
- }
-}
diff --git a/lib/Service/FolderServiceV2.php b/lib/Service/FolderServiceV2.php
index ee24addfd..95761b530 100644
--- a/lib/Service/FolderServiceV2.php
+++ b/lib/Service/FolderServiceV2.php
@@ -13,11 +13,14 @@
namespace OCA\News\Service;
-use OCA\News\Db\Feed;
-use OCA\News\Db\FeedMapperV2;
+use OC\AppFramework\Utility\TimeFactory;
use OCA\News\Db\Folder;
use OCA\News\Db\FolderMapperV2;
+use OCA\News\Service\Exceptions\ServiceConflictException;
+use OCA\News\Service\Exceptions\ServiceNotFoundException;
+use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
+use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use Psr\Log\LoggerInterface;
/**
@@ -31,14 +34,20 @@ class FolderServiceV2 extends Service
* @var FeedServiceV2
*/
private $feedService;
+ /**
+ * @var FeedServiceV2
+ */
+ private $timeFactory;
public function __construct(
FolderMapperV2 $mapper,
FeedServiceV2 $feedService,
+ TimeFactory $timeFactory,
LoggerInterface $logger
) {
parent::__construct($mapper, $logger);
$this->feedService = $feedService;
+ $this->timeFactory = $timeFactory;
}
/**
@@ -55,7 +64,31 @@ class FolderServiceV2 extends Service
}
/**
- * @param string $userId
+ * Finds a folder of a user
+ *
+ * @param string $userId The name/ID of the user
+ * @param int|null $folderId ID of the folder
+ *
+ * @return Folder
+ *
+ * @throws ServiceConflictException
+ * @throws ServiceNotFoundException
+ */
+ public function findForUser(string $userId, ?int $folderId): Entity
+ {
+ try {
+ return $this->mapper->findFromUser($userId, $folderId);
+ } catch (DoesNotExistException $e) {
+ throw new ServiceNotFoundException('Folder not found');
+ } catch (MultipleObjectsReturnedException $e) {
+ throw new ServiceConflictException('Multiple folders found');
+ }
+ }
+
+ /**
+ * Find all folders and it's feeds.
+ *
+ * @param string $userId The name/ID of the owner
*
* @return Folder[]
*/
@@ -80,33 +113,117 @@ class FolderServiceV2 extends Service
return $this->mapper->findAll();
}
+ /**
+ * Create a folder
+ *
+ * @param string $userId
+ * @param string $name
+ * @param int|null $parent
+ *
+ * @return Folder
+ */
public function create(string $userId, string $name, ?int $parent = null): Entity
{
$folder = new Folder();
$folder->setUserId($userId)
->setName($name)
- ->setParentId($parent);
+ ->setParentId($parent)
+ ->setOpened(true);
return $this->mapper->insert($folder);
}
- public function delete(string $user, int $id)
+ /**
+ * Delete a feed.
+ *
+ * @param string $userId Folder owner
+ * @param int $folderId Folder ID
+ *
+ * @return Folder
+ * @throws ServiceConflictException
+ * @throws ServiceNotFoundException
+ */
+ public function delete(string $userId, int $folderId): Entity
{
- $entity = $this->mapper->findFromUser($user, $id);
+ $folder = $this->findForUser($userId, $folderId);
- $this->mapper->delete($entity);
+ return $this->mapper->delete($folder);
}
+ /**
+ * Purge all deleted folders.
+ */
public function purgeDeleted()
{
$this->mapper->purgeDeleted();
}
- public function rename(string $userId, int $folderId, string $newName)
+ /**
+ * Rename a folder
+ *
+ * @param string $userId Folder owner
+ * @param int $folderId Folder ID
+ * @param string $newName New name
+ *
+ * @return Folder
+ * @throws ServiceConflictException
+ * @throws ServiceNotFoundException
+ */
+ public function rename(string $userId, int $folderId, string $newName): Entity
{
- /** @var Folder $folder */
- $folder = $this->mapper->find($userId, $folderId);
+ $folder = $this->findForUser($userId, $folderId);
$folder->setName($newName);
- $this->mapper->update($folder);
+ return $this->mapper->update($folder);
+ }
+
+ /**
+ * Mark a folder as deleted
+ *
+ * @param string $userId Folder owner
+ * @param int $folderId Folder ID
+ * @param bool $mark If the mark should be added or removed
+ *
+ * @return Folder
+ * @throws ServiceConflictException
+ * @throws ServiceNotFoundException
+ */
+ public function markDelete(string $userId, int $folderId, bool $mark): Entity
+ {
+ $folder = $this->findForUser($userId, $folderId);
+ $time = $mark ? $this->timeFactory->getTime() : 0;
+ $folder->setDeletedAt($time);
+
+ return $this->mapper->update($folder);
+ }
+
+ /**
+ * Mark a folder as opened
+ *
+ * @param string $userId Folder owner
+ * @param int|null $folderId Folder ID
+ * @param bool $open If the mark should be added or removed
+ *
+ * @return Folder
+ * @throws ServiceConflictException
+ * @throws ServiceNotFoundException
+ */
+ public function open(string $userId, ?int $folderId, bool $open): Entity
+ {
+ $folder = $this->findForUser($userId, $folderId);
+ $folder->setOpened($open);
+ return $this->mapper->update($folder);
+ }
+
+ /**
+ * Delete all folders of a user
+ *
+ * @param string $userId User ID/name
+ */
+ public function deleteUser(string $userId): void
+ {
+ $folders = $this->findAllForUser($userId);
+ foreach ($folders as $folder) {
+ $this->mapper->delete($folder);
+ }
}
}
diff --git a/lib/Service/Service.php b/lib/Service/Service.php
index 04965af4b..970613830 100644
--- a/lib/Service/Service.php
+++ b/lib/Service/Service.php
@@ -29,7 +29,7 @@ use Psr\Log\LoggerInterface;
abstract class Service
{
/**
- * @var NewsMapper|NewsMapperV2
+ * @var NewsMapperV2
*/
protected $mapper;
/**
@@ -40,7 +40,7 @@ abstract class Service
/**
* Service constructor.
*
- * @param NewsMapper|NewsMapperV2 $mapper
+ * @param NewsMapperV2 $mapper
* @param LoggerInterface $logger
*/
public function __construct($mapper, LoggerInterface $logger)
diff --git a/lib/Service/StatusService.php b/lib/Service/StatusService.php
index acfdf6be1..f46624cd0 100644
--- a/lib/Service/StatusService.php
+++ b/lib/Service/StatusService.php
@@ -36,9 +36,16 @@ class StatusService
$this->connection = $connection;
}
- public function isProperlyConfigured(): bool
+ /**
+ * Check if cron is properly configured
+ *
+ * @return bool
+ */
+ public function isCronProperlyConfigured(): bool
{
+ //Is NC cron enabled?
$cronMode = $this->settings->getAppValue('core', 'backgroundjobs_mode');
+ //Expect nextcloud cron
$cronOff = !$this->settings->getAppValue(
Application::NAME,
'useCronUpdates',
@@ -50,6 +57,11 @@ class StatusService
}
+ /**
+ * Get the app status
+ *
+ * @return array
+ */
public function getStatus(): array
{
$version = $this->settings->getAppValue(
@@ -60,7 +72,7 @@ class StatusService
return [
'version' => $version,
'warnings' => [
- 'improperlyConfiguredCron' => !$this->isProperlyConfigured(),
+ 'improperlyConfiguredCron' => !$this->isCronProperlyConfigured(),
'incorrectDbCharset' => !$this->connection->supports4ByteText()
]
];
diff --git a/lib/Service/UpdaterService.php b/lib/Service/UpdaterService.php
index c18eed8d7..7f1b04ddc 100644
--- a/lib/Service/UpdaterService.php
+++ b/lib/Service/UpdaterService.php
@@ -18,7 +18,7 @@ class UpdaterService
{
/**
- * @var FolderService
+ * @var FolderServiceV2
*/
private $folderService;
@@ -28,7 +28,7 @@ class UpdaterService
private $feedService;
/**
- * @var ItemService
+ * @var ItemServiceV2
*/
private $itemService;