Implement routes for trash index

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
This commit is contained in:
Marcel Klehr 2022-07-05 17:50:18 +02:00
parent 80cc0c03fc
commit 2040909e39
3 changed files with 125 additions and 0 deletions

View File

@ -85,6 +85,8 @@ return [
['name' => 'internal_folders#find_shares', 'url' => '/share', 'verb' => 'GET'],
['name' => 'internal_folders#edit_share', 'url' => '/share/{shareId}', 'verb' => 'PUT'],
['name' => 'internal_folders#delete_share', 'url' => '/share/{shareId}', 'verb' => 'DELETE'],
['name' => 'trash#get_children', 'url' => '/trash/children', 'verb' => 'GET'],
['name' => 'trash#count', 'url' => '/trash/count', 'verb' => 'GET'],
// Public REST API
['name' => 'bookmark#get_bookmarks', 'url' => '/public/rest/v2/bookmark', 'verb' => 'GET'],

View File

@ -0,0 +1,76 @@
<?php
/*
* Copyright (c) 2020. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Controller;
use OCA\Bookmarks\Db\TrashMapper;
use OCA\Bookmarks\Service\Authorizer;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
class TrashController extends ApiController {
/**
* @var Authorizer
*/
private $authorizer;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
private TrashMapper $trash;
/**
* FoldersController constructor.
*
* @param $appName
* @param $request
* @param Authorizer $authorizer
* @param \Psr\Log\LoggerInterface $logger
* @param TrashMapper $trash
*/
public function __construct($appName, $request, Authorizer $authorizer, \Psr\Log\LoggerInterface $logger, TrashMapper $trash) {
parent::__construct($appName, $request);
$this->authorizer = $authorizer;
$this->logger = $logger;
$this->authorizer->setCORS(false);
$this->trash = $trash;
}
/**
* @param int $folderId
* @param int $layers
* @return JSONResponse
*
* @NoAdminRequired
*/
public function getChildren(): JSONResponse {
if (!Authorizer::hasPermission(Authorizer::PERM_READ, $this->authorizer->getPermissionsForFolder(-1, $this->request))) {
return new JSONResponse(['status' => 'error', 'data' => 'Unauthorized'], Http::STATUS_FORBIDDEN);
}
$children = $this->trash->getChildren($this->authorizer->getUserId());
$res = new JSONResponse(['status' => 'success', 'data' => $children]);
return $res;
}
/**
* @return JSONResponse
* @NoAdminRequired
* @NoCSRFRequired
*/
public function count(): JSONResponse {
if (!Authorizer::hasPermission(Authorizer::PERM_ALL, $this->authorizer->getPermissionsForFolder(-1, $this->request))) {
return new JSONResponse(['status' => 'error', 'data' => 'Unauthorized'], Http::STATUS_FORBIDDEN);
}
$count = $this->trash->countTrash($this->authorizer->getUserId());
return new JSONResponse(['status' => 'success', 'item' => $count]);
}
}

View File

@ -240,6 +240,53 @@ class TrashMapper extends QBMapper {
return $descendants;
}
/**
* @return (int|mixed|string)[][]
*
* @psalm-return array<array-key, array<string, int|mixed|string>>
*/
public function getChildren(string $userId): array {
$qb = $this->getChildrenQuery[self::TYPE_BOOKMARK];
$this->selectFromType(self::TYPE_BOOKMARK, ['t.deleted_at', 't.type'], $qb);
$qb->andWhere($qb->expr()->eq('t.user_id', $qb->createNamedParameter($userId)));
$childBookmarks = $qb->execute()->fetchAll();
$qb = $this->getChildrenQuery[self::TYPE_FOLDER];
$this->selectFromType(self::TYPE_FOLDER, ['t.deleted_at', 't.type'], $qb);
$qb->andWhere($qb->expr()->eq('t.user_id', $qb->createNamedParameter($userId)));
$childFolders = $qb->execute()->fetchAll();
$qb = $this->getChildrenQuery[self::TYPE_SHARE];
$this->selectFromType(self::TYPE_SHARE, ['t.deleted_at', 't.type'], $qb);
$qb->andWhere($qb->expr()->eq('t.user_id', $qb->createNamedParameter($userId)));
$childShares = $qb->execute()->fetchAll();
$children = array_merge($childBookmarks, $childFolders, $childShares);
$dates = array_reverse(array_column($children, 'deleted_at'));
array_multisort($dates, $children);
$bookmark = new Bookmark();
$children = array_map(function ($child) use ($bookmark) {
$item = ['type' => $child['type'], 'id' => (int)$child['id'], 'title' => $child['title'], 'userId' => $child['user_id']];
if ($item['type'] === self::TYPE_SHARE) {
$item['type'] = self::TYPE_FOLDER;
$item['id'] = (int)$child['folder_id'];
}
if ($item['type'] === self::TYPE_BOOKMARK) {
foreach (Bookmark::$columns as $col) {
$item[$bookmark->columnToProperty($col)] = $child[$col];
}
}
return $item;
}, $children);
return $children;
}
/**
* @param string $type