diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2020-11-16 20:49:38 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2020-12-08 13:58:12 +0100 |
commit | 4ee3fcb78113caff9f2b890cb7d1702dc936d81e (patch) | |
tree | f8fc506f997acdb8e5315c62bb068b0b6db6cd56 | |
parent | 8abddeab4f541883721d912f97dec07bffdfc6b8 (diff) |
Refactor User ID fetching and fix non-specific cleanup
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
21 files changed, 296 insertions, 203 deletions
diff --git a/lib/Controller/Controller.php b/lib/Controller/Controller.php new file mode 100644 index 000000000..b2b86b007 --- /dev/null +++ b/lib/Controller/Controller.php @@ -0,0 +1,63 @@ +<?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> + * @author David Guillot <david@guillot.me> + * @copyright 2020 Sean Molenaar <sean@seanmolenaar.eu> + */ + +namespace OCA\News\Controller; + +use OCP\AppFramework\Controller as BaseController; +use \OCP\IRequest; +use OCP\IUser; +use \OCP\IUserSession; + +/** + * Class ApiController + * + * @package OCA\News\Controller + */ +class Controller extends BaseController +{ + /** + * @var IUserSession + */ + private $userSession; + + /** + * ApiController constructor. + * + * Stores the user session to be able to leverage the user in further methods + * + * @param string $appName The name of the app + * @param IRequest $request The request + * @param IUserSession $userSession The user session + */ + public function __construct(string $appName, IRequest $request, IUserSession $userSession) + { + parent::__construct($appName, $request); + $this->userSession = $userSession; + } + + /** + * @return IUser + */ + protected function getUser() + { + return $this->userSession->getUser(); + } + + /** + * @return string + */ + protected function getUserId() + { + return $this->getUser()->getUID(); + } +} diff --git a/lib/Controller/ExportController.php b/lib/Controller/ExportController.php index 78f200934..4bd4c667d 100644 --- a/lib/Controller/ExportController.php +++ b/lib/Controller/ExportController.php @@ -19,8 +19,8 @@ use OCA\News\Service\ItemServiceV2; use OCA\News\Service\OpmlService; use OCP\AppFramework\Http\DataDownloadResponse; use \OCP\IRequest; -use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http\JSONResponse; +use OCP\IUserSession; class ExportController extends Controller { @@ -29,7 +29,6 @@ class ExportController extends Controller private $folderService; private $feedService; private $itemService; - private $userId; public function __construct( string $appName, @@ -38,14 +37,13 @@ class ExportController extends Controller FeedServiceV2 $feedService, ItemServiceV2 $itemService, OpmlService $opmlService, - string $UserId + IUserSession $userSession ) { - parent::__construct($appName, $request); + parent::__construct($appName, $request, $userSession); $this->feedService = $feedService; $this->folderService = $folderService; $this->opmlService = $opmlService; $this->itemService = $itemService; - $this->userId = $UserId; } @@ -58,7 +56,7 @@ class ExportController extends Controller $date = date('Y-m-d'); return new DataDownloadResponse( - $this->opmlService->export($this->userId), + $this->opmlService->export($this->getUserId()), "subscriptions-${date}.opml", 'text/xml' ); @@ -71,8 +69,8 @@ class ExportController extends Controller */ public function articles(): JSONResponse { - $feeds = $this->feedService->findAllForUser($this->userId); - $items = $this->itemService->findAllForUser($this->userId, ['unread' => true, 'starred' => true]); + $feeds = $this->feedService->findAllForUser($this->getUserId()); + $items = $this->itemService->findAllForUser($this->getUserId(), ['unread' => true, 'starred' => true]); // build assoc array for fast access $feedsDict = []; diff --git a/lib/Controller/FeedApiController.php b/lib/Controller/FeedApiController.php index ae2f8cdaf..65181f8ac 100644 --- a/lib/Controller/FeedApiController.php +++ b/lib/Controller/FeedApiController.php @@ -117,7 +117,7 @@ class FeedApiController extends ApiController } try { - $this->feedService->purgeDeleted(); + $this->feedService->purgeDeleted($this->getUserId(), time() - 600); $feed = $this->feedService->create($this->getUserId(), $url, $folderId); $result = ['feeds' => $this->serialize($feed)]; diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index 76f777a12..5881d7eb9 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -19,13 +19,11 @@ use OCA\News\Service\FolderServiceV2; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; use OCP\IConfig; -use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCA\News\Service\ItemService; use OCA\News\Service\FeedService; use OCA\News\Db\FeedType; -use OCP\IUser; use OCP\IUserSession; class FeedController extends Controller @@ -40,7 +38,9 @@ class FeedController extends Controller * @var FolderServiceV2 */ private $folderService; - private $userId; + /** + * @var IConfig + */ private $settings; public function __construct( @@ -50,14 +50,13 @@ class FeedController extends Controller FeedService $feedService, ItemService $itemService, IConfig $settings, - IUser $user + IUserSession $userSession ) { - parent::__construct($appName, $request); + parent::__construct($appName, $request, $userSession); $this->folderService = $folderService; $this->feedService = $feedService; $this->itemService = $itemService; $this->settings = $settings; - $this->userId = $user->getUID(); } @@ -71,13 +70,13 @@ 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->findAllForUser($this->userId), - 'starred' => $this->itemService->starredCount($this->userId) + 'feeds' => $this->feedService->findAllForUser($this->getUserId()), + 'starred' => $this->itemService->starredCount($this->getUserId()) ]; try { $params['newestItemId'] = - $this->itemService->getNewestItemId($this->userId); + $this->itemService->getNewestItemId($this->getUserId()); // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId @@ -94,12 +93,12 @@ class FeedController extends Controller public function active(): array { $feedId = (int) $this->settings->getUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'lastViewedFeedId' ); $feedType = $this->settings->getUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'lastViewedFeedType' ); @@ -115,9 +114,9 @@ class FeedController extends Controller if ($feedId === 0) { $feedId = null; } - $this->folderService->find($this->userId, $feedId); + $this->folderService->find($this->getUserId(), $feedId); } elseif ($feedType === FeedType::FEED) { - $this->feedService->find($this->userId, $feedId); + $this->feedService->find($this->getUserId(), $feedId); // if its the first launch, those values will be null } elseif ($feedType === null) { @@ -161,12 +160,12 @@ class FeedController extends Controller try { // we need to purge deleted feeds if a feed is created to // prevent already exists exceptions - $this->feedService->purgeDeleted($this->userId, false); + $this->feedService->purgeDeleted($this->getUserId(), false); $feed = $this->feedService->create( $url, $parentFolderId, - $this->userId, + $this->getUserId(), $title, $user, $password @@ -175,7 +174,7 @@ class FeedController extends Controller try { $params['newestItemId'] = - $this->itemService->getNewestItemId($this->userId); + $this->itemService->getNewestItemId($this->getUserId()); // An exception occurs if there is a newest item. If there is none, // simply ignore it and do not add the newestItemId @@ -201,7 +200,7 @@ class FeedController extends Controller public function delete(int $feedId) { try { - $this->feedService->markDeleted($feedId, $this->userId); + $this->feedService->markDeleted($feedId, $this->getUserId()); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -220,7 +219,7 @@ class FeedController extends Controller public function update(int $feedId) { try { - $feed = $this->feedService->update($this->userId, $feedId); + $feed = $this->feedService->update($this->getUserId(), $feedId); return [ 'feeds' => [ @@ -246,10 +245,10 @@ class FeedController extends Controller */ public function import(array $json): array { - $feed = $this->feedService->importArticles($json, $this->userId); + $feed = $this->feedService->importArticles($json, $this->getUserId()); $params = [ - 'starred' => $this->itemService->starredCount($this->userId) + 'starred' => $this->itemService->starredCount($this->getUserId()) ]; if ($feed) { @@ -269,7 +268,7 @@ class FeedController extends Controller */ public function read(int $feedId, int $highestItemId): array { - $this->itemService->readFeed($feedId, $highestItemId, $this->userId); + $this->itemService->readFeed($feedId, $highestItemId, $this->getUserId()); return [ 'feeds' => [ @@ -292,7 +291,7 @@ class FeedController extends Controller public function restore(int $feedId) { try { - $this->feedService->unmarkDeleted($feedId, $this->userId); + $this->feedService->unmarkDeleted($feedId, $this->getUserId()); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -339,7 +338,7 @@ class FeedController extends Controller ); try { - $this->feedService->patch($feedId, $this->userId, $diff); + $this->feedService->patch($feedId, $this->getUserId(), $diff); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } diff --git a/lib/Controller/FolderApiController.php b/lib/Controller/FolderApiController.php index c9a6b67b7..7f7f97525 100644 --- a/lib/Controller/FolderApiController.php +++ b/lib/Controller/FolderApiController.php @@ -72,7 +72,7 @@ class FolderApiController extends ApiController public function create(string $name) { try { - $this->folderService->purgeDeleted(); + $this->folderService->purgeDeleted($this->getUserId(), time() - 600); $folder = $this->folderService->create($this->getUserId(), $name); return ['folders' => $this->serialize($folder)]; } catch (ServiceValidationException $ex) { diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index 662d45029..c12c7042f 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -16,7 +16,6 @@ namespace OCA\News\Controller; use OCA\News\Service\Exceptions\ServiceException; use OCP\AppFramework\Http\JSONResponse; use \OCP\IRequest; -use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http; use \OCA\News\Service\FolderServiceV2; @@ -24,7 +23,7 @@ use \OCA\News\Service\FeedService; use \OCA\News\Service\ItemService; use \OCA\News\Service\Exceptions\ServiceNotFoundException; use \OCA\News\Service\Exceptions\ServiceConflictException; -use OCP\IUser; +use OCP\IUserSession; class FolderController extends Controller { @@ -38,7 +37,6 @@ class FolderController extends Controller private $feedService; //TODO: Remove private $itemService; - private $userId; public function __construct( string $appName, @@ -46,13 +44,12 @@ class FolderController extends Controller FolderServiceV2 $folderService, FeedService $feedService, ItemService $itemService, - IUser $user + IUserSession $userSession ) { - parent::__construct($appName, $request); + parent::__construct($appName, $request, $userSession); $this->folderService = $folderService; $this->feedService = $feedService; $this->itemService = $itemService; - $this->userId = $user->getUID(); } @@ -61,7 +58,7 @@ class FolderController extends Controller */ public function index() { - $folders = $this->folderService->findAllForUser($this->userId); + $folders = $this->folderService->findAllForUser($this->getUserId()); return ['folders' => $this->serialize($folders)]; } @@ -79,7 +76,7 @@ class FolderController extends Controller $folderId = $folderId === 0 ? null : $folderId; try { - $this->folderService->open($this->userId, $folderId, $open); + $this->folderService->open($this->getUserId(), $folderId, $open); } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -98,8 +95,8 @@ class FolderController extends Controller */ public function create(string $folderName, ?int $parent = null) { - $this->folderService->purgeDeleted(); - $folder = $this->folderService->create($this->userId, $folderName, $parent); + $this->folderService->purgeDeleted($this->getUserId(), time() - 600); + $folder = $this->folderService->create($this->getUserId(), $folderName, $parent); return ['folders' => $this->serialize($folder)]; } @@ -118,7 +115,7 @@ class FolderController extends Controller return new JSONResponse([], Http::STATUS_BAD_REQUEST); } try { - $this->folderService->markDelete($this->userId, $folderId, true); + $this->folderService->markDelete($this->getUserId(), $folderId, true); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } catch (ServiceConflictException $ex) { @@ -143,7 +140,7 @@ class FolderController extends Controller return new JSONResponse([], Http::STATUS_BAD_REQUEST); } try { - $folder = $this->folderService->rename($this->userId, $folderId, $folderName); + $folder = $this->folderService->rename($this->getUserId(), $folderId, $folderName); return ['folders' => $this->serialize($folder)]; } catch (ServiceConflictException $ex) { @@ -168,9 +165,9 @@ class FolderController extends Controller $this->itemService->readFolder( $folderId, $highestItemId, - $this->userId + $this->getUserId() ); - $feeds = $this->feedService->findAllForUser($this->userId); + $feeds = $this->feedService->findAllForUser($this->getUserId()); return ['feeds' => $this->serialize($feeds)]; } @@ -187,7 +184,7 @@ class FolderController extends Controller $folderId = $folderId === 0 ? null : $folderId; try { - $this->folderService->markDelete($this->userId, $folderId, false); + $this->folderService->markDelete($this->getUserId(), $folderId, false); } catch (ServiceNotFoundException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } catch (ServiceConflictException $ex) { diff --git a/lib/Controller/ItemController.php b/lib/Controller/ItemController.php index 37d34e8ca..be4f707c9 100644 --- a/lib/Controller/ItemController.php +++ b/lib/Controller/ItemController.php @@ -15,13 +15,13 @@ namespace OCA\News\Controller; use \OCP\IRequest; use \OCP\IConfig; -use \OCP\AppFramework\Controller; 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\FeedService; +use OCP\IUserSession; class ItemController extends Controller { @@ -29,7 +29,6 @@ class ItemController extends Controller private $itemService; private $feedService; - private $userId; private $settings; public function __construct( @@ -38,12 +37,11 @@ class ItemController extends Controller FeedService $feedService, ItemService $itemService, IConfig $settings, - $UserId + IUserSession $userSession ) { - parent::__construct($appName, $request); + parent::__construct($appName, $request, $userSession); $this->itemService = $itemService; $this->feedService = $feedService; - $this->userId = $UserId; $this->settings = $settings; } @@ -74,7 +72,7 @@ class ItemController extends Controller // internal state if ($showAll === null) { $showAll = $this->settings->getUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'showAll' ) === '1'; @@ -82,20 +80,20 @@ class ItemController extends Controller if ($oldestFirst === null) { $oldestFirst = $this->settings->getUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'oldestFirst' ) === '1'; } $this->settings->setUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'lastViewedFeedId', $id ); $this->settings->setUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'lastViewedFeedType', $type @@ -118,10 +116,10 @@ class ItemController extends Controller // out of sync if ($offset === 0) { $params['newestItemId'] = - $this->itemService->getNewestItemId($this->userId); - $params['feeds'] = $this->feedService->findAllForUser($this->userId); + $this->itemService->getNewestItemId($this->getUserId()); + $params['feeds'] = $this->feedService->findAllForUser($this->getUserId()); $params['starred'] = - $this->itemService->starredCount($this->userId); + $this->itemService->starredCount($this->getUserId()); } $params['items'] = $this->itemService->findAllItems( @@ -131,7 +129,7 @@ class ItemController extends Controller $offset, $showAll, $oldestFirst, - $this->userId, + $this->getUserId(), $search ); @@ -155,7 +153,7 @@ class ItemController extends Controller public function newItems($type, $id, $lastModified = 0) { $showAll = $this->settings->getUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'showAll' ) === '1'; @@ -164,16 +162,16 @@ class ItemController extends Controller try { $params['newestItemId'] = - $this->itemService->getNewestItemId($this->userId); - $params['feeds'] = $this->feedService->findAllForUser($this->userId); + $this->itemService->getNewestItemId($this->getUserId()); + $params['feeds'] = $this->feedService->findAllForUser($this->getUserId()); $params['starred'] = - $this->itemService->starredCount($this->userId); + $this->itemService->starredCount($this->getUserId()); $params['items'] = $this->itemService->findAllNew( $id, $type, $lastModified, $showAll, - $this->userId + $this->getUserId() ); // this gets thrown if there are no items @@ -200,7 +198,7 @@ class ItemController extends Controller $feedId, $guidHash, $isStarred, - $this->userId + $this->getUserId() ); } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); @@ -220,7 +218,7 @@ class ItemController extends Controller public function read($itemId, $isRead = true) { try { - $this->itemService->read($itemId, $isRead, $this->userId); + $this->itemService->read($itemId, $isRead, $this->getUserId()); } catch (ServiceException $ex) { return $this->error($ex, Http::STATUS_NOT_FOUND); } @@ -237,8 +235,8 @@ class ItemController extends Controller */ public function readAll($highestItemId) { - $this->itemService->readAll($highestItemId, $this->userId); - return ['feeds' => $this->feedService->findAllForUser($this->userId)]; + $this->itemService->readAll($highestItemId, $this->getUserId()); + return ['feeds' => $this->feedService->findAllForUser($this->getUserId())]; } @@ -251,7 +249,7 @@ class ItemController extends Controller { foreach ($itemIds as $id) { try { - $this->itemService->read($id, true, $this->userId); + $this->itemService->read($id, true, $this->getUserId()); } catch (ServiceNotFoundException $ex) { continue; } diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index ccabd16db..8e55e69ee 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -18,9 +18,7 @@ use OCP\IRequest; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; -use OCP\AppFramework\Controller; use OCP\AppFramework\Http\TemplateResponse; -use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http; use OCP\AppFramework\Http\ContentSecurityPolicy; @@ -28,6 +26,7 @@ use OCA\News\Service\StatusService; use OCA\News\Explore\RecommendedSites; use OCA\News\Explore\RecommendedSiteNotFoundException; use OCA\News\Db\FeedType; +use OCP\IUserSession; class PageController extends Controller { @@ -44,11 +43,6 @@ class PageController extends Controller private $l10n; /** - * @var string - */ - private $userId; - - /** * @var IURLGenerator */ private $urlGenerator; @@ -71,13 +65,12 @@ class PageController extends Controller IL10N $l10n, RecommendedSites $recommendedSites, StatusService $statusService, - ?string $UserId + IUserSession $userSession ) { - parent::__construct($appName, $request); + parent::__construct($appName, $request, $userSession); $this->settings = $settings; $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; - $this->userId = $UserId; $this->recommendedSites = $recommendedSites; $this->statusService = $statusService; } @@ -149,7 +142,7 @@ class PageController extends Controller foreach ($settings as $setting) { $result[$setting] = $this->settings->getUserValue( - $this->userId, + $this->getUserId(), $this->appName, $setting ) === '1'; @@ -185,7 +178,7 @@ class PageController extends Controller foreach ($settings as $setting => $value) { $value = $value ? '1' : '0'; $this->settings->setUserValue( - $this->userId, + $this->getUserId(), $this->appName, $setting, $value @@ -201,13 +194,13 @@ class PageController extends Controller public function explore(string $lang) { $this->settings->setUserValue( - $this->userId, + $this->getUserId(), $this->appName, 'lastViewedFeedId', 0 ); $this->settings->setUserValue( - $this->userId, + $this->getUserId(), $this->appName, |