refactor: program as a transform insstead of duplication

pull/376/head
Raymond Huang 2023-02-01 11:47:57 +08:00
parent 2097f26d17
commit 60ee600c52
6 changed files with 50 additions and 337 deletions

View File

@ -77,9 +77,6 @@ return [
['name' => 'Download#file', 'url' => '/api/download/{handle}', 'verb' => 'GET'], ['name' => 'Download#file', 'url' => '/api/download/{handle}', 'verb' => 'GET'],
['name' => 'Download#one', 'url' => '/api/stream/{fileid}', 'verb' => 'GET'], ['name' => 'Download#one', 'url' => '/api/stream/{fileid}', 'verb' => 'GET'],
['name' => 'Locations#daysWithBounds', 'url' => '/api/location/days/{minLat}/{maxLat}/{minLng}/{maxLng}', 'verb' => 'GET'],
['name' => 'Locations#dayWithBounds', 'url' => '/api/location/days/{id}/{minLat}/{maxLat}/{minLng}/{maxLng}', 'verb' => 'GET'],
// Config API // Config API
['name' => 'Other#setUserConfig', 'url' => '/api/config/{key}', 'verb' => 'PUT'], ['name' => 'Other#setUserConfig', 'url' => '/api/config/{key}', 'verb' => 'PUT'],

View File

@ -243,6 +243,15 @@ class DaysController extends ApiBase
$transforms[] = [$this->timelineQuery, 'transformLimitDay', (int) $limit]; $transforms[] = [$this->timelineQuery, 'transformLimitDay', (int) $limit];
} }
// Filter geological bounds
$minLat = $this->request->getParam('minLat');
$maxLat = $this->request->getParam('maxLat');
$minLng = $this->request->getParam('minLng');
$maxLng = $this->request->getParam('maxLng');
if ($minLat && $maxLat && $minLng && $maxLng) {
$transforms[] = [$this->timelineQuery, 'transformBoundFilter', $minLat, $maxLat, $minLng, $maxLng];
}
return $transforms; return $transforms;
} }

View File

