More refactor

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/563/head
Varun Patil 2023-03-23 17:56:41 -07:00
parent f6fa48e089
commit fa9205a11e
13 changed files with 142 additions and 156 deletions

View File

@ -26,6 +26,7 @@ namespace OCA\Memories\AppInfo;
use OCA\Memories\ClustersBackend;
use OCA\Memories\Listeners\PostDeleteListener;
use OCA\Memories\Listeners\PostWriteListener;
use OCA\Memories\Manager\ClustersBackendManager;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
@ -75,11 +76,11 @@ class Application extends App implements IBootstrap
$context->registerEventListener(NodeDeletedEvent::class, PostDeleteListener::class);
// Register clusters backends
ClustersBackend\Backend::register('albums', ClustersBackend\AlbumsBackend::class);
ClustersBackend\Backend::register('tags', ClustersBackend\TagsBackend::class);
ClustersBackend\Backend::register('places', ClustersBackend\PlacesBackend::class);
ClustersBackend\Backend::register('recognize', ClustersBackend\RecognizeBackend::class);
ClustersBackend\Backend::register('facerecognition', ClustersBackend\FaceRecognitionBackend::class);
ClustersBackendManager::register('albums', ClustersBackend\AlbumsBackend::class);
ClustersBackendManager::register('tags', ClustersBackend\TagsBackend::class);
ClustersBackendManager::register('places', ClustersBackend\PlacesBackend::class);
ClustersBackendManager::register('recognize', ClustersBackend\RecognizeBackend::class);
ClustersBackendManager::register('facerecognition', ClustersBackend\FaceRecognitionBackend::class);
}
public function boot(IBootContext $context): void

View File

