Skip to content

Commit 81c3038

Browse files
feat(recent-search): pass recent_limit config value to getRecentSearch function
feat(recent-search): pass recent_limit config value to getRecentSearch function Signed-off-by: Cristian Scheid <[email protected]> [skip ci]
1 parent 8bb59f3 commit 81c3038

File tree

12 files changed

+45
-5
lines changed

12 files changed

+45
-5
lines changed

apps/dav/lib/Capabilities.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ public function __construct(IConfig $config, IAvailabilityCoordinator $coordinat
3838
}
3939

4040
/**
41-
* @return array{dav: array{chunking: string, search_supports_creation_time: bool, search_supports_upload_time: bool, bulkupload?: string, absence-supported?: bool}}
41+
* @return array{dav: array{chunking: string, search_supports_creation_time: bool, search_supports_upload_time: bool, search_supports_last_activity: bool, bulkupload?: string, absence-supported?: bool}}
4242
*/
4343
public function getCapabilities() {
4444
$capabilities = [
4545
'dav' => [
4646
'chunking' => '1.0',
4747
'search_supports_creation_time' => true,
4848
'search_supports_upload_time' => true,
49+
'search_supports_last_activity' => true,
4950
]
5051
];
5152
if ($this->config->getSystemValueBool('bulkupload.enabled', true)) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class FilesPlugin extends ServerPlugin {
8585
public const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag';
8686
public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
8787
public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
88+
public const LAST_ACTIVITY_PROPERTYNAME = '{http://nextcloud.org/ns}last_activity';
8889
public const SHARE_NOTE = '{http://nextcloud.org/ns}note';
8990
public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count';
9091
public const SUBFILE_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-file-count';
@@ -401,6 +402,10 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
401402
return $node->getFileInfo()->getCreationTime();
402403
});
403404

405+
$propFind->handle(self::LAST_ACTIVITY_PROPERTYNAME, function () use ($node) {
406+
return $node->getFileInfo()->getLastActivity();
407+
});
408+
404409
foreach ($node->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) {
405410
$propFind->handle(self::FILE_METADATA_PREFIX . $metadataKey, $metadataValue);
406411
}

