Skip to content

Commit 654547e

Browse files
feat(recent-files): add nc:last_activity property to allow sorting by max between upload_time and mtime
Signed-off-by: Cristian Scheid <cristianscheid@gmail.com>
1 parent 701902d commit 654547e

8 files changed

Lines changed: 32 additions & 4 deletions

File tree

apps/dav/lib/Capabilities.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(
2020
}
2121

2222
/**
23-
* @return array{dav: array{chunking: string, public_shares_chunking: bool, search_supports_creation_time: bool, search_supports_upload_time: bool, bulkupload?: string, absence-supported?: bool, absence-replacement?: bool}}
23+
* @return array{dav: array{chunking: string, public_shares_chunking: bool, search_supports_creation_time: bool, search_supports_upload_time: bool, search_supports_last_activity: bool, bulkupload?: string, absence-supported?: bool, absence-replacement?: bool}}
2424
*/
2525
public function getCapabilities() {
2626
$capabilities = [
@@ -29,6 +29,7 @@ public function getCapabilities() {
2929
'public_shares_chunking' => true,
3030
'search_supports_creation_time' => true,
3131
'search_supports_upload_time' => true,
32+
'search_supports_last_activity' => true,
3233
]
3334
];
3435
if ($this->config->getSystemValueBool('bulkupload.enabled', true)) {

apps/dav/lib/Connector/Sabre/FilesPlugin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class FilesPlugin extends ServerPlugin {
6767
public const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag';
6868
public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
6969
public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
70+
public const LAST_ACTIVITY_PROPERTYNAME = '{http://nextcloud.org/ns}last_activity';
7071
public const SHARE_NOTE = '{http://nextcloud.org/ns}note';
7172
public const SHARE_HIDE_DOWNLOAD_PROPERTYNAME = '{http://nextcloud.org/ns}hide-download';
7273
public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count';
@@ -441,6 +442,11 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
441442
return $node->getFileInfo()->getCreationTime();
442443
});
443444

445+
$propFind->handle(self::LAST_ACTIVITY_PROPERTYNAME, function () use ($node) {
446+
$fileInfo = $node->getFileInfo();
447+
return max($fileInfo->getUploadTime(), $fileInfo->getMTime());
448+
});
449+
444450
foreach ($node->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) {
445451
$propFind->handle(self::FILE_METADATA_PREFIX . $metadataKey, $metadataValue);
446452
}

apps/dav/lib/Files/FileSearchBackend.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function getPropertyDefinitionsForScope(string $href, ?string $path): arr
8888
new SearchPropertyDefinition('{DAV:}getlastmodified', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
8989
new SearchPropertyDefinition('{DAV:}creationdate', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
9090
new SearchPropertyDefinition('{http://nextcloud.org/ns}upload_time', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
91+
new SearchPropertyDefinition('{http://nextcloud.org/ns}last_activity', true, false, true, SearchPropertyDefinition::DATATYPE_DATETIME),
9192
new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
9293
new SearchPropertyDefinition(TagsPlugin::FAVORITE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_BOOLEAN),
9394
new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, true, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
@@ -304,6 +305,8 @@ private function getSearchResultProperty(SearchResult $result, SearchPropertyDef
304305
return $node->getNode()->getCreationTime();
305306
case '{http://nextcloud.org/ns}upload_time':
306307
return $node->getNode()->getUploadTime();
308+
case '{http://nextcloud.org/ns}last_activity':
309+
return max($node->getNode()->getUploadTime(), $node->getNode()->getMTime());
307310
case FilesPlugin::SIZE_PROPERTYNAME:
308311
return $node->getSize();
309312
case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
@@ -332,6 +335,8 @@ private function transformQuery(Query $query, ?SearchBinaryOperator $scopeOperat
332335
$direction = $order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING;
333336
if (str_starts_with($order->property->name, FilesPlugin::FILE_METADATA_PREFIX)) {
334337
return new SearchOrder($direction, substr($order->property->name, strlen(FilesPlugin::FILE_METADATA_PREFIX)), IMetadataQuery::EXTRA);
338+
} elseif ($order->property->name === FilesPlugin::LAST_ACTIVITY_PROPERTYNAME) {
339+
return new SearchOrder($direction, 'last_activity');
335340
} else {
336341
return new SearchOrder($direction, $this->mapPropertyNameToColumn($order->property));
337342
}

apps/dav/openapi.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"chunking",
3333
"public_shares_chunking",
3434
"search_supports_creation_time",
35-
"search_supports_upload_time"
35+
"search_supports_upload_time",
36+
"search_supports_last_activity"
3637
],
3738
"properties": {
3839
"chunking": {
@@ -47,6 +48,9 @@
4748
"search_supports_upload_time": {
4849
"type": "boolean"
4950
},
51+
"search_supports_last_activity": {
52+
"type": "boolean"
53+
},
5054
"bulkupload": {
5155
"type": "string"
5256
},

apps/files/src/services/Recent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function getContents(path = '/', options: { signal: AbortSignal }):
4141
const contentsResponse = await client.search('/', {
4242
signal: options.signal,
4343
details: true,
44-
data: getRecentSearch(lastTwoWeeksTimestamp, store.userConfig.recent_files_limit),
44+
data: getRecentSearch(lastTwoWeeksTimestamp, store.userConfig.recent_files_limit + 1),
4545
}) as ResponseDataDetailed<SearchResult>
4646

4747
const contents = contentsResponse.data.results

lib/private/Files/Cache/SearchBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders, ?IMe
352352
if ($field === 'mtime') {
353353
$field = $query->func()->add($field, $query->createNamedParameter(0));
354354
}
355+
356+
if ($field === 'last_activity') {
357+
$field = $query->func()->greatest('file.mtime', $query->createFunction('COALESCE(fe.upload_time, 0)'));
358+
}
355359
}
356360
$query->addOrderBy($field, $order->getDirection());
357361
}

lib/private/Files/Search/SearchOrder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ private function sortFileInfoNoDirection(FileInfo $a, FileInfo $b): int {
6060
return $a->getId() <=> $b->getId();
6161
case 'permissions':
6262
return $a->getPermissions() <=> $b->getPermissions();
63+
case 'last_activity':
64+
$timeA = max($a->getUploadTime(), $a->getMtime());
65+
$timeB = max($b->getUploadTime(), $b->getMtime());
66+
return $timeA <=> $timeB;
6367
default:
6468
return 0;
6569
}

openapi.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,8 @@
15061506
"chunking",
15071507
"public_shares_chunking",
15081508
"search_supports_creation_time",
1509-
"search_supports_upload_time"
1509+
"search_supports_upload_time",
1510+
"search_supports_last_activity"
15101511
],
15111512
"properties": {
15121513
"chunking": {
@@ -1521,6 +1522,9 @@
15211522
"search_supports_upload_time": {
15221523
"type": "boolean"
15231524
},
1525+
"search_supports_last_activity": {
1526+
"type": "boolean"
1527+
},
15241528
"bulkupload": {
15251529
"type": "string"
15261530
},

0 commit comments

Comments
 (0)