@ -60,10 +60,8 @@ class AlbumsBackend extends Backend
{
$albumId = (string) $this->request->getParam('albums');
$uid = Util::isLoggedIn() ? Util::getUID() : '';
// Get album object
$album = $this->albumsQuery->getIfAllowed($uid, $albumId);
$album = $this->albumsQuery->getIfAllowed($this->getUID(), $albumId);
// Check permission
if (null === $album) {
@ -110,7 +108,7 @@ class AlbumsBackend extends Backend
public function getPhotos(string $name, ?int $limit = null): array
{
// Get album
$album = $this->albumsQuery->getIfAllowed(Util::getUID(), $name);
$album = $this->albumsQuery->getIfAllowed($this->getUID(), $name);
if (null === $album) {
throw Exceptions::NotFound("album {$name}");
}
@ -120,4 +118,9 @@ class AlbumsBackend extends Backend
return $this->albumsQuery->getAlbumPhotos($id, $limit) ?? [];
}
private function getUID(): string
{
return Util::isLoggedIn() ? Util::getUID() : '---';
}
}

View File

@ -24,13 +24,9 @@ declare(strict_types=1);
namespace OCA\Memories\ClustersBackend;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IRequest;
abstract class Backend
{
/** Mapping of backend name to className */
public static array $backends = [];
/**
* A human-readable name for the app.
* Used for error messages.
@ -71,66 +67,6 @@ abstract class Backend
*/
abstract public function getPhotos(string $name, ?int $limit = null): array;
/**
* Get a cluster backend.
*
* @param string $name Name of the backend
*
* @throws \Exception If the backend is not registered
*/
public static function get(string $name): self
{
if (!\array_key_exists($name, self::$backends)) {
throw new \Exception("Invalid clusters backend '{$name}'");
}
return \OC::$server->get(self::$backends[$name]);
}
/**
* Apply all query transformations for the given request.
*/
public static function getTransforms(IRequest $request): array
{
$transforms = [];
foreach (array_keys(self::$backends) as $backendName) {
if ($request->getParam($backendName)) {
$backend = self::get($backendName);
if ($backend->isEnabled()) {
$transforms[] = [$backend, 'transformDayQuery'];
}
}
}
return $transforms;
}
/**
* Apply all post-query transformations for the given day object.
*/
public static function applyDayPostTransforms(IRequest $request, array &$row): void
{
foreach (array_keys(self::$backends) as $backendName) {
if ($request->getParam($backendName)) {
$backend = self::get($backendName);
if ($backend->isEnabled()) {
$backend->transformDayPost($row);
}
}
}
}
/**
* Register a new backend.
*
* @param mixed $name
* @param mixed $className
*/
public static function register($name, $className): void
{
self::$backends[$name] = $className;
}
/**
* Human readable name for the cluster.
*/

View File

@ -25,6 +25,7 @@ namespace OCA\Memories\Controller;
use OCA\Memories\ClustersBackend\Backend;
use OCA\Memories\Exceptions;
use OCA\Memories\Manager\ClustersBackendManager;
use OCA\Memories\Util;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
@ -114,7 +115,7 @@ class ClustersController extends GenericApiController
throw Exceptions::NotLoggedIn();
}
$this->backend = Backend::get($backend);
$this->backend = ClustersBackendManager::get($backend);
if (!$this->backend->isEnabled()) {
throw Exceptions::NotEnabled($this->backend->appName());
@ -130,23 +131,13 @@ class ClustersController extends GenericApiController
$previewManager = \OC::$server->get(\OCP\IPreview::class);
// Try to get a preview
$userFolder = Util::getUserFolder();
foreach ($photos as $img) {
// Get the file
$files = $userFolder->getById($this->backend->getFileId($img));
if (0 === \count($files)) {
continue;
}
// Check read permission
if (!$files[0]->isReadable()) {
continue;
}
// Get preview image
try {
$quality = $this->backend->getPreviewQuality();
$file = $previewManager->getPreview($files[0], $quality, $quality, false);
$file = $this->fs->getUserFile($this->backend->getFileId($img));
$file = $previewManager->getPreview($file, $quality, $quality, false);
[$blob, $mimetype] = $this->backend->getPreviewBlob($file, $img);

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OCA\Memories\Controller;
use OCA\Memories\Exceptions;
use OCA\Memories\Manager\ClustersBackendManager;
use OCA\Memories\Util;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
@ -145,7 +146,7 @@ class DaysController extends GenericApiController
$transforms = [];
// Add clustering transforms
$transforms = array_merge($transforms, \OCA\Memories\ClustersBackend\Backend::getTransforms($this->request));
$transforms = array_merge($transforms, ClustersBackendManager::getTransforms($this->request));
// Other transforms not allowed for public shares
if (!Util::isLoggedIn()) {
@ -259,4 +260,24 @@ class DaysController extends GenericApiController
return $dayIds;
}
private function isRecursive()
{
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
}
private function isArchive()
{
return null !== $this->request->getParam('archive');
}
private function isMonthView()
{
return null !== $this->request->getParam('monthView');
}
private function isReverse()
{
return null !== $this->request->getParam('reverse');
}
}

View File

@ -37,8 +37,6 @@ use Psr\Log\LoggerInterface;
abstract class GenericApiController extends Controller
{
use GenericApiControllerParams;
protected IConfig $config;
protected IUserSession $userSession;
protected IRootFolder $rootFolder;

View File

@ -1,54 +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;
trait GenericApiControllerParams
{
/**
* current request.
*
* @var \OCP\IRequest
*/
protected $request;
protected function isRecursive()
{
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
}
protected function isArchive()
{
return null !== $this->request->getParam('archive');
}
protected function isMonthView()
{
return null !== $this->request->getParam('monthView');
}
protected function isReverse()
{
return null !== $this->request->getParam('reverse');
}
}

View File

@ -33,21 +33,19 @@ class MapController extends GenericApiController
/**
* @NoAdminRequired
*/
public function clusters(): Http\Response
public function clusters(string $bounds, string $zoom): Http\Response
{
return Util::guardEx(function () {
return Util::guardEx(function () use ($bounds, $zoom) {
// Make sure we have bounds and zoom level
// Zoom level is used to determine the grid length
$bounds = $this->request->getParam('bounds');
$zoomLevel = $this->request->getParam('zoom');
if (!$bounds || !$zoomLevel || !is_numeric($zoomLevel)) {
if (!$bounds || !$zoom || !is_numeric($zoom)) {
throw Exceptions::MissingParameter('bounds or zoom');
}
// A tweakable parameter to determine the number of boxes in the map
// Note: these parameters need to be changed in MapSplitMatter.vue as well
$clusterDensity = 1;
$gridLen = 180.0 / (2 ** $zoomLevel * $clusterDensity);
$gridLen = 180.0 / (2 ** $zoom * $clusterDensity);
$clusters = $this->timelineQuery->getMapClusters($gridLen, $bounds);

View File

@ -45,14 +45,12 @@ class OtherController extends GenericApiController
public function setUserConfig(string $key, string $value): Http\Response
{
return Util::guardEx(function () use ($key, $value) {
$uid = Util::getUID();
// Make sure not running in read-only mode
if ($this->config->getSystemValue('memories.readonly', false)) {
throw Exceptions::Forbidden('Cannot change settings in readonly mode');
}
$this->config->setUserValue($uid, Application::APPNAME, $key, $value);
$this->config->setUserValue(Util::getUID(), Application::APPNAME, $key, $value);
return new JSONResponse([], Http::STATUS_OK);
});

View File

@ -47,7 +47,7 @@ class TagsController extends GenericApiController
$file = $this->fs->getUserFile($id);
// Check the user is allowed to edit the file
if (!$file->isUpdateable() || !($file->getPermissions() & \OCP\Constants::PERMISSION_UPDATE)) {
if (!$file->isUpdateable()) {
throw Exceptions::ForbiddenFileUpdate($file->getName());
}

View File

@ -124,7 +124,7 @@ class AlbumsQuery
* @param string $uid UID of CURRENT user
* @param string $albumId $user/$name where $user is the OWNER of the album
*/
public function getIfAllowed(string $uid, string $albumId)
public function getIfAllowed(string $uid, string $albumId): ?array
{
$album = null;

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\Manager\ClustersBackendManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@ -274,7 +275,7 @@ trait TimelineQueryDays
}
// All cluster transformations
\OCA\Memories\ClustersBackend\Backend::applyDayPostTransforms($this->request, $row);
ClustersBackendManager::applyDayPostTransforms($this->request, $row);
// We don't need these fields
unset($row['datetaken']);

View File

@ -0,0 +1,93 @@
<?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\Manager;
use OCA\Memories\ClustersBackend\Backend;
use OCP\IRequest;
class ClustersBackendManager
{
/** Mapping of backend name to className */
public static array $backends = [];
/**
* Get a cluster backend.
*
* @param string $name Name of the backend
*
* @throws \Exception If the backend is not registered
*/
public static function get(string $name): Backend
{
if (!\array_key_exists($name, self::$backends)) {
throw new \Exception("Invalid clusters backend '{$name}'");
}
return \OC::$server->get(self::$backends[$name]);
}
/**
* Register a new backend.
*
* @param mixed $name
* @param mixed $className
*/
public static function register($name, $className): void
{
self::$backends[$name] = $className;
}
/**
* Apply all query transformations for the given request.
*/
public static function getTransforms(IRequest $request): array
{
$transforms = [];
foreach (array_keys(self::$backends) as $backendName) {
if ($request->getParam($backendName)) {
$backend = self::get($backendName);
if ($backend->isEnabled()) {
$transforms[] = [$backend, 'transformDayQuery'];
}
}
}
return $transforms;
}
/**
* Apply all post-query transformations for the given day object.
*/
public static function applyDayPostTransforms(IRequest $request, array &$row): void
{
foreach (array_keys(self::$backends) as $backendName) {
if ($request->getParam($backendName)) {
$backend = self::get($backendName);
if ($backend->isEnabled()) {
$backend->transformDayPost($row);
}
}
}
}
}