-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRecentlyEditedFilesSource.php
More file actions
103 lines (86 loc) · 2.44 KB
/
RecentlyEditedFilesSource.php
File metadata and controls
103 lines (86 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Recommendations\Service;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IServerContainer;
use OCP\IUser;
class RecentlyEditedFilesSource implements IRecommendationSource {
public const REASON = 'recently-edited';
private IServerContainer $serverContainer;
private IL10N $l10n;
private IConfig $config;
public function __construct(IServerContainer $serverContainer,
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;
}
/**
* @return RecommendedFile[]
*/
#[\Override]
public function getMostRecentRecommendation(IUser $user, int $max): array {
/** @var IRootFolder $rootFolder */
$rootFolder = $this->serverContainer->get(IRootFolder::class);
$userFolder = $rootFolder->getUserFolder($user->getUID());
$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;
}
}
// 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;
}
}