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;
|
|
|
|
|
2023-03-24 04:57:54 +00:00
|
|
|
use OCA\Memories\ClustersBackend;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Util;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
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
|
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function days(): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () {
|
2023-10-14 06:51:12 +00:00
|
|
|
$list = $this->tq->getDays(
|
2022-11-03 22:39:48 +00:00
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
2023-10-15 00:01:56 +00:00
|
|
|
$this->isMonthView(),
|
2023-10-29 18:19:12 +00:00
|
|
|
$this->isReverse(),
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->getTransformations(),
|
2022-10-29 18:05:05 +00:00
|
|
|
);
|
|
|
|
|
2023-10-15 00:20:21 +00:00
|
|
|
// Preload some day responses
|
|
|
|
$this->preloadDays($list);
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
2023-03-23 20:32:23 +00:00
|
|
|
});
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
2023-10-15 00:46:54 +00:00
|
|
|
*
|
|
|
|
* @param int[] $dayIds
|
2022-10-29 18:05:05 +00:00
|
|
|
*/
|
2023-10-15 00:46:54 +00:00
|
|
|
public function day(array $dayIds): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-10-15 00:46:54 +00:00
|
|
|
return Util::guardEx(function () use ($dayIds) {
|
2023-03-23 20:32:23 +00:00
|
|
|
// Run actual query
|
2023-10-14 06:51:12 +00:00
|
|
|
$list = $this->tq->getDay(
|
2022-11-03 22:39:48 +00:00
|
|
|
$dayIds,
|
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
2023-10-03 00:22:05 +00:00
|
|
|
$this->isHidden(),
|
2023-10-14 23:35:06 +00:00
|
|
|
$this->isMonthView(),
|
2023-10-29 18:19:12 +00:00
|
|
|
$this->isReverse(),
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->getTransformations(),
|
2022-10-29 18:05:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
2023-03-23 20:32:23 +00:00
|
|
|
});
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 18:09:38 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
2023-10-15 00:46:54 +00:00
|
|
|
public function dayGet(string $id): Http\Response
|
2022-10-29 18:09:38 +00:00
|
|
|
{
|
2023-10-15 00:46:54 +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.
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
private function getTransformations(): array
|
2023-02-08 21:53:38 +00:00
|
|
|
{
|
|
|
|
$transforms = [];
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
// Add clustering transforms
|
2023-03-24 04:57:54 +00:00
|
|
|
$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
|
2023-03-23 23:58:49 +00:00
|
|
|
if (!Util::isLoggedIn()) {
|
2023-02-08 21:53:38 +00:00
|
|
|
return $transforms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter only favorites
|
|
|
|
if ($this->request->getParam('fav')) {
|
2023-10-14 06:51:12 +00:00
|
|
|
$transforms[] = [$this->tq, 'transformFavoriteFilter'];
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter only videos
|
|
|
|
if ($this->request->getParam('vid')) {
|
2023-10-14 06:51:12 +00:00
|
|
|
$transforms[] = [$this->tq, 'transformVideoFilter'];
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter geological bounds
|
2023-03-23 23:58:49 +00:00
|
|
|
if ($bounds = $this->request->getParam('mapbounds')) {
|
2023-10-14 06:51:12 +00:00
|
|
|
$transforms[] = [$this->tq, 'transformMapBoundsFilter', $bounds];
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Limit number of responses for day query
|
2023-03-23 23:58:49 +00:00
|
|
|
if ($limit = $this->request->getParam('limit')) {
|
2023-10-14 06:51:12 +00:00
|
|
|
$transforms[] = [$this->tq, 'transformLimit', (int) $limit];
|
2023-02-08 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 06:07:07 +00:00
|
|
|
// Add extra fields for native callers
|
|
|
|
if (Util::callerIsNative()) {
|
2023-10-14 06:51:12 +00:00
|
|
|
$transforms[] = [$this->tq, 'transformNativeQuery'];
|
2023-08-21 06:07:07 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2023-10-14 22:37:05 +00:00
|
|
|
* @param array $days the days array (modified in place)
|
2022-10-29 18:05:05 +00:00
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
private function preloadDays(array &$days): void
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-05-04 02:59:23 +00:00
|
|
|
// Do not preload anything for native clients.
|
|
|
|
// Since the contents of preloads are trusted, clients will not load locals.
|
2023-10-03 00:54:56 +00:00
|
|
|
if (Util::callerIsNative() || $this->noPreload()) {
|
2023-05-04 02:59:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-14 22:37:05 +00:00
|
|
|
// Construct map of dayid-day
|
|
|
|
$totalCount = 0;
|
|
|
|
$drefMap = [];
|
2022-10-29 18:05:05 +00:00
|
|
|
foreach ($days as &$day) {
|
2023-10-14 22:37:05 +00:00
|
|
|
if ($count = (int) $day['count']) {
|
|
|
|
$totalCount += max($count, 10); // max 5 days
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 22:37:05 +00:00
|
|
|
$dayId = (int) $day['dayid'];
|
|
|
|
$drefMap[$dayId] = &$day;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-10-14 22:37:05 +00:00
|
|
|
if ($totalCount >= 50) { // should be enough
|
2022-10-29 18:05:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 22:37:05 +00:00
|
|
|
if (!$totalCount) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-10-14 22:37:05 +00:00
|
|
|
// Preload photos for these days
|
|
|
|
$details = $this->tq->getDay(
|
|
|
|
array_keys($drefMap),
|
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
|
|
|
$this->isHidden(),
|
2023-10-14 23:35:06 +00:00
|
|
|
$this->isMonthView(),
|
2023-10-29 18:19:12 +00:00
|
|
|
$this->isReverse(),
|
2023-10-14 22:37:05 +00:00
|
|
|
$this->getTransformations(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Load details into map byref
|
|
|
|
foreach ($details as $photo) {
|
|
|
|
$dayId = (int) $photo['dayid'];
|
2023-10-22 18:56:29 +00:00
|
|
|
if (!($drefMap[$dayId] ?? null)) {
|
2023-10-14 22:37:05 +00:00
|
|
|
continue;
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
2023-10-14 22:37:05 +00:00
|
|
|
|
2023-10-22 18:56:29 +00:00
|
|
|
if (!($drefMap[$dayId]['detail'] ?? null)) {
|
2023-10-14 22:37:05 +00:00
|
|
|
$drefMap[$dayId]['detail'] = [];
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
2023-10-14 22:37:05 +00:00
|
|
|
|
|
|
|
$drefMap[$dayId]['detail'][] = $photo;
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-23 23:58:49 +00:00
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function isRecursive(): bool
|
2023-03-24 00:56:41 +00:00
|
|
|
{
|
|
|
|
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function isArchive(): bool
|
2023-03-24 00:56:41 +00:00
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('archive');
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function isHidden(): bool
|
2023-10-03 00:22:05 +00:00
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('hidden');
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function noPreload(): bool
|
2023-10-03 00:54:56 +00:00
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('nopreload');
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function isMonthView(): bool
|
2023-03-24 00:56:41 +00:00
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('monthView');
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function isReverse(): bool
|
2023-03-24 00:56:41 +00:00
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('reverse');
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|