From 238b4c8bca4a6e4df812b8da54e4e9f6aedb7edb Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 12 Aug 2025 17:09:28 +0200 Subject: [PATCH] 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 --- lib/Service/NoteUtil.php | 17 +++++++++--- lib/Service/SettingsService.php | 49 ++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/Service/NoteUtil.php b/lib/Service/NoteUtil.php index 150a221c4..38f066348 100644 --- a/lib/Service/NoteUtil.php +++ b/lib/Service/NoteUtil.php @@ -13,6 +13,7 @@ use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\Node; +use OCP\Files\NotFoundException; use OCP\IDBConnection; use OCP\IUserSession; use OCP\Share\IManager; @@ -196,12 +197,20 @@ public function getNotesFolderUserPath(string $userId, bool $saveInitial = false public function getOrCreateNotesFolder(string $userId, bool $create = true) : Folder { $userFolder = $this->getRoot()->getUserFolder($userId); $notesPath = $this->settingsService->get($userId, 'notesPath'); - $allowShared = $notesPath !== $this->settingsService->getDefaultNotesPath($userId); - $folder = null; + ['path' => $defaultPath, 'node' => $folder] = $this->settingsService->getDefaultNotesNode($userId); + $allowShared = $notesPath !== $defaultPath; + + if ($allowShared) { + try { + $folder = $userFolder->get($notesPath); + } catch (NotFoundException) { + $folder = null; + } + } + $updateNotesPath = false; - if ($userFolder->nodeExists($notesPath)) { - $folder = $userFolder->get($notesPath); + if ($folder instanceof Folder) { if (!$allowShared && $folder->isShared()) { $notesPath = $userFolder->getNonExistingName($notesPath); $folder = $userFolder->newFolder($notesPath); diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php index 8fc62a687..eadc9a9e5 100644 --- a/lib/Service/SettingsService.php +++ b/lib/Service/SettingsService.php @@ -12,7 +12,9 @@ use OCA\Notes\AppInfo\Application; use OCP\App\IAppManager; +use OCP\Files\Folder; use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; use OCP\IConfig; use OCP\IL10N; @@ -41,7 +43,7 @@ public function __construct( 'fileSuffix' => $this->getListAttrs('fileSuffix', [...$this->defaultSuffixes, 'custom']), 'notesPath' => [ 'default' => function (string $uid) { - return $this->getDefaultNotesPath($uid); + return $this->getDefaultNotesNode($uid)['path']; }, 'validate' => function ($value) { $value = str_replace([ '/', '\\' ], DIRECTORY_SEPARATOR, $value); @@ -86,13 +88,46 @@ private function getListAttrs(string $attributeName, array $values) : array { ]; } - public function getDefaultNotesPath(string $uid) : string { + /** + * Return the default notes node if it exists and the expected path if it exists + * @return array{ + * path: string, + * folder: ?Folder + * } + */ + public function getDefaultNotesNode(string $uid): array { $defaultFolder = $this->config->getAppValue(Application::APP_ID, 'defaultFolder', 'Notes'); - $defaultExists = $this->root->getUserFolder($uid)->nodeExists($defaultFolder); - if ($defaultExists) { - return $defaultFolder; - } else { - return $this->l10n->t($defaultFolder); + $userFolder = $this->root->getUserFolder($uid); + try { + /** @var Folder $node */ + $node = $userFolder->get($defaultFolder); + return [ + 'path' => $defaultFolder, + 'folder' => $node, + ]; + } catch (NotFoundException) { + $path = $this->l10n->t($defaultFolder); + + if ($path == $defaultFolder) { + // English locale, still non-existing + return [ + 'path' => $path, + 'folder' => null, + ]; + } + + try { + $node = $userFolder->get($path); + return [ + 'path' => $path, + 'folder' => $node, + ]; + } catch (NotFoundException) { + return [ + 'path' => $path, + 'folder' => null, + ]; + } } }