@ -1,299 +0,0 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
* @author Varun Patil <radialapps@gmail.com>, Raymond Huang <raymond860909@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\Db\TimelineRoot;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
class LocationsController extends ApiBase
{
use FoldersTrait;
/**
* @NoAdminRequired
*
* @PublicPage
*/
public function daysWithBounds(string $minLat, string $maxLat, string $minLng, string $maxLng): JSONResponse
{
if (null === $minLat || null === $maxLat || null === $minLng || null === $maxLng) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}
// Get the folder to show
try {
$uid = $this->getUID();
} catch (\Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_PRECONDITION_FAILED);
}
// Get the folder to show
$root = null;
try {
$root = $this->getRequestRoot();
} catch (\Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_NOT_FOUND);
}
// Run actual query
try {
$list = $this->timelineQuery->getDaysWithBounds(
$root,
$uid,
$this->isRecursive(),
$this->isArchive(),
$this->getTransformations(true),
$minLat,
$maxLat,
$minLng,
$maxLng
);
if ($this->isMonthView()) {
// Group days together into months
$list = $this->timelineQuery->daysToMonths($list);
} else {
// Preload some day responses
$this->preloadDaysWithBounds($list, $uid, $root, $minLat, $maxLat, $minLng, $maxLng);
}
// Reverse response if requested. Folders still stay at top.
if ($this->isReverse()) {
$list = array_reverse($list);
}
// Add subfolder info if querying non-recursively
if (!$this->isRecursive()) {
array_unshift($list, $this->getSubfoldersEntry($root->getFolder($root->getOneId())));
}
return new JSONResponse($list, Http::STATUS_OK);
} catch (\Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
public function dayWithBounds(string $id, string $minLat, string $maxLat, string $minLng, string $maxLng): JSONResponse
{
if (null === $minLat || null === $maxLat || null === $minLng || null === $maxLng) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}
// Get user
$uid = $this->getUID();
// Check for wildcard
$dayIds = [];
if ('*' === $id) {
$dayIds = null;
} else {
// Split at commas and convert all parts to int
$dayIds = array_map(function ($part) {
return (int) $part;
}, explode(',', $id));
}
// Check if $dayIds is empty
if (null !== $dayIds && 0 === \count($dayIds)) {
return new JSONResponse([], Http::STATUS_OK);
}
// Get the folder to show
$root = null;
try {
$root = $this->getRequestRoot();
} catch (\Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_NOT_FOUND);
}
// Convert to actual dayIds if month view
if ($this->isMonthView()) {
$dayIds = $this->timelineQuery->monthIdToDayIds((int) $dayIds[0]);
}
// Run actual query
try {
$list = $this->timelineQuery->getDayWithBounds(
$root,
$uid,
$dayIds,
$this->isRecursive(),
$this->isArchive(),
$this->getTransformations(false),
$minLat,
$maxLat,
$minLng,
$maxLng
);
// Force month id for dayId for month view
if ($this->isMonthView()) {
foreach ($list as &$photo) {
$photo['dayid'] = (int) $dayIds[0];
}
}
// Reverse response if requested.
if ($this->isReverse()) {
$list = array_reverse($list);
}
return new JSONResponse($list, Http::STATUS_OK);
} catch (\Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get transformations depending on the request.
*
* @param bool $aggregateOnly Only apply transformations for aggregation (days call)
*/
private function getTransformations(bool $aggregateOnly)
{
$transforms = [];
// Add extra information, basename and mimetype
if (!$aggregateOnly && ($fields = $this->request->getParam('fields'))) {
$fields = explode(',', $fields);
$transforms[] = [$this->timelineQuery, 'transformExtraFields', $fields];
}
// Filter for one album
if ($this->albumsIsEnabled()) {
if ($albumId = $this->request->getParam('album')) {
$transforms[] = [$this->timelineQuery, 'transformAlbumFilter', $albumId];
}
}
// Other transforms not allowed for public shares
if (null === $this->userSession->getUser()) {
return $transforms;
}
// Filter only favorites
if ($this->request->getParam('fav')) {
$transforms[] = [$this->timelineQuery, 'transformFavoriteFilter'];
}
// Filter only videos
if ($this->request->getParam('vid')) {
$transforms[] = [$this->timelineQuery, 'transformVideoFilter'];
}
// Filter only for one face on Recognize
if (($recognize = $this->request->getParam('recognize')) && $this->recognizeIsEnabled()) {
$transforms[] = [$this->timelineQuery, 'transformPeopleRecognitionFilter', $recognize];
$faceRect = $this->request->getParam('facerect');
if ($faceRect && !$aggregateOnly) {
$transforms[] = [$this->timelineQuery, 'transformPeopleRecognizeRect', $recognize];
}
}
// Filter only for one face on Face Recognition
if (($face = $this->request->getParam('facerecognition')) && $this->facerecognitionIsEnabled()) {
$currentModel = (int) $this->config->getAppValue('facerecognition', 'model', -1);
$transforms[] = [$this->timelineQuery, 'transformPeopleFaceRecognitionFilter', $currentModel, $face];
$faceRect = $this->request->getParam('facerect');
if ($faceRect && !$aggregateOnly) {
$transforms[] = [$this->timelineQuery, 'transformPeopleFaceRecognitionRect', $face];
}
}
// Filter only for one tag
if ($this->tagsIsEnabled()) {
if ($tagName = $this->request->getParam('tag')) {
$transforms[] = [$this->timelineQuery, 'transformTagFilter', $tagName];
}
}
// Limit number of responses for day query
$limit = $this->request->getParam('limit');
if ($limit) {
$transforms[] = [$this->timelineQuery, 'transformLimitDay', (int) $limit];
}
return $transforms;
}
/**
* Preload a few "day" at the start of "days" response.
*
* @param array $days the days array
* @param string $uid User ID or blank for public shares
* @param TimelineRoot $root the root folder
*/
private function preloadDaysWithBounds(array &$days, string $uid, TimelineRoot &$root, string $minLat, string $maxLat, string $minLng, string $maxLng)
{
$transforms = $this->getTransformations(false);
$preloaded = 0;
$preloadDayIds = [];
$preloadDays = [];
foreach ($days as &$day) {
if ($day['count'] <= 0) {
continue;
}
$preloaded += $day['count'];
$preloadDayIds[] = $day['dayid'];
$preloadDays[] = &$day;
if ($preloaded >= 50 || \count($preloadDayIds) > 5) { // should be enough
break;
}
}
if (\count($preloadDayIds) > 0) {
$allDetails = $this->timelineQuery->getDayWithBounds(
$root,
$uid,
$preloadDayIds,
$this->isRecursive(),
$this->isArchive(),
$transforms,
$minLat,
$maxLat,
$minLng,
$maxLng
);
// Group into dayid
$detailMap = [];
foreach ($allDetails as &$detail) {
$detailMap[$detail['dayid']][] = &$detail;
}
foreach ($preloadDays as &$day) {
$m = $detailMap[$day['dayid']];
if (isset($m) && null !== $m && \count($m) > 0) {
$day['detail'] = $m;
}
}
}
}
}

View File

