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;
|
|
|
|
|
2022-11-16 07:45:01 +00:00
|
|
|
use OCA\Memories\Db\TimelineRoot;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
|
|
|
|
class DaysController extends ApiBase
|
|
|
|
{
|
2022-11-07 04:48:10 +00:00
|
|
|
use FoldersTrait;
|
|
|
|
|
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(): JSONResponse
|
|
|
|
{
|
|
|
|
// Get the folder to show
|
2023-01-18 03:02:00 +00:00
|
|
|
try {
|
|
|
|
$uid = $this->getUID();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
// Get the folder to show
|
2022-11-16 07:45:01 +00:00
|
|
|
$root = null;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
try {
|
2022-11-16 07:45:01 +00:00
|
|
|
$root = $this->getRequestRoot();
|
2022-10-29 18:05:05 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
try {
|
|
|
|
$list = $this->timelineQuery->getDays(
|
2022-11-16 07:45:01 +00:00
|
|
|
$root,
|
2022-10-29 18:05:05 +00:00
|
|
|
$uid,
|
2022-11-03 22:39:48 +00:00
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
2022-10-29 18:05:05 +00:00
|
|
|
$this->getTransformations(true),
|
|
|
|
);
|
|
|
|
|
2022-11-03 22:39:48 +00:00
|
|
|
if ($this->isMonthView()) {
|
|
|
|
// Group days together into months
|
|
|
|
$list = $this->timelineQuery->daysToMonths($list);
|
|
|
|
} else {
|
|
|
|
// Preload some day responses
|
2022-11-16 07:45:01 +00:00
|
|
|
$this->preloadDays($list, $uid, $root);
|
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
|
|
|
// Add subfolder info if querying non-recursively
|
2022-11-03 22:39:48 +00:00
|
|
|
if (!$this->isRecursive()) {
|
2022-11-16 07:45:01 +00:00
|
|
|
array_unshift($list, $this->getSubfoldersEntry($root->getFolder($root->getOneId())));
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
|
|
|
public function day(string $id): JSONResponse
|
|
|
|
{
|
|
|
|
// Get user
|
2022-12-04 17:22:59 +00:00
|
|
|
$uid = $this->getUID();
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
// Check for wildcard
|
2022-11-03 22:39:48 +00:00
|
|
|
$dayIds = [];
|
2022-10-29 18:05:05 +00:00
|
|
|
if ('*' === $id) {
|
2022-11-03 22:39:48 +00:00
|
|
|
$dayIds = null;
|
2022-10-29 18:05:05 +00:00
|
|
|
} else {
|
|
|
|
// Split at commas and convert all parts to int
|
2022-11-03 22:39:48 +00:00
|
|
|
$dayIds = array_map(function ($part) {
|
2022-10-29 18:05:05 +00:00
|
|
|
return (int) $part;
|
|
|
|
}, explode(',', $id));
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:39:48 +00:00
|
|
|
// Check if $dayIds is empty
|
|
|
|
if (null !== $dayIds && 0 === \count($dayIds)) {
|
2022-10-29 18:05:05 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the folder to show
|
2022-11-16 07:45:01 +00:00
|
|
|
$root = null;
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
try {
|
2022-11-16 07:45:01 +00:00
|
|
|
$root = $this->getRequestRoot();
|
2022-10-29 18:05:05 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:39:48 +00:00
|
|
|
// Convert to actual dayIds if month view
|
|
|
|
if ($this->isMonthView()) {
|
2022-12-04 17:40:58 +00:00
|
|
|
$dayIds = $this->timelineQuery->monthIdToDayIds((int) $dayIds[0]);
|
2022-11-03 22:39:48 +00:00
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
try {
|
|
|
|
$list = $this->timelineQuery->getDay(
|
2022-11-16 07:45:01 +00:00
|
|
|
$root,
|
2022-10-29 18:05:05 +00:00
|
|
|
$uid,
|
2022-11-03 22:39:48 +00:00
|
|
|
$dayIds,
|
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
2022-10-29 18:05:05 +00:00
|
|
|
$this->getTransformations(false),
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:09:38 +00:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*/
|
|
|
|
public function dayPost(): JSONResponse
|
|
|
|
{
|
|
|
|
$id = $this->request->getParam('body_ids');
|
|
|
|
if (null === $id) {
|
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->day($id);
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
/**
|
|
|
|
* Preload a few "day" at the start of "days" response.
|
|
|
|
*
|
2022-11-16 07:45:01 +00:00
|
|
|
* @param array $days the days array
|
|
|
|
* @param string $uid User ID or blank for public shares
|
|
|
|
* @param TimelineRoot $root the root folder
|
2022-10-29 18:05:05 +00:00
|
|
|
*/
|
2022-11-16 07:45:01 +00:00
|
|
|
private function preloadDays(array &$days, string $uid, TimelineRoot &$root)
|
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(
|
2022-11-16 07:45:01 +00:00
|
|
|
$root,
|
2022-10-29 18:05:05 +00:00
|
|
|
$uid,
|
|
|
|
$preloadDayIds,
|
2022-11-03 22:39:48 +00:00
|
|
|
$this->isRecursive(),
|
|
|
|
$this->isArchive(),
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|