Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/files/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -118,6 +123,7 @@
'verb' => 'GET'
],
],

'ocs' => [
[
'name' => 'DirectEditing#info',
Expand Down
7 changes: 7 additions & 0 deletions apps/files/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
* @package OCA\Files
*/
class Capabilities implements ICapability {
public const SORTING_MODES = ['name', 'size', 'mtime'];
public const SORTING_DIRECTIONS = ['asc', 'desc'];
Comment on lines +39 to +40
Copy link
Member

@blizzz blizzz Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use associative indices (probably event constants) to avoid messing with strings or meaningless numerical indices in the code.



/** @var IConfig */
protected $config;
Expand Down Expand Up @@ -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
]
],
];
Expand Down
25 changes: 20 additions & 5 deletions apps/files/lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!in_array($mode, Capabilities::SORTING_MODES) || !in_array($direction, Capabilities::SORTING_DIRECTIONS)) {
if (!in_array($mode, Capabilities::SORTING_MODES, true) || !in_array($direction, Capabilities::SORTING_DIRECTIONS, true)) {

for strict comparison

$response = new Response();
$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
return $response;
Expand All @@ -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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥 if the user is not logged in. please do a null check on the result of $this->userSession->getUser()

$file_sorting_direction = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', Capabilities::SORTING_DIRECTIONS[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

return new JSONResponse([
'file_sorting' => $file_sorting,
'file_sorting_direction' => $file_sorting_direction
]);
}

/**
* Toggle default for showing/hiding hidden files
*
Expand Down
5 changes: 3 additions & 2 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions apps/files/tests/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
Expand Down