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\Exceptions;
|
|
|
|
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 () {
|
2022-10-29 18:05:05 +00:00
|
|
|
$list = $this->timelineQuery->getDays(
|
2022-11-03 22:39:48 +00:00
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->getTransformations(),
|
2022-10-29 18:05:05 +00:00
|
|
|
);
|
|
|
|
|
2022-11-03 22:39:48 +00:00
|
|
|
if ($this->isMonthView()) {
|
|
|
|
// Group days together into months
|
2023-03-23 23:58:49 +00:00
|
|
|
$list = $this->daysToMonths($list);
|
2022-11-03 22:39:48 +00:00
|
|
|
} else {
|
|
|
|
// Preload some day responses
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->preloadDays($list);
|
2022-11-03 22:39:48 +00:00
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2022-11-03 22:44:52 +00:00
|
|
|
// Reverse response if requested. Folders still stay at top.
|
|
|
|
if ($this->isReverse()) {
|
|
|
|
$list = array_reverse($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-03-19 03:38:37 +00:00
|
|
|
public function day(string $id): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($id) {
|
|
|
|
// Check for wildcard
|
|
|
|
$dayIds = [];
|
|
|
|
if ('*' === $id) {
|
|
|
|
$dayIds = null;
|
|
|
|
} else {
|
|
|
|
// Split at commas and convert all parts to int
|
2023-08-30 18:10:08 +00:00
|
|
|
$dayIds = array_map(static fn ($p) => (int) $p, explode(',', $id));
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Check if $dayIds is empty
|
|
|
|
if (null !== $dayIds && 0 === \count($dayIds)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Convert to actual dayIds if month view
|
|
|
|
if ($this->isMonthView()) {
|
2023-03-23 23:58:49 +00:00
|
|
|
$dayIds = $this->monthIdToDayIds((int) $dayIds[0]);
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Run actual query
|
2022-10-29 18:05:05 +00:00
|
|
|
$list = $this->timelineQuery->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-03-23 23:58:49 +00:00
|
|
|
$this->getTransformations(),
|
2022-10-29 18:05:05 +00:00
|
|
|
);
|
|
|
|
|
2022-11-03 22:39:48 +00:00
|
|
|
// Force month id for dayId for month view
|
|
|
|
if ($this->isMonthView()) {
|
|
|
|
foreach ($list as &$photo) {
|
|
|
|
$photo['dayid'] = (int) $dayIds[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:44:52 +00:00
|
|
|
// Reverse response if requested.
|
|
|
|
if ($this->isReverse()) {
|
|
|
|
$list = array_reverse($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
|
|
|
}
|
|
|
|
|
2022-10-29 18:09:38 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function dayPost(): Http\Response
|
2022-10-29 18:09:38 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () {
|
|
|
|
$id = $this->request->getParam('body_ids');
|
|
|
|
if (null === $id) {
|
|
|
|
throw Exceptions::MissingParameter('body_ids');
|
|
|
|
}
|
2022-10-29 18:09:38 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return $this->day($id);
|
|
|
|
});
|
2022-10-29 18:09:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 21:53:38 +00:00
|
|
|
/**
|
|
|
|
* Get transformations depending on the request.
|
|
|
|
*/
|
2023-03-23 23:58:49 +00:00
|
|
|
private function getTransformations()
|
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')) {
|
|
|
|
$transforms[] = [$this->timelineQuery, 'transformFavoriteFilter'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter only videos
|
|
|
|
if ($this->request->getParam('vid')) {
|
|
|
|
$transforms[] = [$this->timelineQuery, 'transformVideoFilter'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter geological bounds
|
2023-03-23 23:58:49 +00:00
|
|
|
if ($bounds = $this->request->getParam('mapbounds')) {
|
2023-02-08 21:53:38 +00:00
|
|
|
$transforms[] = [$this->timelineQuery, 'transformMapBoundsFilter', $bounds];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit number of responses for day query
|
2023-03-23 23:58:49 +00:00
|
|
|
if ($limit = $this->request->getParam('limit')) {
|
|
|
|
$transforms[] = [$this->timelineQuery, '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()) {
|
|
|
|
$transforms[] = [$this->timelineQuery, '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.
|
|
|
|
*
|
2023-03-23 23:58:49 +00:00
|
|
|
* @param array $days the days array
|
2022-10-29 18:05:05 +00:00
|
|
|
*/
|
2023-03-23 23:58:49 +00:00
|
|
|
private function preloadDays(array &$days)
|
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.
|
|
|
|
if (Util::callerIsNative()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build identical transforms for sub queries
|
2022-10-29 18:05:05 +00:00
|
|
|
$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->getDay(
|
|
|
|
$preloadDayIds,
|
2022-11-03 22:39:48 +00:00
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
2023-10-03 00:22:05 +00:00
|
|
|
$this->isHidden(),
|
2022-10-29 18:05:05 +00:00
|
|
|
$transforms,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert days response to months response.
|
|
|
|
* The dayId is used to group the days into months.
|
|
|
|
*/
|
|
|
|
private function daysToMonths(array $days)
|
|
|
|
{
|
|
|
|
$months = [];
|
|
|
|
foreach ($days as $day) {
|
|
|
|
$dayId = $day['dayid'];
|
|
|
|
$time = $dayId * 86400;
|
|
|
|
$monthid = strtotime(date('Ym', $time).'01') / 86400;
|
|
|
|
|
|
|
|
if (empty($months) || $months[\count($months) - 1]['dayid'] !== $monthid) {
|
|
|
|
$months[] = [
|
|
|
|
'dayid' => $monthid,
|
|
|
|
'count' => 0,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$months[\count($months) - 1]['count'] += $day['count'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $months;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert list of month IDs to list of dayIds */
|
|
|
|
private function monthIdToDayIds(int $monthId)
|
|
|
|
{
|
|
|
|
$dayIds = [];
|
|
|
|
$firstDay = (int) $monthId;
|
|
|
|
$lastDay = strtotime(date('Ymt', $firstDay * 86400)) / 86400;
|
|
|
|
for ($i = $firstDay; $i <= $lastDay; ++$i) {
|
|
|
|
$dayIds[] = (string) $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dayIds;
|
|
|
|
}
|
2023-03-24 00:56:41 +00:00
|
|
|
|
|
|
|
private function isRecursive()
|
|
|
|
{
|
|
|
|
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isArchive()
|
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('archive');
|
|
|
|
}
|
|
|
|
|
2023-10-03 00:22:05 +00:00
|
|
|
private function isHidden()
|
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('hidden');
|
|
|
|
}
|
|
|
|
|
2023-03-24 00:56:41 +00:00
|
|
|
private function isMonthView()
|
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('monthView');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isReverse()
|
|
|
|
{
|
|
|
|
return null !== $this->request->getParam('reverse');
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|