Skip to content

Commit

Permalink
Exclude hidden files from recent recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Kleinschmidt committed Jan 13, 2021
1 parent 1aaf809 commit 7225783
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions lib/Service/RecentlyEditedFilesSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\Recommendations\Service;

use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\StorageNotAvailableException;
use OCP\IL10N;
use OCP\IServerContainer;
Expand All @@ -45,26 +46,61 @@ public function __construct(IServerContainer $serverContainer,
$this->l10n = $l10n;
}

/**
* @return bool
*/
private function isNodeExcluded(Node $node, bool $showHidden): bool {
$next = $node;

while (!$showHidden) {
if ($next->getName()[0] == ".")
return true;

try {
$next = $next->getParent();
} catch (NotFoundException $e) {
break;
}
}

return false;
}

/**
* @return array
*/
public function getMostRecentRecommendation(IUser $user, int $max): array {
$showHidden = (bool) $this->serverContainer->getConfig()->getUserValue($user->getUID(), 'files', 'show_hidden', false);
$userFolder = $this->serverContainer->getUserFolder($user->getUID());

return array_filter(array_map(function (Node $node) use ($userFolder) {
try {
return new RecommendedFile(
$userFolder->getRelativePath($node->getParent()->getPath()),
$node,
$node->getMTime(),
$this->l10n->t("Recently edited")
);
} catch (StorageNotAvailableException $e) {
return null;
$count = 0;
$limit = 2 * $max;
$offset = 0;
$results = [];

do {
$recentFiles = $userFolder->getRecent($limit, $offset);

foreach ($recentFiles as $node) {
if (!$this->isNodeExcluded($node, $showHidden)) {
try {
$results[] = new RecommendedFile(
$userFolder->getRelativePath($node->getParent()->getPath()),
$node,
$node->getMTime(),
$this->l10n->t("Recently edited")
);
} catch (StorageNotAvailableException $e) {
//pass
}
}
}
}, $userFolder->getRecent($max)), function ($entry) {
return $entry !== null;
});

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

return array_slice($results, 0, $max);
}

}

0 comments on commit 7225783

Please sign in to comment.