Skip to content

Commit 238b4c8

Browse files
Carl SchwanCarlSchwan
authored andcommitted
fix(performance): Avoid checking existence of default notes folder 3 times
In the case where the user didn't change their default notes folder. In the worst case, if the user did change their default notes folder we keep the same amount of queries as before. Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent dd96e04 commit 238b4c8

2 files changed

Lines changed: 55 additions & 11 deletions

File tree

lib/Service/NoteUtil.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCP\Files\Folder;
1414
use OCP\Files\IRootFolder;
1515
use OCP\Files\Node;
16+
use OCP\Files\NotFoundException;
1617
use OCP\IDBConnection;
1718
use OCP\IUserSession;
1819
use OCP\Share\IManager;
@@ -196,12 +197,20 @@ public function getNotesFolderUserPath(string $userId, bool $saveInitial = false
196197
public function getOrCreateNotesFolder(string $userId, bool $create = true) : Folder {
197198
$userFolder = $this->getRoot()->getUserFolder($userId);
198199
$notesPath = $this->settingsService->get($userId, 'notesPath');
199-
$allowShared = $notesPath !== $this->settingsService->getDefaultNotesPath($userId);
200200

201-
$folder = null;
201+
['path' => $defaultPath, 'node' => $folder] = $this->settingsService->getDefaultNotesNode($userId);
202+
$allowShared = $notesPath !== $defaultPath;
203+
204+
if ($allowShared) {
205+
try {
206+
$folder = $userFolder->get($notesPath);
207+
} catch (NotFoundException) {
208+
$folder = null;
209+
}
210+
}
211+
202212
$updateNotesPath = false;
203-
if ($userFolder->nodeExists($notesPath)) {
204-
$folder = $userFolder->get($notesPath);
213+
if ($folder instanceof Folder) {
205214
if (!$allowShared && $folder->isShared()) {
206215
$notesPath = $userFolder->getNonExistingName($notesPath);
207216
$folder = $userFolder->newFolder($notesPath);

lib/Service/SettingsService.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use OCA\Notes\AppInfo\Application;
1313

1414
use OCP\App\IAppManager;
15+
use OCP\Files\Folder;
1516
use OCP\Files\IRootFolder;
17+
use OCP\Files\NotFoundException;
1618
use OCP\IConfig;
1719
use OCP\IL10N;
1820

@@ -41,7 +43,7 @@ public function __construct(
4143
'fileSuffix' => $this->getListAttrs('fileSuffix', [...$this->defaultSuffixes, 'custom']),
4244
'notesPath' => [
4345
'default' => function (string $uid) {
44-
return $this->getDefaultNotesPath($uid);
46+
return $this->getDefaultNotesNode($uid)['path'];
4547
},
4648
'validate' => function ($value) {
4749
$value = str_replace([ '/', '\\' ], DIRECTORY_SEPARATOR, $value);
@@ -86,13 +88,46 @@ private function getListAttrs(string $attributeName, array $values) : array {
8688
];
8789
}
8890

89-
public function getDefaultNotesPath(string $uid) : string {
91+
/**
92+
* Return the default notes node if it exists and the expected path if it exists
93+
* @return array{
94+
* path: string,
95+
* folder: ?Folder
96+
* }
97+
*/
98+
public function getDefaultNotesNode(string $uid): array {
9099
$defaultFolder = $this->config->getAppValue(Application::APP_ID, 'defaultFolder', 'Notes');
91-
$defaultExists = $this->root->getUserFolder($uid)->nodeExists($defaultFolder);
92-
if ($defaultExists) {
93-
return $defaultFolder;
94-
} else {
95-
return $this->l10n->t($defaultFolder);
100+
$userFolder = $this->root->getUserFolder($uid);
101+
try {
102+
/** @var Folder $node */
103+
$node = $userFolder->get($defaultFolder);
104+
return [
105+
'path' => $defaultFolder,
106+
'folder' => $node,
107+
];
108+
} catch (NotFoundException) {
109+
$path = $this->l10n->t($defaultFolder);
110+
111+
if ($path == $defaultFolder) {
112+
// English locale, still non-existing
113+
return [
114+
'path' => $path,
115+
'folder' => null,
116+
];
117+
}
118+
119+
try {
120+
$node = $userFolder->get($path);
121+
return [
122+
'path' => $path,
123+
'folder' => $node,
124+
];
125+
} catch (NotFoundException) {
126+
return [
127+
'path' => $path,
128+
'folder' => null,
129+
];
130+
}
96131
}
97132
}
98133

0 commit comments

Comments
 (0)