diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 5bd8dd0e..abfd751f 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -75,11 +75,11 @@ class Application extends App implements IBootstrap $context->registerEventListener(NodeDeletedEvent::class, PostDeleteListener::class); // Register clusters backends - ClustersBackend\Manager::register('albums', ClustersBackend\AlbumsBackend::class); - ClustersBackend\Manager::register('tags', ClustersBackend\TagsBackend::class); - ClustersBackend\Manager::register('places', ClustersBackend\PlacesBackend::class); - ClustersBackend\Manager::register('recognize', ClustersBackend\RecognizeBackend::class); - ClustersBackend\Manager::register('facerecognition', ClustersBackend\FaceRecognitionBackend::class); + ClustersBackend\AlbumsBackend::register(); + ClustersBackend\TagsBackend::register(); + ClustersBackend\PlacesBackend::register(); + ClustersBackend\RecognizeBackend::register(); + ClustersBackend\FaceRecognitionBackend::register(); } public function boot(IBootContext $context): void diff --git a/lib/ClustersBackend/AlbumsBackend.php b/lib/ClustersBackend/AlbumsBackend.php index 91d2acc7..a008b017 100644 --- a/lib/ClustersBackend/AlbumsBackend.php +++ b/lib/ClustersBackend/AlbumsBackend.php @@ -41,11 +41,16 @@ class AlbumsBackend extends Backend $this->request = $request; } - public function appName(): string + public static function appName(): string { return 'Albums'; } + public static function clusterType(): string + { + return 'albums'; + } + public function isEnabled(): bool { return Util::albumsIsEnabled(); @@ -105,6 +110,11 @@ class AlbumsBackend extends Backend return array_values($list); } + public static function getClusterId(array $cluster) + { + return $cluster['album_id']; + } + public function getPhotos(string $name, ?int $limit = null): array { // Get album diff --git a/lib/ClustersBackend/Backend.php b/lib/ClustersBackend/Backend.php index 89c90f3f..8ad162ef 100644 --- a/lib/ClustersBackend/Backend.php +++ b/lib/ClustersBackend/Backend.php @@ -31,7 +31,12 @@ abstract class Backend * A human-readable name for the app. * Used for error messages. */ - abstract public function appName(): string; + abstract static public function appName(): string; + + /** + * Get name of the cluster type. + */ + abstract static public function clusterType(): string; /** * Whether the app is enabled for the current user. @@ -58,6 +63,11 @@ abstract class Backend */ abstract public function getClusters(): array; + /** + * Get a cluster ID for the given cluster. + */ + abstract static public function getClusterId(array $cluster); + /** * Get a list of photos with any extra parameters for the given cluster * Used for preview generation and download. @@ -112,4 +122,12 @@ abstract class Backend { return (int) $photo['fileid']; } + + /** + * Register the backend. Do not override. + */ + public static function register(): void + { + Manager::register(static::clusterType(), static::class); + } } diff --git a/lib/ClustersBackend/FaceRecognitionBackend.php b/lib/ClustersBackend/FaceRecognitionBackend.php index 6e81c8e6..2ddce61d 100644 --- a/lib/ClustersBackend/FaceRecognitionBackend.php +++ b/lib/ClustersBackend/FaceRecognitionBackend.php @@ -46,11 +46,16 @@ class FaceRecognitionBackend extends Backend $this->config = $config; } - public function appName(): string + public static function appName(): string { return 'Face Recognition'; } + public static function clusterType(): string + { + return 'facerecognition'; + } + public function isEnabled(): bool { return Util::facerecognitionIsInstalled() @@ -134,6 +139,11 @@ class FaceRecognitionBackend extends Backend return $faces; } + public static function getClusterId(array $cluster) + { + return $cluster['id']; + } + public function getPhotos(string $name, ?int $limit = null): array { $query = $this->tq->getBuilder(); diff --git a/lib/ClustersBackend/PlacesBackend.php b/lib/ClustersBackend/PlacesBackend.php index 11ae221e..167c891e 100644 --- a/lib/ClustersBackend/PlacesBackend.php +++ b/lib/ClustersBackend/PlacesBackend.php @@ -38,11 +38,16 @@ class PlacesBackend extends Backend $this->request = $request; } - public function appName(): string + public static function appName(): string { return 'Places'; } + public static function clusterType(): string + { + return 'places'; + } + public function isEnabled(): bool { return Util::placesGISType() > 0; @@ -92,6 +97,11 @@ class PlacesBackend extends Backend return $places; } + public static function getClusterId(array $cluster) + { + return $cluster['osm_id']; + } + public function getPhotos(string $name, ?int $limit = null): array { $query = $this->tq->getBuilder(); diff --git a/lib/ClustersBackend/RecognizeBackend.php b/lib/ClustersBackend/RecognizeBackend.php index 3d59398b..e00549ed 100644 --- a/lib/ClustersBackend/RecognizeBackend.php +++ b/lib/ClustersBackend/RecognizeBackend.php @@ -40,11 +40,16 @@ class RecognizeBackend extends Backend $this->request = $request; } - public function appName(): string + public static function appName(): string { return 'Recognize'; } + public static function clusterType(): string + { + return 'recognize'; + } + public function isEnabled(): bool { return Util::recognizeIsEnabled(); @@ -162,6 +167,11 @@ class RecognizeBackend extends Backend return $faces; } + public static function getClusterId(array $cluster) + { + return $cluster['id']; + } + public function getPhotos(string $name, ?int $limit = null): array { $query = $this->tq->getBuilder(); diff --git a/lib/ClustersBackend/TagsBackend.php b/lib/ClustersBackend/TagsBackend.php index e305937b..c1db033d 100644 --- a/lib/ClustersBackend/TagsBackend.php +++ b/lib/ClustersBackend/TagsBackend.php @@ -39,11 +39,16 @@ class TagsBackend extends Backend $this->request = $request; } - public function appName(): string + public static function appName(): string { return 'Tags'; } + public static function clusterType(): string + { + return 'tags'; + } + public function isEnabled(): bool { return Util::tagsIsEnabled(); @@ -101,6 +106,11 @@ class TagsBackend extends Backend return $tags; } + public static function getClusterId(array $cluster) + { + return $cluster['name']; + } + public function getPhotos(string $name, ?int $limit = null): array { $query = $this->tq->getBuilder(); diff --git a/lib/Controller/ClustersController.php b/lib/Controller/ClustersController.php index cb6f6592..b3dd6a2d 100644 --- a/lib/Controller/ClustersController.php +++ b/lib/Controller/ClustersController.php @@ -47,6 +47,12 @@ class ClustersController extends GenericApiController $list = $this->backend->getClusters(); + // Set cluster_id and cluster_type for each cluster + foreach ($list as &$cluster) { + $cluster['cluster_id'] = $this->backend->getClusterId($cluster); + $cluster['cluster_type'] = $this->backend->clusterType(); + } + return new JSONResponse($list, Http::STATUS_OK); }); }