@ -11,18 +11,28 @@ trait TimelineQueryFilters
{ {
public function transformFavoriteFilter(IQueryBuilder &$query, string $userId) public function transformFavoriteFilter(IQueryBuilder &$query, string $userId)
{ {
$query->innerJoin('m', 'vcategory_to_object', 'vcoi', $query->expr()->andX( $query->innerJoin(
'm',
'vcategory_to_object',
'vcoi',
$query->expr()->andX(
$query->expr()->eq('vcoi.objid', 'm.fileid'), $query->expr()->eq('vcoi.objid', 'm.fileid'),
$query->expr()->in('vcoi.categoryid', $this->getFavoriteVCategoryFun($query, $userId)), $query->expr()->in('vcoi.categoryid', $this->getFavoriteVCategoryFun($query, $userId)),
)); )
);
} }
public function addFavoriteTag(IQueryBuilder &$query, string $userId) public function addFavoriteTag(IQueryBuilder &$query, string $userId)
{ {
$query->leftJoin('m', 'vcategory_to_object', 'vco', $query->expr()->andX( $query->leftJoin(
'm',
'vcategory_to_object',
'vco',
$query->expr()->andX(
$query->expr()->eq('vco.objid', 'm.fileid'), $query->expr()->eq('vco.objid', 'm.fileid'),
$query->expr()->in('vco.categoryid', $this->getFavoriteVCategoryFun($query, $userId)), $query->expr()->in('vco.categoryid', $this->getFavoriteVCategoryFun($query, $userId)),
)); )
);
$query->addSelect('vco.categoryid'); $query->addSelect('vco.categoryid');
} }
@ -40,6 +50,18 @@ trait TimelineQueryFilters
$query->setMaxResults($limit); $query->setMaxResults($limit);
} }
public function transformBoundFilter(IQueryBuilder &$query, string $userId, string $minLat, string $maxLat, string $minLng, string $maxLng)
{
$query->andWhere(
$query->expr()->andX(
$query->expr()->gte('m.latitude', $query->createNamedParameter($minLat, IQueryBuilder::PARAM_STR)),
$query->expr()->lte('m.latitude', $query->createNamedParameter($maxLat, IQueryBuilder::PARAM_STR)),
$query->expr()->gte('m.longitude', $query->createNamedParameter($minLng, IQueryBuilder::PARAM_STR)),
$query->expr()->lte('m.longitude', $query->createNamedParameter($maxLng, IQueryBuilder::PARAM_STR))
)
);
}
private function applyAllTransforms(array $transforms, IQueryBuilder &$query, string $uid): void private function applyAllTransforms(array $transforms, IQueryBuilder &$query, string $uid): void
{ {
foreach ($transforms as &$transform) { foreach ($transforms as &$transform) {

View File

@ -698,6 +698,14 @@ export default defineComponent({
query.set("reverse", "1"); query.set("reverse", "1");
} }
// Geological Bounds
if (this.$route.name === "locations") {
query.set("minLat", "" + this.mapBoundary.minLat);
query.set("maxLat", "" + this.mapBoundary.maxLat);
query.set("minLng", "" + this.mapBoundary.minLng);
query.set("maxLng", "" + this.mapBoundary.maxLng);
}
return query; return query;
}, },
@ -731,12 +739,7 @@ export default defineComponent({
/** Fetch timeline main call */ /** Fetch timeline main call */
async fetchDays(noCache = false) { async fetchDays(noCache = false) {
let url = ""; const url = API.Q(API.DAYS(), this.getQuery());
if (this.$route.name === "locations") {
url = API.Q(API.DAYS_WITH_BOUNDS(this.mapBoundary), this.getQuery());
} else {
url = API.Q(API.DAYS(), this.getQuery());
}
const cacheUrl = <string>this.$route.name + url; const cacheUrl = <string>this.$route.name + url;
// Try cache first // Try cache first
@ -902,14 +905,7 @@ export default defineComponent({
/** API url for Day call */ /** API url for Day call */
getDayUrl(dayId: number | string) { getDayUrl(dayId: number | string) {
if (this.$route.name === "locations") {
return API.Q(
API.DAY_WITH_BOUNDS(dayId, this.mapBoundary),
this.getQuery()
);
} else {
return API.Q(API.DAY(dayId), this.getQuery()); return API.Q(API.DAY(dayId), this.getQuery());
}
}, },
/** Fetch image data for one dayId */ /** Fetch image data for one dayId */
@ -1439,20 +1435,16 @@ export default defineComponent({
/** Static and dynamic top matter */ /** Static and dynamic top matter */
.top-matter { .top-matter {
padding-top: 4px; padding-top: 4px;
@include phone { @include phone {
padding-left: 40px; padding-left: 40px;
} }
} }
.recycler-before { .recycler-before {
width: 100%; width: 100%;
> .text { > .text {
font-size: 1.2em; font-size: 1.2em;
padding-top: 13px; padding-top: 13px;
padding-left: 8px; padding-left: 8px;
@include phone { @include phone {
padding-left: 48px; padding-left: 48px;
} }

View File

@ -40,14 +40,6 @@ export class API {
return tok(gen(`${BASE}/days/{id}`, { id })); return tok(gen(`${BASE}/days/{id}`, { id }));
} }
static DAYS_WITH_BOUNDS(mapBoundary: MapBoundary) {
return tok(gen(`${BASE}/location/days/{minLat}/{maxLat}/{minLng}/{maxLng}`, mapBoundary));
}
static DAY_WITH_BOUNDS(id: number | string, mapBoundary: MapBoundary) {
return tok(gen(`${BASE}/location/days/{id}/{minLat}/{maxLat}/{minLng}/{maxLng}`, { id, ...mapBoundary }));
}
static ALBUM_LIST(t: "1" | "2" | "3" = "3") { static ALBUM_LIST(t: "1" | "2" | "3" = "3") {
return gen(`${BASE}/albums?t=${t}`); return gen(`${BASE}/albums?t=${t}`);
} }