apps/dav/lib/Files/FileSearchBackend.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function getPropertyDefinitionsForScope(string $href, ?string $path): arr
107107
new SearchPropertyDefinition('{DAV:}getlastmodified', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
108108
new SearchPropertyDefinition('{DAV:}creationdate', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
109109
new SearchPropertyDefinition('{http://nextcloud.org/ns}upload_time', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
110+
new SearchPropertyDefinition('{http://nextcloud.org/ns}last_activity', true, false, true, SearchPropertyDefinition::DATATYPE_DATETIME),
110111
new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
111112
new SearchPropertyDefinition(TagsPlugin::FAVORITE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_BOOLEAN),
112113
new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, true, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
@@ -323,6 +324,8 @@ private function getSearchResultProperty(SearchResult $result, SearchPropertyDef
323324
return $node->getNode()->getCreationTime();
324325
case '{http://nextcloud.org/ns}upload_time':
325326
return $node->getNode()->getUploadTime();
327+
case '{http://nextcloud.org/ns}last_activity':
328+
return $node->getNode()->getLastActivity();
326329
case FilesPlugin::SIZE_PROPERTYNAME:
327330
return $node->getSize();
328331
case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
@@ -351,6 +354,8 @@ private function transformQuery(Query $query, ?SearchBinaryOperator $scopeOperat
351354
$direction = $order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING;
352355
if (str_starts_with($order->property->name, FilesPlugin::FILE_METADATA_PREFIX)) {
353356
return new SearchOrder($direction, substr($order->property->name, strlen(FilesPlugin::FILE_METADATA_PREFIX)), IMetadataQuery::EXTRA);
357+
} elseif ($order->property->name === FilesPlugin::LAST_ACTIVITY_PROPERTYNAME) {
358+
return new SearchOrder($direction, 'last_activity');
354359
} else {
355360
return new SearchOrder($direction, $this->mapPropertyNameToColumn($order->property));
356361
}

apps/dav/openapi.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"required": [
3232
"chunking",
3333
"search_supports_creation_time",
34-
"search_supports_upload_time"
34+
"search_supports_upload_time",
35+
"search_supports_last_activity"
3536
],
3637
"properties": {
3738
"chunking": {
@@ -43,6 +44,9 @@
4344
"search_supports_upload_time": {
4445
"type": "boolean"
4546
},
47+
"search_supports_last_activity": {
48+
"type": "boolean"
49+
},
4650
"bulkupload": {
4751
"type": "string"
4852
},

apps/dav/tests/unit/CapabilitiesTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testGetCapabilities(): void {
4848
'chunking' => '1.0',
4949
'search_supports_creation_time' => true,
5050
'search_supports_upload_time' => true,
51+
'search_supports_last_activity' => true,
5152
],
5253
];
5354
$this->assertSame($expected, $capabilities->getCapabilities());
@@ -69,6 +70,7 @@ public function testGetCapabilitiesWithBulkUpload(): void {
6970
'chunking' => '1.0',
7071
'search_supports_creation_time' => true,
7172
'search_supports_upload_time' => true,
73+
'search_supports_last_activity' => true,
7274
'bulkupload' => '1.0',
7375
],
7476
];
@@ -91,6 +93,7 @@ public function testGetCapabilitiesWithAbsence(): void {
9193
'chunking' => '1.0',
9294
'search_supports_creation_time' => true,
9395
'search_supports_upload_time' => true,
96+
'search_supports_last_activity' => true,
9497
'absence-supported' => true,
9598
],
9699
];

apps/files/lib/Controller/ViewController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use OCP\AppFramework\Http\RedirectResponse;
5050
use OCP\AppFramework\Http\Response;
5151
use OCP\AppFramework\Http\TemplateResponse;
52+
use OCP\AppFramework\Services\IAppConfig;
5253
use OCP\AppFramework\Services\IInitialState;
5354
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
5455
use OCP\Constants;
@@ -77,6 +78,7 @@ class ViewController extends Controller {
7778
private ITemplateManager $templateManager;
7879
private UserConfig $userConfig;
7980
private ViewConfig $viewConfig;
81+
private IAppConfig $appConfig;
8082

8183
public function __construct(string $appName,
8284
IRequest $request,
@@ -90,6 +92,7 @@ public function __construct(string $appName,
9092
ITemplateManager $templateManager,
9193
UserConfig $userConfig,
9294
ViewConfig $viewConfig,
95+
IAppConfig $appConfig,
9396
) {
9497
parent::__construct($appName, $request);
9598
$this->urlGenerator = $urlGenerator;
@@ -102,6 +105,7 @@ public function __construct(string $appName,
102105
$this->templateManager = $templateManager;
103106
$this->userConfig = $userConfig;
104107
$this->viewConfig = $viewConfig;
108+
$this->appConfig = $appConfig;
105109
}
106110

107111
/**
@@ -216,6 +220,7 @@ public function index($dir = '', $view = '', $fileid = null) {
216220
$this->initialState->provideInitialState('storageStats', $storageInfo);
217221
$this->initialState->provideInitialState('config', $this->userConfig->getConfigs());
218222
$this->initialState->provideInitialState('viewConfigs', $this->viewConfig->getConfigs());
223+
$this->initialState->provideInitialState('recent_limit', $this->appConfig->getAppValueInt('recent_limit', 100));
219224

220225
// File sorting user config
221226
$filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true);

apps/files/src/services/Recent.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { FileStat, ResponseDataDetailed, SearchResult } from 'webdav'
2525

2626
import { getCurrentUser } from '@nextcloud/auth'
2727
import { Folder, Permission, davGetRecentSearch, davGetClient, davResultToNode, davRootPath, davRemoteURL } from '@nextcloud/files'
28+
import { loadState } from '@nextcloud/initial-state'
2829
import { getBaseUrl } from '@nextcloud/router'
2930
import { CancelablePromise } from 'cancelable-promise'
3031
import { useUserConfigStore } from '../store/userconfig.ts'
@@ -33,6 +34,7 @@ import { pinia } from '../store/index.ts'
3334
const client = davGetClient()
3435

3536
const lastTwoWeeksTimestamp = Math.round((Date.now() / 1000) - (60 * 60 * 24 * 14))
37+
const recentLimit = loadState<number>('files', 'recent_limit', 100)
3638

3739
/**
3840
* Helper to map a WebDAV result to a Nextcloud node
@@ -70,7 +72,7 @@ export const getContents = async (path = '/'): Promise<ContentsWithRoot> => {
7072
try {
7173
contentsResponse = await client.search('/', {
7274
details: true,
73-
data: davGetRecentSearch(lastTwoWeeksTimestamp),
75+
data: davGetRecentSearch(lastTwoWeeksTimestamp, recentLimit),
7476
signal: abort.signal,
7577
}) as ResponseDataDetailed<SearchResult>
7678
} catch (e) {

apps/files/tests/Controller/ViewControllerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCP\AppFramework\Http\ContentSecurityPolicy;
4242
use OCP\AppFramework\Http\RedirectResponse;
4343
use OCP\AppFramework\Http\TemplateResponse;
44+
use OCP\AppFramework\Services\IAppConfig;
4445
use OCP\AppFramework\Services\IInitialState;
4546
use OCP\Diagnostics\IEventLogger;
4647
use OCP\EventDispatcher\IEventDispatcher;
@@ -82,6 +83,8 @@ class ViewControllerTest extends TestCase {
8283
private $userSession;
8384
/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
8485
private $appManager;
86+
/** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
87+
private $appConfig;
8588
/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
8689
private $rootFolder;
8790
/** @var IInitialState|\PHPUnit\Framework\MockObject\MockObject */
@@ -108,6 +111,7 @@ class ViewControllerTest extends TestCase {
108111
protected function setUp(): void {
109112
parent::setUp();
110113
$this->appManager = $this->createMock(IAppManager::class);
114+
$this->appConfig = $this->createMock(IAppConfig::class);
111115
$this->config = $this->createMock(IConfig::class);
112116
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
113117
$this->initialState = $this->createMock(IInitialState::class);
@@ -166,6 +170,7 @@ protected function setUp(): void {
166170
$this->templateManager,
167171
$this->userConfig,
168172
$this->viewConfig,
173+
$this->appConfig,
169174
])
170175
->setMethods([
171176
'getStorageInfo',

lib/private/Files/Cache/QuerySearchHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ public function searchInCaches(ISearchQuery $searchQuery, array $caches): array
175175

176176
$requestedFields = $this->searchBuilder->extractRequestedFields($searchQuery->getSearchOperation());
177177

178-
$joinExtendedCache = in_array('creation_time', $requestedFields) || in_array('upload_time', $requestedFields);
178+
$orderFields = array_map(fn ($order) => $order->getField(), $searchQuery->getOrder());
179+
180+
$joinExtendedCache = in_array('creation_time', $requestedFields)
181+
|| in_array('upload_time', $requestedFields)
182+
|| in_array('last_activity', $orderFields);
179183

180184
$query = $builder->selectFileCache('file', $joinExtendedCache);
181185

lib/private/Files/Cache/SearchBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders, ?IMe
372372
if ($field === 'mtime') {
373373
$field = $query->func()->add($field, $query->createNamedParameter(0));
374374
}
375+
376+
if ($field === 'last_activity') {
377+
$field = $query->func()->greatest('file.mtime', $query->createFunction('COALESCE(fe.upload_time, 0)'));
378+
}
375379
}
376380
$query->addOrderBy($field, $order->getDirection());
377381
}

0 commit comments

Comments
 (0)