Skip to content

Commit a697671

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

4 files changed

Lines changed: 37 additions & 8 deletions

File tree

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
*/
3838
class Capabilities implements ICapability {
3939

40+
const SORTING_MODES = ['name', 'size', 'mtime'];
41+
const SORTING_DIRECTIONS = ['asc', 'desc'];
42+
43+
4044
/** @var IConfig */
4145
protected $config;
4246

@@ -70,6 +74,10 @@ public function getCapabilities() {
7074
'directEditing' => [
7175
'url' => $this->urlGenerator->linkToOCSRouteAbsolute('files.DirectEditing.info'),
7276
'etag' => $this->directEditingService->getDirectEditingETag()
77+
],
78+
'sorting' => [
79+
'sorting_modes' => self::SORTING_MODES,
80+
'sorting_directions' => self::SORTING_DIRECTIONS
7381
]
7482
],
7583
];

apps/files/lib/Controller/ApiController.php

Lines changed: 20 additions & 6 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;
@@ -54,7 +55,6 @@
5455
use OCP\IUserSession;
5556
use OCP\Share\IManager;
5657
use OCP\Share\IShare;
57-
5858
/**
5959
* Class ApiController
6060
*
@@ -267,12 +267,9 @@ public function getRecentFiles() {
267267
* @param string $mode
268268
* @param string $direction
269269
* @return Response
270-
* @throws \OCP\PreConditionNotMetException
271270
*/
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)) {
271+
public function updateFileSorting($mode, $direction): Response {
272+
if (!in_array($mode, Capabilities::SORTING_MODES) || !in_array($direction, Capabilities::SORTING_DIRECTIONS)) {
276273
$response = new Response();
277274
$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
278275
return $response;
@@ -282,6 +279,23 @@ public function updateFileSorting($mode, $direction) {
282279
return new Response();
283280
}
284281

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

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);

0 commit comments

Comments
 (0)