Skip to content

Commit ad0ef00

Browse files
committed
Add file sorting capabilities and sorting GET api
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent 37a2164 commit ad0ef00

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

apps/files/appinfo/routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
'url' => '/api/v1/recent/',
6868
'verb' => 'GET'
6969
],
70+
[
71+
'name' => 'API#getFileSorting',
72+
'url' => '/api/v1/sorting',
73+
'verb' => 'GET'
74+
],
7075
[
7176
'name' => 'API#updateFileSorting',
7277
'url' => '/api/v1/sorting',
@@ -118,6 +123,7 @@
118123
'verb' => 'GET'
119124
],
120125
],
126+
121127
'ocs' => [
122128
[
123129
'name' => 'DirectEditing#info',

apps/files/lib/Capabilities.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
* @package OCA\Files
3737
*/
3838
class Capabilities implements ICapability {
39+
public const SORTING_MODES = ['name', 'size', 'mtime'];
40+
public const SORTING_DIRECTIONS = ['asc', 'desc'];
41+
3942

4043
/** @var IConfig */
4144
protected $config;
@@ -70,6 +73,10 @@ public function getCapabilities() {
7073
'directEditing' => [
7174
'url' => $this->urlGenerator->linkToOCSRouteAbsolute('files.DirectEditing.info'),
7275
'etag' => $this->directEditingService->getDirectEditingETag()
76+
],
77+
'sorting' => [
78+
'sorting_modes' => self::SORTING_MODES,
79+
'sorting_directions' => self::SORTING_DIRECTIONS
7380
]
7481
],
7582
];

apps/files/lib/Controller/ApiController.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
namespace OCA\Files\Controller;
3939

4040
use OC\Files\Node\Node;
41+
use OCA\Files\Capabilities;
4142
use OCA\Files\Service\TagService;
4243
use OCP\AppFramework\Controller;
4344
use OCP\AppFramework\Http;
@@ -267,12 +268,9 @@ public function getRecentFiles() {
267268
* @param string $mode
268269
* @param string $direction
269270
* @return Response
270-
* @throws \OCP\PreConditionNotMetException
271271
*/
272-
public function updateFileSorting($mode, $direction) {
273-
$allowedMode = ['name', 'size', 'mtime'];
274-
$allowedDirection = ['asc', 'desc'];
275-
if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) {
272+
public function updateFileSorting($mode, $direction): Response {
273+
if (!in_array($mode, Capabilities::SORTING_MODES) || !in_array($direction, Capabilities::SORTING_DIRECTIONS)) {
276274
$response = new Response();
277275
$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
278276
return $response;
@@ -282,6 +280,23 @@ public function updateFileSorting($mode, $direction) {
282280
return new Response();
283281
}
284282

283+
284+
/**
285+
* Get the default sort mode
286+
*
287+
* @NoAdminRequired
288+
*
289+
* @return JSONResponse
290+
*/
291+
public function getFileSorting(): JSONResponse {
292+
$file_sorting = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', Capabilities::SORTING_MODES[0]);
293+
$file_sorting_direction = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', Capabilities::SORTING_DIRECTIONS[0]);
294+
return new JSONResponse([
295+
'file_sorting' => $file_sorting,
296+
'file_sorting_direction' => $file_sorting_direction
297+
]);
298+
}
299+
285300
/**
286301
* Toggle default for showing/hiding hidden files
287302
*

apps/files/lib/Controller/ViewController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
namespace OCA\Files\Controller;
3737

3838
use OCA\Files\Activity\Helper;
39+
use OCA\Files\Capabilities;
3940
use OCA\Files\Event\LoadAdditionalScriptsEvent;
4041
use OCA\Files\Event\LoadSidebar;
4142
use OCA\Viewer\Event\LoadViewer;
@@ -309,8 +310,8 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
309310
$params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? '';
310311
$params['isPublic'] = false;
311312
$params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no';
312-
$params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
313-
$params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
313+
$params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', Capabilities::SORTING_MODES[0]);
314+
$params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', Capabilities::SORTING_DIRECTIONS[0]);
314315
$params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false);
315316
$params['isIE'] = \OC_Util::isIe();
316317
$showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);

apps/files/tests/Controller/ApiControllerTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
namespace OCA\Files\Controller;
2929

30+
use OCA\Files\Capabilities;
3031
use OCA\Files\Service\TagService;
3132
use OCP\AppFramework\Http;
3233
use OCP\AppFramework\Http\DataResponse;
@@ -211,6 +212,42 @@ public function testUpdateFileSorting() {
211212
$this->assertEquals($expected, $actual);
212213
}
213214

215+
public function testUpdateFileSortingWithBadParams() {
216+
$mode = 'abcdef';
217+
$direction = '123456';
218+
219+
$this->config->expects($this->never())
220+
->method('setUserValue')
221+
->with($this->user->getUID(), 'files', 'file_sorting', $mode);
222+
$this->config->expects($this->never())
223+
->method('setUserValue')
224+
->with($this->user->getUID(), 'files', 'file_sorting_direction', $direction);
225+
226+
$actual = $this->apiController->updateFileSorting($mode, $direction);
227+
$this->assertEquals($actual->getStatus(), Http::STATUS_UNPROCESSABLE_ENTITY);
228+
}
229+
230+
public function testGetFileSorting() {
231+
$file_sorting = Capabilities::SORTING_MODES[0];
232+
$file_sorting_direction = Capabilities::SORTING_DIRECTIONS[0];
233+
234+
$this->config->expects($this->at(0))
235+
->method('getUserValue')
236+
->with($this->user->getUID(), 'files', 'file_sorting', $file_sorting)
237+
->willReturn($file_sorting);
238+
$this->config->expects($this->at(1))
239+
->method('getUserValue')
240+
->with($this->user->getUID(), 'files', 'file_sorting_direction', $file_sorting_direction)
241+
->willReturn($file_sorting_direction);
242+
243+
$expected = new HTTP\JSONResponse([
244+
'file_sorting' => 'name',
245+
'file_sorting_direction' => 'asc'
246+
]);
247+
$actual = $this->apiController->getFileSorting();
248+
$this->assertEquals($expected, $actual);
249+
}
250+
214251
public function invalidSortingModeData() {
215252
return [
216253
['color', 'asc'],

0 commit comments

Comments
 (0)