diff --git a/lib/ClustersBackend/AlbumsBackend.php b/lib/ClustersBackend/AlbumsBackend.php index 2de3286e..64125ad2 100644 --- a/lib/ClustersBackend/AlbumsBackend.php +++ b/lib/ClustersBackend/AlbumsBackend.php @@ -25,23 +25,16 @@ namespace OCA\Memories\ClustersBackend; use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Exceptions; -use OCP\App\IAppManager; -use OCP\IUserSession; +use OCA\Memories\Util; class AlbumsBackend extends Backend { protected TimelineQuery $timelineQuery; - protected string $userId; - protected IAppManager $appManager; public function __construct( - TimelineQuery $timelineQuery, - IUserSession $userSession, - IAppManager $appManager + TimelineQuery $timelineQuery ) { $this->timelineQuery = $timelineQuery; - $this->userId = $userSession->getUser()->getUID(); - $this->appManager = $appManager; } public function appName(): string @@ -51,7 +44,7 @@ class AlbumsBackend extends Backend public function isEnabled(): bool { - return \OCA\Memories\Util::albumsIsEnabled($this->appManager); + return Util::albumsIsEnabled(); } public function clusterName(string $name) @@ -68,10 +61,10 @@ class AlbumsBackend extends Backend $list = []; $t = (int) $request->getParam('t', 0); if ($t & 1) { // personal - $list = array_merge($list, $this->timelineQuery->getAlbums($this->userId)); + $list = array_merge($list, $this->timelineQuery->getAlbums(Util::getUID())); } if ($t & 2) { // shared - $list = array_merge($list, $this->timelineQuery->getAlbums($this->userId, true)); + $list = array_merge($list, $this->timelineQuery->getAlbums(Util::getUID(), true)); } // Remove elements with duplicate album_id @@ -92,7 +85,7 @@ class AlbumsBackend extends Backend public function getPhotos(string $name, ?int $limit = null): array { // Get album - $album = $this->timelineQuery->getAlbumIfAllowed($this->userId, $name); + $album = $this->timelineQuery->getAlbumIfAllowed(Util::getUID(), $name); if (null === $album) { throw Exceptions::NotFound("album {$name}"); } diff --git a/lib/ClustersBackend/FaceRecognitionBackend.php b/lib/ClustersBackend/FaceRecognitionBackend.php index faef320e..12b5ca23 100644 --- a/lib/ClustersBackend/FaceRecognitionBackend.php +++ b/lib/ClustersBackend/FaceRecognitionBackend.php @@ -25,9 +25,8 @@ namespace OCA\Memories\ClustersBackend; use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Db\TimelineRoot; -use OCP\App\IAppManager; +use OCA\Memories\Util; use OCP\IConfig; -use OCP\IUserSession; class FaceRecognitionBackend extends Backend { @@ -35,19 +34,13 @@ class FaceRecognitionBackend extends Backend public TimelineRoot $root; protected TimelineQuery $timelineQuery; - protected string $userId; - protected IAppManager $appManager; protected IConfig $config; public function __construct( TimelineQuery $timelineQuery, - IUserSession $userSession, - IAppManager $appManager, IConfig $config ) { $this->timelineQuery = $timelineQuery; - $this->userId = $userSession->getUser()->getUID(); - $this->appManager = $appManager; $this->config = $config; } @@ -58,8 +51,8 @@ class FaceRecognitionBackend extends Backend public function isEnabled(): bool { - return \OCA\Memories\Util::facerecognitionIsInstalled($this->appManager) - && \OCA\Memories\Util::facerecognitionIsEnabled($this->config, $this->userId); + return Util::facerecognitionIsInstalled() + && Util::facerecognitionIsEnabled(); } public function getClusters(): array diff --git a/lib/ClustersBackend/PlacesBackend.php b/lib/ClustersBackend/PlacesBackend.php index 521d0d82..1f75b4ff 100644 --- a/lib/ClustersBackend/PlacesBackend.php +++ b/lib/ClustersBackend/PlacesBackend.php @@ -25,20 +25,17 @@ namespace OCA\Memories\ClustersBackend; use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Db\TimelineRoot; -use OCP\IUserSession; +use OCA\Memories\Util; class PlacesBackend extends Backend { public TimelineRoot $root; protected TimelineQuery $timelineQuery; - protected string $userId; public function __construct( - TimelineQuery $timelineQuery, - IUserSession $userSession + TimelineQuery $timelineQuery ) { $this->timelineQuery = $timelineQuery; - $this->userId = $userSession->getUser()->getUID(); } public function appName(): string @@ -48,7 +45,7 @@ class PlacesBackend extends Backend public function isEnabled(): bool { - return \OCA\Memories\Util::placesGISType() > 0; + return Util::placesGISType() > 0; } public function getClusters(): array diff --git a/lib/ClustersBackend/RecognizeBackend.php b/lib/ClustersBackend/RecognizeBackend.php index 5bf50dd4..efd8a4e5 100644 --- a/lib/ClustersBackend/RecognizeBackend.php +++ b/lib/ClustersBackend/RecognizeBackend.php @@ -25,8 +25,7 @@ namespace OCA\Memories\ClustersBackend; use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Db\TimelineRoot; -use OCP\App\IAppManager; -use OCP\IUserSession; +use OCA\Memories\Util; class RecognizeBackend extends Backend { @@ -34,17 +33,11 @@ class RecognizeBackend extends Backend public TimelineRoot $root; protected TimelineQuery $timelineQuery; - protected string $userId; - protected IAppManager $appManager; public function __construct( - TimelineQuery $timelineQuery, - IUserSession $userSession, - IAppManager $appManager + TimelineQuery $timelineQuery ) { $this->timelineQuery = $timelineQuery; - $this->userId = $userSession->getUser()->getUID(); - $this->appManager = $appManager; } public function appName(): string @@ -54,12 +47,12 @@ class RecognizeBackend extends Backend public function isEnabled(): bool { - return \OCA\Memories\Util::recognizeIsEnabled($this->appManager); + return Util::recognizeIsEnabled(); } public function getClusters(): array { - return $this->timelineQuery->getPeopleRecognize($this->root, $this->userId); + return $this->timelineQuery->getPeopleRecognize($this->root, Util::getUID()); } public function getPhotos(string $name, ?int $limit = null): array diff --git a/lib/ClustersBackend/TagsBackend.php b/lib/ClustersBackend/TagsBackend.php index aa93b606..17fbd330 100644 --- a/lib/ClustersBackend/TagsBackend.php +++ b/lib/ClustersBackend/TagsBackend.php @@ -25,24 +25,17 @@ namespace OCA\Memories\ClustersBackend; use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Db\TimelineRoot; -use OCP\App\IAppManager; -use OCP\IUserSession; +use OCA\Memories\Util; class TagsBackend extends Backend { public TimelineRoot $root; protected TimelineQuery $timelineQuery; - protected string $userId; - protected IAppManager $appManager; public function __construct( - TimelineQuery $timelineQuery, - IUserSession $userSession, - IAppManager $appManager + TimelineQuery $timelineQuery ) { $this->timelineQuery = $timelineQuery; - $this->userId = $userSession->getUser()->getUID(); - $this->appManager = $appManager; } public function appName(): string @@ -52,7 +45,7 @@ class TagsBackend extends Backend public function isEnabled(): bool { - return \OCA\Memories\Util::tagsIsEnabled($this->appManager); + return Util::tagsIsEnabled(); } public function getClusters(): array diff --git a/lib/Controller/DaysController.php b/lib/Controller/DaysController.php index e1105eda..be6f2e58 100644 --- a/lib/Controller/DaysController.php +++ b/lib/Controller/DaysController.php @@ -165,7 +165,7 @@ class DaysController extends GenericApiController } // Filter for one album - if ($this->albumsIsEnabled()) { + if (Util::albumsIsEnabled()) { if ($albumId = $this->request->getParam('album')) { $transforms[] = [$this->timelineQuery, 'transformAlbumFilter', $albumId]; } @@ -187,7 +187,7 @@ class DaysController extends GenericApiController } // Filter only for one face on Recognize - if (($recognize = $this->request->getParam('recognize')) && $this->recognizeIsEnabled()) { + if (($recognize = $this->request->getParam('recognize')) && Util::recognizeIsEnabled()) { $transforms[] = [$this->timelineQuery, 'transformPeopleRecognitionFilter', $recognize, $aggregateOnly]; $faceRect = $this->request->getParam('facerect'); @@ -197,7 +197,7 @@ class DaysController extends GenericApiController } // Filter only for one face on Face Recognition - if (($face = $this->request->getParam('facerecognition')) && $this->facerecognitionIsEnabled()) { + if (($face = $this->request->getParam('facerecognition')) && Util::facerecognitionIsEnabled()) { $currentModel = (int) $this->config->getAppValue('facerecognition', 'model', -1); $transforms[] = [$this->timelineQuery, 'transformPeopleFaceRecognitionFilter', $currentModel, $face]; @@ -208,17 +208,13 @@ class DaysController extends GenericApiController } // Filter only for one tag - if ($this->tagsIsEnabled()) { - if ($tagName = $this->request->getParam('tag')) { - $transforms[] = [$this->timelineQuery, 'transformTagFilter', $tagName]; - } + if (($tagName = $this->request->getParam('tag')) && Util::tagsIsEnabled()) { + $transforms[] = [$this->timelineQuery, 'transformTagFilter', $tagName]; } // Filter only for one place - if ($this->placesIsEnabled()) { - if ($locationId = $this->request->getParam('place')) { - $transforms[] = [$this->timelineQuery, 'transformPlaceFilter', (int) $locationId]; - } + if (($locationId = $this->request->getParam('place')) && Util::placesGISType() > 0) { + $transforms[] = [$this->timelineQuery, 'transformPlaceFilter', (int) $locationId]; } // Filter geological bounds diff --git a/lib/Controller/GenericApiController.php b/lib/Controller/GenericApiController.php index 1186161e..702d6e90 100644 --- a/lib/Controller/GenericApiController.php +++ b/lib/Controller/GenericApiController.php @@ -38,7 +38,6 @@ abstract class GenericApiController extends Controller { use GenericApiControllerFs; use GenericApiControllerParams; - use GenericApiControllerUtils; protected IConfig $config; protected IUserSession $userSession; diff --git a/lib/Controller/GenericApiControllerFs.php b/lib/Controller/GenericApiControllerFs.php index 4ffffba7..bea8d074 100644 --- a/lib/Controller/GenericApiControllerFs.php +++ b/lib/Controller/GenericApiControllerFs.php @@ -36,7 +36,6 @@ use OCP\IUserSession; trait GenericApiControllerFs { use GenericApiControllerParams; - use GenericApiControllerUtils; protected IConfig $config; protected IUserSession $userSession; @@ -50,7 +49,7 @@ trait GenericApiControllerFs $root = new TimelineRoot(); // Albums have no folder - if ($this->albumsIsEnabled() && $this->request->getParam('album')) { + if ($this->request->getParam('album') && Util::albumsIsEnabled()) { if (null !== $user) { return $root; } diff --git a/lib/Controller/GenericApiControllerUtils.php b/lib/Controller/GenericApiControllerUtils.php deleted file mode 100644 index ff59156c..00000000 --- a/lib/Controller/GenericApiControllerUtils.php +++ /dev/null @@ -1,80 +0,0 @@ - - * @author Varun Patil - * @license AGPL-3.0-or-later - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -namespace OCA\Memories\Controller; - -use OCA\Memories\Util; -use OCP\App\IAppManager; -use OCP\IConfig; - -trait GenericApiControllerUtils -{ - protected IAppManager $appManager; - protected IConfig $config; - - /** - * Check if albums are enabled for this user. - */ - protected function albumsIsEnabled(): bool - { - return Util::albumsIsEnabled($this->appManager); - } - - /** - * Check if tags is enabled for this user. - */ - protected function tagsIsEnabled(): bool - { - return Util::tagsIsEnabled($this->appManager); - } - - /** - * Check if recognize is enabled for this user. - */ - protected function recognizeIsEnabled(): bool - { - return Util::recognizeIsEnabled($this->appManager); - } - - // Check if facerecognition is installed and enabled for this user. - protected function facerecognitionIsInstalled(): bool - { - return Util::facerecognitionIsInstalled($this->appManager); - } - - /** - * Check if facerecognition is enabled for this user. - */ - protected function facerecognitionIsEnabled(): bool - { - return Util::facerecognitionIsEnabled($this->config, Util::getUID()); - } - - /** - * Check if geolocation is enabled for this user. - */ - protected function placesIsEnabled(): bool - { - return Util::placesGISType() > 0; - } -} diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index 28dc6b05..45ab8074 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -299,7 +299,7 @@ class ImageController extends GenericApiController private function getTags(int $fileId): array { // Make sure tags are enabled - if (!Util::tagsIsEnabled($this->appManager)) { + if (!Util::tagsIsEnabled()) { return []; } diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index a79f9f76..fd932f88 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -4,6 +4,7 @@ namespace OCA\Memories\Controller; use OCA\Files\Event\LoadSidebar; use OCA\Memories\AppInfo\Application; +use OCA\Memories\Util; use OCP\App\IAppManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\ContentSecurityPolicy; @@ -13,7 +14,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IRequest; use OCP\IUserSession; -use OCP\Util; +use OCP\Util as OCPUtil; class PageController extends Controller { @@ -58,7 +59,7 @@ class PageController extends Controller } // Scripts - Util::addScript($this->appName, 'memories-main'); + OCPUtil::addScript($this->appName, 'memories-main'); $this->eventDispatcher->dispatchTyped(new LoadSidebar()); // Configuration @@ -81,19 +82,19 @@ class PageController extends Controller $pi('enableTopMemories', 'true'); // Apps enabled - $this->initialState->provideInitialState('systemtags', true === $this->appManager->isEnabledForUser('systemtags')); - $this->initialState->provideInitialState('recognize', \OCA\Memories\Util::recognizeIsEnabled($this->appManager)); - $this->initialState->provideInitialState('facerecognitionInstalled', \OCA\Memories\Util::facerecognitionIsInstalled($this->appManager)); - $this->initialState->provideInitialState('facerecognitionEnabled', \OCA\Memories\Util::facerecognitionIsEnabled($this->config, $uid)); - $this->initialState->provideInitialState('albums', \OCA\Memories\Util::albumsIsEnabled($this->appManager)); + $this->initialState->provideInitialState('systemtags', Util::tagsIsEnabled()); + $this->initialState->provideInitialState('recognize', Util::recognizeIsEnabled()); + $this->initialState->provideInitialState('facerecognitionInstalled', Util::facerecognitionIsInstalled()); + $this->initialState->provideInitialState('facerecognitionEnabled', Util::facerecognitionIsEnabled()); + $this->initialState->provideInitialState('albums', Util::albumsIsEnabled()); // Common state self::provideCommonInitialState($this->initialState); // Extra translations - if (\OCA\Memories\Util::recognizeIsEnabled($this->appManager)) { + if (Util::recognizeIsEnabled()) { // Auto translation for tags - Util::addTranslations('recognize'); + OCPUtil::addTranslations('recognize'); } $response = new TemplateResponse($this->appName, 'main'); diff --git a/lib/Controller/TagsController.php b/lib/Controller/TagsController.php index 67de2d3c..2e1cfed5 100644 --- a/lib/Controller/TagsController.php +++ b/lib/Controller/TagsController.php @@ -39,7 +39,7 @@ class TagsController extends GenericApiController { return Util::guardEx(function () use ($id, $add, $remove) { // Check tags enabled for this user - if (!$this->tagsIsEnabled()) { + if (!Util::tagsIsEnabled()) { throw Exceptions::NotEnabled('Tags'); } diff --git a/lib/Util.php b/lib/Util.php index 3ace47f0..60b0d0fe 100644 --- a/lib/Util.php +++ b/lib/Util.php @@ -59,8 +59,10 @@ class Util /** * Check if albums are enabled for this user. */ - public static function albumsIsEnabled(IAppManager &$appManager): bool + public static function albumsIsEnabled(): bool { + $appManager = \OC::$server->get(IAppManager::class); + if (!$appManager->isEnabledForUser('photos')) { return false; } @@ -72,19 +74,21 @@ class Util /** * Check if tags is enabled for this user. - * - * @param mixed $appManager */ - public static function tagsIsEnabled(&$appManager): bool + public static function tagsIsEnabled(): bool { + $appManager = \OC::$server->get(IAppManager::class); + return $appManager->isEnabledForUser('systemtags'); } /** * Check if recognize is enabled for this user. */ - public static function recognizeIsEnabled(IAppManager &$appManager): bool + public static function recognizeIsEnabled(): bool { + $appManager = \OC::$server->get(IAppManager::class); + if (!$appManager->isEnabledForUser('recognize')) { return false; } @@ -105,9 +109,16 @@ class Util /** * Check if Face Recognition is enabled by the user. */ - public static function facerecognitionIsEnabled(IConfig &$config, string $userId): bool + public static function facerecognitionIsEnabled(): bool { - $e = $config->getUserValue($userId, 'facerecognition', 'enabled', 'false'); + try { + $uid = self::getUID(); + } catch (\Exception $e) { + return false; + } + + $config = \OC::$server->get(IConfig::class); + $e = $config->getUserValue($uid, 'facerecognition', 'enabled', 'false'); return 'true' === $e; } @@ -115,8 +126,10 @@ class Util /** * Check if Face Recognition is installed and enabled for this user. */ - public static function facerecognitionIsInstalled(IAppManager &$appManager): bool + public static function facerecognitionIsInstalled(): bool { + $appManager = \OC::$server->get(IAppManager::class); + if (!$appManager->isEnabledForUser('facerecognition')) { return false; } @@ -129,8 +142,10 @@ class Util /** * Check if link sharing is allowed. */ - public static function isLinkSharingEnabled(IConfig &$config): bool + public static function isLinkSharingEnabled(): bool { + $config = \OC::$server->get(IConfig::class); + // Check if the shareAPI is enabled if ('yes' !== $config->getAppValue('core', 'shareapi_enabled', 'yes')) { return false; diff --git a/lib/UtilController.php b/lib/UtilController.php index ef2795b9..7d2c8015 100644 --- a/lib/UtilController.php +++ b/lib/UtilController.php @@ -12,7 +12,7 @@ trait UtilController /** * Run a function and catch exceptions to return HTTP response. * - * @param Function $function + * @param mixed $function */ public static function guardEx($function): Http\Response {