Skip to content
Draft
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
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
'OCA\\DAV\\Connector\\Sabre\\File' => $baseDir . '/../lib/Connector/Sabre/File.php',
'OCA\\DAV\\Connector\\Sabre\\FilesPlugin' => $baseDir . '/../lib/Connector/Sabre/FilesPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\FilesReportPlugin' => $baseDir . '/../lib/Connector/Sabre/FilesReportPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\GroupableFile' => $baseDir . '/../lib/Connector/Sabre/GroupableFile.php',
'OCA\\DAV\\Connector\\Sabre\\LockPlugin' => $baseDir . '/../lib/Connector/Sabre/LockPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MaintenancePlugin' => $baseDir . '/../lib/Connector/Sabre/MaintenancePlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MtimeSanitizer' => $baseDir . '/../lib/Connector/Sabre/MtimeSanitizer.php',
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Connector\\Sabre\\File' => __DIR__ . '/..' . '/../lib/Connector/Sabre/File.php',
'OCA\\DAV\\Connector\\Sabre\\FilesPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/FilesPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\FilesReportPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/FilesReportPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\GroupableFile' => __DIR__ . '/..' . '/../lib/Connector/Sabre/GroupableFile.php',
'OCA\\DAV\\Connector\\Sabre\\LockPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/LockPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MaintenancePlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/MaintenancePlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MtimeSanitizer' => __DIR__ . '/..' . '/../lib/Connector/Sabre/MtimeSanitizer.php',
Expand Down
5 changes: 5 additions & 0 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class FilesPlugin extends ServerPlugin {
public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
public const LAST_ACTIVITY_PROPERTYNAME = '{http://nextcloud.org/ns}last_activity';
public const MIME_TYPE_GROUP = '{http://nextcloud.org/ns}mime_type_group';
public const SHARE_NOTE = '{http://nextcloud.org/ns}note';
public const SHARE_HIDE_DOWNLOAD_PROPERTYNAME = '{http://nextcloud.org/ns}hide-download';
public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count';
Expand Down Expand Up @@ -453,6 +454,10 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
return $node->getFileInfo()->getLastActivity();
});

$propFind->handle(self::MIME_TYPE_GROUP, function () use ($node) {
return $node instanceof GroupableFile ? $node->getGroup() : 0;
});

foreach ($node->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) {
$propFind->handle(self::FILE_METADATA_PREFIX . $metadataKey, fn () => $metadataValue);
}
Expand Down
39 changes: 39 additions & 0 deletions apps/dav/lib/Connector/Sabre/GroupableFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\DAV\Connector\Sabre;

use OC\Files\View;
use OCA\DAV\Connector\Sabre\File;
use OCP\Files\FileInfo;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Share\IManager;

class GroupableFile extends File {

public function __construct(
View $view,
FileInfo $info,
?IManager $shareManager = null,
?IRequest $request = null,
?IL10N $l10n = null,
protected int $group = 0,
) {
parent::__construct($view, $info, $shareManager, $request, $l10n);
}

public function getGroup(): int
{
return $this->group;
}

public function setGroup(int $group): void
{
$this->group = $group;
}
}
115 changes: 114 additions & 1 deletion apps/dav/lib/Files/FileSearchBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\View;
use OCA\Files\AppInfo\Application;
use OCA\Files\ConfigLexicon;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCA\DAV\Connector\Sabre\GroupableFile;
use OCA\DAV\Connector\Sabre\Server;
use OCA\DAV\Connector\Sabre\TagsPlugin;
use OCP\Files\Cache\ICacheEntry;
Expand All @@ -31,6 +34,7 @@
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\FilesMetadata\IMetadataQuery;
use OCP\FilesMetadata\Model\IMetadataValueWrapper;
use OCP\IAppConfig;
use OCP\IUser;
use OCP\Share\IManager;
use Sabre\DAV\Exception\NotFound;
Expand All @@ -54,6 +58,7 @@
private IManager $shareManager,
private View $view,
private IFilesMetadataManager $filesMetadataManager,
private IAppConfig $appConfig,
) {
}

Expand Down Expand Up @@ -216,10 +221,14 @@
$results = $userFolder->search($query);
}

$groupRecentFilesEnabled = $this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::GROUP_RECENT_FILES, false);

