Skip to content
Merged
72 changes: 55 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,43 @@ 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;
});

$offset += $max;
} while (count($results) < $max);
Comment thread
skjnldsv marked this conversation as resolved.

return $results;
Comment thread
skjnldsv marked this conversation as resolved.
}
}
Loading