From bae5f99b2bbea19d778fda93e672c1c04f5a5d16 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 5 Feb 2023 19:46:44 -0800 Subject: [PATCH] meta: use places --- lib/Controller/ApiBase.php | 4 ++-- lib/Controller/DaysController.php | 2 +- lib/Controller/PageController.php | 3 +-- lib/Controller/PlacesController.php | 4 ++-- lib/Db/TimelineQuery.php | 18 +++++++++++++- lib/Db/TimelineWrite.php | 3 +-- lib/Util.php | 9 +++++++ src/components/Metadata.vue | 37 ++--------------------------- src/types.ts | 1 + 9 files changed, 36 insertions(+), 45 deletions(-) diff --git a/lib/Controller/ApiBase.php b/lib/Controller/ApiBase.php index 7f3dda34..fc6727f7 100644 --- a/lib/Controller/ApiBase.php +++ b/lib/Controller/ApiBase.php @@ -374,9 +374,9 @@ class ApiBase extends Controller /** * Check if geolocation is enabled for this user. */ - protected function geoPlacesIsEnabled(): bool + protected function placesIsEnabled(): bool { - return true; + return \OCA\Memories\Util::placesGISType() !== 0; } /** diff --git a/lib/Controller/DaysController.php b/lib/Controller/DaysController.php index 2b4a9b0d..206914f6 100644 --- a/lib/Controller/DaysController.php +++ b/lib/Controller/DaysController.php @@ -238,7 +238,7 @@ class DaysController extends ApiBase } // Filter only for one place - if ($this->geoPlacesIsEnabled()) { + if ($this->placesIsEnabled()) { if ($locationId = $this->request->getParam('place')) { $transforms[] = [$this->timelineQuery, 'transformPlaceFilter', (int) $locationId]; } diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 31a30a1f..984131d1 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -114,8 +114,7 @@ class PageController extends Controller // Image editor $policy->addAllowedConnectDomain('data:'); - // Allow nominatim for metadata - $policy->addAllowedConnectDomain('nominatim.openstreetmap.org'); + // Allow OSM $policy->addAllowedFrameDomain('www.openstreetmap.org'); return $policy; diff --git a/lib/Controller/PlacesController.php b/lib/Controller/PlacesController.php index af8119e9..bbc025bd 100644 --- a/lib/Controller/PlacesController.php +++ b/lib/Controller/PlacesController.php @@ -43,7 +43,7 @@ class PlacesController extends ApiBase } // Check tags enabled for this user - if (!$this->geoPlacesIsEnabled()) { + if (!$this->placesIsEnabled()) { return new JSONResponse(['message' => 'Places not enabled'], Http::STATUS_PRECONDITION_FAILED); } @@ -74,7 +74,7 @@ class PlacesController extends ApiBase } // Check tags enabled for this user - if (!$this->geoPlacesIsEnabled()) { + if (!$this->placesIsEnabled()) { return new JSONResponse(['message' => 'Places not enabled'], Http::STATUS_PRECONDITION_FAILED); } diff --git a/lib/Db/TimelineQuery.php b/lib/Db/TimelineQuery.php index ba1c0b56..ec6c39d7 100644 --- a/lib/Db/TimelineQuery.php +++ b/lib/Db/TimelineQuery.php @@ -92,12 +92,28 @@ class TimelineQuery } } + $address = null; + if (!$basic && \OCA\Memories\Util::placesGISType() !== 0) { + $qb = $this->connection->getQueryBuilder(); + $qb->select('e.name') + ->from('memories_places', 'mp') + ->innerJoin('mp', 'memories_planet', 'e', $qb->expr()->eq('mp.osm_id', 'e.osm_id')) + ->where($qb->expr()->eq('mp.fileid', $qb->createNamedParameter($id, \PDO::PARAM_INT))) + ->orderBy('e.admin_level', 'DESC') + ; + $places = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN); + if (\count($places) > 0) { + $address = implode(', ', $places); + } + } + return [ 'fileid' => (int) $row['fileid'], 'dayid' => (int) $row['dayid'], - 'datetaken' => $utcTs, 'w' => (int) $row['w'], 'h' => (int) $row['h'], + 'datetaken' => $utcTs, + 'address' => $address, 'exif' => $exif, ]; } diff --git a/lib/Db/TimelineWrite.php b/lib/Db/TimelineWrite.php index 87e65794..338099a1 100644 --- a/lib/Db/TimelineWrite.php +++ b/lib/Db/TimelineWrite.php @@ -289,8 +289,7 @@ class TimelineWrite public function updateGeoData(File &$file, float $lat, float $lon): void { // Get GIS type - $config = \OC::$server->get(\OCP\IConfig::class); - $gisType = $config->getSystemValue('memories.gis_type', 0); + $gisType = \OCA\Memories\Util::placesGISType(); // Construct WHERE clause depending on GIS type $where = null; diff --git a/lib/Util.php b/lib/Util.php index ba921f8b..666d7a62 100644 --- a/lib/Util.php +++ b/lib/Util.php @@ -142,6 +142,15 @@ class Util return false; } + /** + * Check if geolocation (places) is enabled and available. + * Returns the type of the GIS. + */ + public static function placesGISType(): int + { + return \OC::$server->get(\OCP\IConfig::class)->getSystemValue('memories.gis_type', 0); + } + /** * Kill all instances of a process by name. * Similar to pkill, which may not be available on all systems. diff --git a/src/components/Metadata.vue b/src/components/Metadata.vue index 410f9870..9773df0e 100644 --- a/src/components/Metadata.vue +++ b/src/components/Metadata.vue @@ -87,7 +87,6 @@ export default defineComponent({ fileInfo: null as IFileInfo, exif: {} as { [prop: string]: any }, baseInfo: {} as any, - nominatim: null as any, state: 0, }), @@ -245,22 +244,7 @@ export default defineComponent({ }, address(): string | null { - if (!this.lat || !this.lon) return null; - - if (!this.nominatim) return this.t("memories", "Loading …"); - - const n = this.nominatim; - const country = n.address.country_code?.toUpperCase(); - - if (n.address?.city && n.address.state) { - return `${n.address.city}, ${n.address.state}, ${country}`; - } else if (n.address?.state) { - return `${n.address.state}, ${country}`; - } else if (n.address?.country) { - return n.address.country; - } else { - return n.display_name; - } + return this.baseInfo.address; }, lat(): number { @@ -293,7 +277,6 @@ export default defineComponent({ this.state = Math.random(); this.fileInfo = fileInfo; this.exif = {}; - this.nominatim = null; const state = this.state; const url = API.IMAGE_INFO(fileInfo.id); @@ -302,9 +285,6 @@ export default defineComponent({ this.baseInfo = res.data; this.exif = res.data.exif || {}; - - // Lazy loading - this.getNominatim().catch(); }, handleFileUpdated({ fileid }) { @@ -312,19 +292,6 @@ export default defineComponent({ this.update(this.fileInfo); } }, - - async getNominatim() { - const lat = this.lat; - const lon = this.lon; - if (!lat || !lon) return null; - - const state = this.state; - const n = await axios.get( - `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json&zoom=18` - ); - if (state !== this.state) return; - this.nominatim = n.data; - }, }, }); @@ -368,4 +335,4 @@ export default defineComponent({ min-height: 200px; max-height: 250px; } - \ No newline at end of file + diff --git a/src/types.ts b/src/types.ts index ad7b1e38..01775c09 100644 --- a/src/types.ts +++ b/src/types.ts @@ -76,6 +76,7 @@ export type IPhoto = { h: number; w: number; datetaken: number; + address?: string; exif?: { Rotation?: number; Orientation?: number;