util: remove duplicate checks

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/563/head
Varun Patil 2023-03-23 13:49:26 -07:00
parent bffefdd3ec
commit 5c9f1c4915
14 changed files with 64 additions and 165 deletions

View File

@ -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}");
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,18 +208,14 @@ class DaysController extends GenericApiController
}
// Filter only for one tag
if ($this->tagsIsEnabled()) {
if ($tagName = $this->request->getParam('tag')) {
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')) {
if (($locationId = $this->request->getParam('place')) && Util::placesGISType() > 0) {
$transforms[] = [$this->timelineQuery, 'transformPlaceFilter', (int) $locationId];
}
}
// Filter geological bounds
$bounds = $this->request->getParam('mapbounds');

View File

@ -38,7 +38,6 @@ abstract class GenericApiController extends Controller
{
use GenericApiControllerFs;
use GenericApiControllerParams;
use GenericApiControllerUtils;
protected IConfig $config;
protected IUserSession $userSession;

View File

@ -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;
}

View File

@ -1,80 +0,0 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Varun Patil <radialapps@gmail.com>
* @author Varun Patil <radialapps@gmail.com>
* @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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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 [];
}

View File

@ -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');

View File

@ -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');
}

View File

@ -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;

View File

@ -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
{