2023-02-05 21:43:25 +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-18 17:52:04 +00:00
|
|
|
use OCA\Memories\Errors;
|
2023-02-05 21:43:25 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class PlacesController extends GenericApiController
|
2023-02-05 21:43:25 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get list of places with counts of images
|
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function places(): Http\Response
|
2023-02-05 21:43:25 +00:00
|
|
|
{
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (null === $user) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotLoggedIn();
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check tags enabled for this user
|
2023-02-06 03:46:44 +00:00
|
|
|
if (!$this->placesIsEnabled()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotEnabled('places');
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't the timeline folder then things aren't going to work
|
|
|
|
$root = $this->getRequestRoot();
|
|
|
|
if ($root->isEmpty()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NoRequestRoot();
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
$list = $this->timelineQuery->getPlaces($root);
|
|
|
|
|
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Get preview for a location
|
|
|
|
*/
|
|
|
|
public function preview(int $id): Http\Response
|
|
|
|
{
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (null === $user) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotLoggedIn();
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check tags enabled for this user
|
2023-02-06 03:46:44 +00:00
|
|
|
if (!$this->placesIsEnabled()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotEnabled('places');
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't the timeline folder then things aren't going to work
|
|
|
|
$root = $this->getRequestRoot();
|
|
|
|
if ($root->isEmpty()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NoRequestRoot();
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
$list = $this->timelineQuery->getPlacePreviews($id, $root);
|
|
|
|
if (null === $list || 0 === \count($list)) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotFound('previews');
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
shuffle($list);
|
|
|
|
|
|
|
|
// Get preview from image list
|
2023-02-24 07:50:09 +00:00
|
|
|
return $this->getPreviewFromImageList(array_map(function ($item) {
|
2023-02-08 02:52:43 +00:00
|
|
|
return (int) $item['fileid'];
|
2023-02-05 21:43:25 +00:00
|
|
|
}, $list));
|
|
|
|
}
|
|
|
|
}
|