diff --git a/apps/files/appinfo/routes.php b/apps/files/appinfo/routes.php index a98170363e926..3a4858f38d3c5 100644 --- a/apps/files/appinfo/routes.php +++ b/apps/files/appinfo/routes.php @@ -67,6 +67,11 @@ 'url' => '/api/v1/recent/', 'verb' => 'GET' ], + [ + 'name' => 'API#getFileSorting', + 'url' => '/api/v1/sorting', + 'verb' => 'GET' + ], [ 'name' => 'API#updateFileSorting', 'url' => '/api/v1/sorting', @@ -118,6 +123,7 @@ 'verb' => 'GET' ], ], + 'ocs' => [ [ 'name' => 'DirectEditing#info', diff --git a/apps/files/lib/Capabilities.php b/apps/files/lib/Capabilities.php index 29abfb5b25315..4a6e9c2694397 100644 --- a/apps/files/lib/Capabilities.php +++ b/apps/files/lib/Capabilities.php @@ -36,6 +36,9 @@ * @package OCA\Files */ class Capabilities implements ICapability { + public const SORTING_MODES = ['name', 'size', 'mtime']; + public const SORTING_DIRECTIONS = ['asc', 'desc']; + /** @var IConfig */ protected $config; @@ -70,6 +73,10 @@ public function getCapabilities() { 'directEditing' => [ 'url' => $this->urlGenerator->linkToOCSRouteAbsolute('files.DirectEditing.info'), 'etag' => $this->directEditingService->getDirectEditingETag() + ], + 'sorting' => [ + 'sorting_modes' => self::SORTING_MODES, + 'sorting_directions' => self::SORTING_DIRECTIONS ] ], ]; diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php index 45c7466e0c201..34810e15f2b59 100644 --- a/apps/files/lib/Controller/ApiController.php +++ b/apps/files/lib/Controller/ApiController.php @@ -38,6 +38,7 @@ namespace OCA\Files\Controller; use OC\Files\Node\Node; +use OCA\Files\Capabilities; use OCA\Files\Service\TagService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -267,12 +268,9 @@ public function getRecentFiles() { * @param string $mode * @param string $direction * @return Response - * @throws \OCP\PreConditionNotMetException */ - public function updateFileSorting($mode, $direction) { - $allowedMode = ['name', 'size', 'mtime']; - $allowedDirection = ['asc', 'desc']; - if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) { + public function updateFileSorting($mode, $direction): Response { + if (!in_array($mode, Capabilities::SORTING_MODES) || !in_array($direction, Capabilities::SORTING_DIRECTIONS)) { $response = new Response(); $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); return $response; @@ -282,6 +280,23 @@ public function updateFileSorting($mode, $direction) { return new Response(); } + + /** + * Get the default sort mode + * + * @NoAdminRequired + * + * @return JSONResponse + */ + public function getFileSorting(): JSONResponse { + $file_sorting = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', Capabilities::SORTING_MODES[0]); + $file_sorting_direction = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', Capabilities::SORTING_DIRECTIONS[0]); + return new JSONResponse([ + 'file_sorting' => $file_sorting, + 'file_sorting_direction' => $file_sorting_direction + ]); + } + /** * Toggle default for showing/hiding hidden files * diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php index f649453f16ed4..310d465dacc02 100644 --- a/apps/files/lib/Controller/ViewController.php +++ b/apps/files/lib/Controller/ViewController.php @@ -36,6 +36,7 @@ namespace OCA\Files\Controller; use OCA\Files\Activity\Helper; +use OCA\Files\Capabilities; use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCA\Files\Event\LoadSidebar; use OCA\Viewer\Event\LoadViewer; @@ -309,8 +310,8 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? ''; $params['isPublic'] = false; $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no'; - $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); - $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); + $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', Capabilities::SORTING_MODES[0]); + $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', Capabilities::SORTING_DIRECTIONS[0]); $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); $params['isIE'] = \OC_Util::isIe(); $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); diff --git a/apps/files/tests/Controller/ApiControllerTest.php b/apps/files/tests/Controller/ApiControllerTest.php index 0bce2763be9dd..e4e6c157de17c 100644 --- a/apps/files/tests/Controller/ApiControllerTest.php +++ b/apps/files/tests/Controller/ApiControllerTest.php @@ -27,6 +27,7 @@ */ namespace OCA\Files\Controller; +use OCA\Files\Capabilities; use OCA\Files\Service\TagService; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -211,6 +212,42 @@ public function testUpdateFileSorting() { $this->assertEquals($expected, $actual); } + public function testUpdateFileSortingWithBadParams() { + $mode = 'abcdef'; + $direction = '123456'; + + $this->config->expects($this->never()) + ->method('setUserValue') + ->with($this->user->getUID(), 'files', 'file_sorting', $mode); + $this->config->expects($this->never()) + ->method('setUserValue') + ->with($this->user->getUID(), 'files', 'file_sorting_direction', $direction); + + $actual = $this->apiController->updateFileSorting($mode, $direction); + $this->assertEquals($actual->getStatus(), Http::STATUS_UNPROCESSABLE_ENTITY); + } + + public function testGetFileSorting() { + $file_sorting = Capabilities::SORTING_MODES[0]; + $file_sorting_direction = Capabilities::SORTING_DIRECTIONS[0]; + + $this->config->expects($this->at(0)) + ->method('getUserValue') + ->with($this->user->getUID(), 'files', 'file_sorting', $file_sorting) + ->willReturn($file_sorting); + $this->config->expects($this->at(1)) + ->method('getUserValue') + ->with($this->user->getUID(), 'files', 'file_sorting_direction', $file_sorting_direction) + ->willReturn($file_sorting_direction); + + $expected = new HTTP\JSONResponse([ + 'file_sorting' => 'name', + 'file_sorting_direction' => 'asc' + ]); + $actual = $this->apiController->getFileSorting(); + $this->assertEquals($expected, $actual); + } + public function invalidSortingModeData() { return [ ['color', 'asc'],