/** @var SearchResult[] $nodes */
$nodes = array_map(function (Node $node) {
$nodes = array_map(function (Node $node) use ($groupRecentFilesEnabled) {
if ($node instanceof Folder) {
$davNode = new Directory($this->view, $node, $this->tree, $this->shareManager);
} elseif ($groupRecentFilesEnabled) {
$davNode = new GroupableFile($this->view, $node, $this->shareManager);
} else {
$davNode = new File($this->view, $node, $this->shareManager);
}
Expand All @@ -228,6 +237,10 @@
return new SearchResult($davNode, $path);
}, $results);

if ($groupRecentFilesEnabled) {
$nodes = $this->setGroupOnNodes($nodes);
}

if (!$query->limitToHome()) {
// Sort again, since the result from multiple storages is appended and not sorted
usort($nodes, function (SearchResult $a, SearchResult $b) use ($search) {
Expand Down Expand Up @@ -572,4 +585,104 @@
return null;
}
}

/**
* @param SearchResult[] $searchResults
* @return SearchResult[] $searchResults
*/
private function setGroupOnNodes(array $searchResults): array
{
$mimeTypes = $this->appConfig->getValueArray(Application::APP_ID, ConfigLexicon::RECENT_FILES_GROUP_MIME_TYPES, []);
if (count($mimeTypes) === 0) {
return $searchResults;
}
$timespanMinutes = $this->appConfig->getValueInt(Application::APP_ID, ConfigLexicon::RECENT_FILES_GROUP_TIMESPAN_MINUTES, 2);
$timespan = $timespanMinutes * 60;

// sort by most most recent action to the oldest
usort($searchResults, fn($a, $b) => $this->getNodeTime($b) <=> $this->getNodeTime($a));

$count = count($searchResults);
$result = [];
$groupNumber = 1;
$i = 0;

while ($i < $count) {
$current = $searchResults[$i];

if (!$this->isNodeGroupable($current, $mimeTypes)) {
$result[] = $current;
$i++;
continue;
}

$groupStartTime = $this->getNodeTime($current);
$isContaminated = false;

// look ahead to check if the time window is contaminated by a non-groupable node
for ($j = $i + 1; $j < $count; $j++) {
$nextTime = $this->getNodeTime($searchResults[$j]);
if (abs($nextTime - $groupStartTime) > $timespan) {
break;
}
if (!$this->isNodeGroupable($searchResults[$j], $mimeTypes)) {
$isContaminated = true;
break;
}
}

if ($isContaminated) {
$result[] = $current;
$i++;
continue;
}

$groupIndexes = [$i];
$i++;

// add nodes to group until time window limit is reached
while ($i < $count) {
$next = $searchResults[$i];
$nextTime = $this->getNodeTime($next);

if (abs($nextTime - $groupStartTime) > $timespan) {
break;
}

$groupIndexes[] = $i;
$i++;
}

if (count($groupIndexes) === 1) {
$result[] = $searchResults[$groupIndexes[0]];
continue;
}

foreach ($groupIndexes as $idx) {
$searchResults[$idx]->node->setGroup($groupNumber);

Check failure on line 662 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/dav/lib/Files/FileSearchBackend.php:662:33: UndefinedInterfaceMethod: Method Sabre\DAV\INode::setGroup does not exist (see https://psalm.dev/181)
$result[] = $searchResults[$idx];
}
$groupNumber++;
}

return $result;
}

private function getNodeTime(SearchResult $result): int
{
$node = $result->node;
if (!$node instanceof GroupableFile) {
return 0;
}
$uploadTime = $node->getNode()->getUploadTime() ?? 0;

Check failure on line 677 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TypeDoesNotContainNull

apps/dav/lib/Files/FileSearchBackend.php:677:54: TypeDoesNotContainNull: Cannot resolve types for $<tmp coalesce var>22194 - int does not contain null (see https://psalm.dev/090)

Check failure on line 677 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCondition

apps/dav/lib/Files/FileSearchBackend.php:677:17: RedundantCondition: Type int for $<tmp coalesce var>22194 is never null (see https://psalm.dev/122)
$crtime = $node->getNode()->getCreationTime() ?? 0;

Check failure on line 678 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TypeDoesNotContainNull

apps/dav/lib/Files/FileSearchBackend.php:678:52: TypeDoesNotContainNull: Cannot resolve types for $<tmp coalesce var>22246 - int does not contain null (see https://psalm.dev/090)

Check failure on line 678 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCondition

apps/dav/lib/Files/FileSearchBackend.php:678:13: RedundantCondition: Type int for $<tmp coalesce var>22246 is never null (see https://psalm.dev/122)
$mtime = $node->getLastModified() ?? 0;

Check failure on line 679 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TypeDoesNotContainNull

apps/dav/lib/Files/FileSearchBackend.php:679:40: TypeDoesNotContainNull: Cannot resolve types for $<tmp coalesce var>22299 - int does not contain null (see https://psalm.dev/090)

Check failure on line 679 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCondition

apps/dav/lib/Files/FileSearchBackend.php:679:12: RedundantCondition: Type int for $<tmp coalesce var>22299 is never null (see https://psalm.dev/122)
return max((int)$uploadTime, (int)$crtime, (int)$mtime);

Check failure on line 680 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCast

apps/dav/lib/Files/FileSearchBackend.php:680:46: RedundantCast: Redundant cast to int (see https://psalm.dev/262)

Check failure on line 680 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCast

apps/dav/lib/Files/FileSearchBackend.php:680:32: RedundantCast: Redundant cast to int (see https://psalm.dev/262)

Check failure on line 680 in apps/dav/lib/Files/FileSearchBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCast

apps/dav/lib/Files/FileSearchBackend.php:680:14: RedundantCast: Redundant cast to int (see https://psalm.dev/262)
}

private function isNodeGroupable(SearchResult $result, array $mimeTypes): bool
{
$node = $result->node;
return $node instanceof GroupableFile && in_array($node->getNode()->getMimetype(), $mimeTypes, true);
}
}
3 changes: 2 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ public function __construct(
\OCP\Server::get(IRootFolder::class),
$shareManager,
$view,
\OCP\Server::get(IFilesMetadataManager::class)
\OCP\Server::get(IFilesMetadataManager::class),
\OCP\Server::get(IAppConfig::class),
));
$this->server->addPlugin(
new BulkUploadPlugin(
Expand Down
4 changes: 3 additions & 1 deletion apps/dav/tests/unit/Files/FileSearchBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCA\DAV\Connector\Sabre\ObjectTree;
use OCA\DAV\Connector\Sabre\Server;
use OCA\DAV\Files\FileSearchBackend;
use OCP\IAppConfig;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
Expand Down Expand Up @@ -80,8 +81,9 @@ protected function setUp(): void {
->willReturn($this->searchFolder);

$filesMetadataManager = $this->createMock(IFilesMetadataManager::class);
$appConfig = $this->createMock(IAppConfig::class);

$this->search = new FileSearchBackend($this->server, $this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
$this->search = new FileSearchBackend($this->server, $this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager, $appConfig);
}

public function testSearchFilename(): void {
Expand Down
Loading