memories/lib/Controller/DaysController.php

225 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2022-10-29 18:05:05 +00:00
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 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\ClustersBackend;
use OCA\Memories\Util;
2022-10-29 18:05:05 +00:00
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
class DaysController extends GenericApiController
2022-10-29 18:05:05 +00:00
{
/**
* @NoAdminRequired
2022-11-07 04:48:10 +00:00
*
2022-10-29 18:05:05 +00:00
* @PublicPage
*/
public function days(): Http\Response
2022-10-29 18:05:05 +00:00
{
return Util::guardEx(function () {
$list = $this->tq->getDays(
2022-11-03 22:39:48 +00:00
$this->isRecursive(),
$this->isArchive(),
$this->isMonthView(),
$this->isReverse(),
$this->getTransformations(),
2022-10-29 18:05:05 +00:00
);
// Preload some day responses
$this->preloadDays($list);
2022-10-29 18:05:05 +00:00
return new JSONResponse($list, Http::STATUS_OK);
});
2022-10-29 18:05:05 +00:00
}
/**
* @NoAdminRequired
*
* @PublicPage
*
* @param int[] $dayIds
2022-10-29 18:05:05 +00:00
*/
public function day(array $dayIds): Http\Response
2022-10-29 18:05:05 +00:00
{
return Util::guardEx(function () use ($dayIds) {
// Run actual query
$list = $this->tq->getDay(
2022-11-03 22:39:48 +00:00
$dayIds,
$this->isRecursive(),
$this->isArchive(),
$this->isHidden(),
$this->isMonthView(),
$this->isReverse(),
$this->getTransformations(),
2022-10-29 18:05:05 +00:00
);
return new JSONResponse($list, Http::STATUS_OK);
});
2022-10-29 18:05:05 +00:00
}
2022-10-29 18:09:38 +00:00
/**
* @NoAdminRequired
*
* @PublicPage
*/
public function dayGet(string $id): Http\Response
2022-10-29 18:09:38 +00:00
{
// Split at commas and convert all parts to int
return $this->day(array_map(static fn ($p) => (int) $p, explode(',', $id)));
2022-10-29 18:09:38 +00:00
}
2023-02-08 21:53:38 +00:00
/**
* Get transformations depending on the request.
*/
private function getTransformations(): array
2023-02-08 21:53:38 +00:00
{
$transforms = [];
// Add clustering transforms
$clusterTs = ClustersBackend\Manager::getTransforms($this->request);
$transforms = array_merge($transforms, $clusterTs);
2023-02-08 21:53:38 +00:00
// Other transforms not allowed for public shares
if (!Util::isLoggedIn()) {
2023-02-08 21:53:38 +00:00
return $transforms;
}
// Filter only favorites
if ($this->request->getParam('fav')) {
$transforms[] = [$this->tq, 'transformFavoriteFilter'];
2023-02-08 21:53:38 +00:00
}
// Filter only videos
if ($this->request->getParam('vid')) {
$transforms[] = [$this->tq, 'transformVideoFilter'];
2023-02-08 21:53:38 +00:00
}
// Filter geological bounds
if ($bounds = $this->request->getParam('mapbounds')) {
$transforms[] = [$this->tq, 'transformMapBoundsFilter', $bounds];
2023-02-08 21:53:38 +00:00
}
// Limit number of responses for day query
if ($limit = $this->request->getParam('limit')) {
$transforms[] = [$this->tq, 'transformLimit', (int) $limit];
2023-02-08 21:53:38 +00:00
}
// Add extra fields for native callers
if (Util::callerIsNative()) {
$transforms[] = [$this->tq, 'transformNativeQuery'];
}
2023-02-08 21:53:38 +00:00
return $transforms;
}
2022-10-29 18:05:05 +00:00
/**
* Preload a few "day" at the start of "days" response.
*
* @param array $days the days array (modified in place)
2022-10-29 18:05:05 +00:00
*/
private function preloadDays(array &$days): void
2022-10-29 18:05:05 +00:00
{
// Do not preload anything for native clients.
// Since the contents of preloads are trusted, clients will not load locals.
if (Util::callerIsNative() || $this->noPreload()) {
return;
}
// Construct map of dayid-day
$totalCount = 0;
$drefMap = [];
2022-10-29 18:05:05 +00:00
foreach ($days as &$day) {
if ($count = (int) $day['count']) {
$totalCount += max($count, 10); // max 5 days
2022-10-29 18:05:05 +00:00
}
$dayId = (int) $day['dayid'];
$drefMap[$dayId] = &$day;
2022-10-29 18:05:05 +00:00
if ($totalCount >= 50) { // should be enough
2022-10-29 18:05:05 +00:00
break;
}
}
if (!$totalCount) {
return;
}
2022-10-29 18:05:05 +00:00
// Preload photos for these days
$details = $this->tq->getDay(
array_keys($drefMap),
$this->isRecursive(),
$this->isArchive(),
$this->isHidden(),
$this->isMonthView(),
$this->isReverse(),
$this->getTransformations(),
);
// Load details into map byref
foreach ($details as $photo) {
$dayId = (int) $photo['dayid'];
if (!($drefMap[$dayId] ?? null)) {
continue;
2022-10-29 18:05:05 +00:00
}
if (!($drefMap[$dayId]['detail'] ?? null)) {
$drefMap[$dayId]['detail'] = [];
2022-10-29 18:05:05 +00:00
}
$drefMap[$dayId]['detail'][] = $photo;
2022-10-29 18:05:05 +00:00
}
}
private function isRecursive(): bool
{
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
}
private function isArchive(): bool
{
return null !== $this->request->getParam('archive');
}
private function isHidden(): bool
{
return null !== $this->request->getParam('hidden');
}
private function noPreload(): bool
{
return null !== $this->request->getParam('nopreload');
}
private function isMonthView(): bool
{
return null !== $this->request->getParam('monthView');
}
private function isReverse(): bool
{
return null !== $this->request->getParam('reverse');
}
2022-10-29 18:05:05 +00:00
}