Skip to content
Merged
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
vendor/
.idea/
.php-cs-fixer.cache
.phpunit.cache
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@
"lint": "find . -name \\*.php -not -path './vendor/*' -not -path './tests/stubs/*' -not -path './vendor-bin/*' -print0 | xargs -0 -n1 php -l",
"cs:check": "php-cs-fixer fix --dry-run --diff",
"cs:fix": "php-cs-fixer fix",
"test:unit": "echo 'Only testing installation of the app'",
"test:unit": "vendor/bin/phpunit --configuration phpunit.xml.dist",
"psalm": "psalm --threads=$(nproc) --no-cache",
"openapi": "generate-spec"
},
"autoload": {
"psr-4": {
"OCA\\Recommendations\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"OCA\\Recommendations\\Tests\\": "tests/",
"OCP\\": "vendor/nextcloud/ocp/OCP/"
}
},
"require-dev": {
"nextcloud/coding-standard": "^v1.4.0",
"nextcloud/ocp": "dev-master"
Expand Down
77 changes: 60 additions & 17 deletions lib/Service/RecentlyEditedFilesSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IServerContainer;
use OCP\IUser;
Expand All @@ -22,11 +23,28 @@ class RecentlyEditedFilesSource implements IRecommendationSource {

private IServerContainer $serverContainer;
private IL10N $l10n;
private IConfig $config;

public function __construct(IServerContainer $serverContainer,
IL10N $l10n) {
IL10N $l10n,
IConfig $config) {
$this->serverContainer = $serverContainer;
$this->l10n = $l10n;
$this->config = $config;
}

/**
* Returns true if the node's path contains a hidden component (a path
* segment starting with '.'), meaning the file itself or one of its
* ancestor directories is hidden.
*/
private function isNodeHidden(Node $node): bool {
foreach (explode('/', $node->getPath()) as $part) {
if ($part !== '' && str_starts_with($part, '.')) {
return true;
}
}
return false;
}

/**
Expand All @@ -38,23 +56,48 @@ public function getMostRecentRecommendation(IUser $user, int $max): array {
$rootFolder = $this->serverContainer->get(IRootFolder::class);
$userFolder = $rootFolder->getUserFolder($user->getUID());

return array_filter(array_map(function (Node $node) use ($userFolder): ?RecommendedFile {
try {
$parentPath = dirname($node->getPath());
if ($parentPath === '' || $parentPath === '.' || $parentPath === '/') {
$parentPath = $node->getParent()->getPath();
$showHidden = $this->config->getUserValue($user->getUID(), 'files', 'show_hidden', '0') === '1';

$results = [];
$offset = 0;

do {
$batch = $userFolder->getRecent($max, $offset);
if (empty($batch)) {
break;
}

foreach ($batch as $node) {
if (!$showHidden && $this->isNodeHidden($node)) {
continue;
}
try {
$parentPath = dirname($node->getPath());
if ($parentPath === '' || $parentPath === '.' || $parentPath === '/') {
$parentPath = $node->getParent()->getPath();
}
$results[] = new RecommendedFile(
$userFolder->getRelativePath($parentPath),
$node,
$node->getMTime(),
self::REASON,
);
} catch (StorageNotAvailableException $e) {
// skip unavailable files
}
if (count($results) >= $max) {
break;
}
return new RecommendedFile(
$userFolder->getRelativePath($parentPath),
$node,
$node->getMTime(),
self::REASON,
);
} catch (StorageNotAvailableException $e) {
return null;
}
}, $userFolder->getRecent($max)), function (?RecommendedFile $entry): bool {
return $entry !== null;
});

// If the batch was smaller than requested, there are no more items to fetch
if (count($batch) < $max) {
break;
}

$offset += $max;
} while (count($results) < $max);

return $results;
}
}
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">lib</directory>
</include>
</source>
</phpunit>
Loading
Loading