Skip to content

Commit b1d5496

Browse files
authored
Merge pull request #1327 from nextcloud/backport/1320/stable4.10
[stable4.10] fix/shared notes
2 parents b8d9004 + b9d69c9 commit b1d5496

File tree

4 files changed

+42
-24
lines changed

4 files changed

+42
-24
lines changed

lib/AppInfo/BeforeShareCreatedListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public function overwriteShareTarget(IShare $share): void {
4949
$receiver = $share->getSharedWith();
5050
$receiverPath = $this->noteUtil->getRoot()->getUserFolder($receiver)->getPath();
5151
$receiverNotesInternalPath = $this->settings->get($receiver, 'notesPath');
52-
$receiverNotesPath = $receiverPath . '/' . $receiverNotesInternalPath;
53-
$this->noteUtil->getOrCreateFolder($receiverNotesPath);
52+
$this->noteUtil->getOrCreateNotesFolder($receiver);
5453

5554
if ($itemType !== 'file' || strpos($fileSourcePath, $ownerNotesPath) !== 0) {
5655
return;

lib/Service/NoteUtil.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@ class NoteUtil {
2121
private TagService $tagService;
2222
private IManager $shareManager;
2323
private IUserSession $userSession;
24+
private SettingsService $settingsService;
2425

2526
public function __construct(
2627
Util $util,
2728
IRootFolder $root,
2829
IDBConnection $db,
2930
TagService $tagService,
3031
IManager $shareManager,
31-
IUserSession $userSession
32+
IUserSession $userSession,
33+
SettingsService $settingsService
3234
) {
3335
$this->util = $util;
3436
$this->root = $root;
3537
$this->db = $db;
3638
$this->tagService = $tagService;
3739
$this->shareManager = $shareManager;
3840
$this->userSession = $userSession;
41+
$this->settingsService = $settingsService;
3942
}
4043

4144
public function getRoot() : IRootFolder {
@@ -172,9 +175,36 @@ public function getOrCreateFolder(string $path, bool $create = true) : Folder {
172175
throw new NotesFolderException($path.' is not a folder');
173176
}
174177

175-
if ($folder->isShared()) {
176-
$folderName = $this->root->getNonExistingName($path);
177-
$folder = $this->root->newFolder($folderName);
178+
return $folder;
179+
}
180+
181+
public function getOrCreateNotesFolder(string $userId, bool $create = true) : Folder {
182+
$userFolder = $this->getRoot()->getUserFolder($userId);
183+
$notesPath = $this->settingsService->get($userId, 'notesPath');
184+
$allowShared = $notesPath !== $this->settingsService->getDefaultNotesPath($userId);
185+
186+
$folder = null;
187+
$updateNotesPath = false;
188+
if ($userFolder->nodeExists($notesPath)) {
189+
$folder = $userFolder->get($notesPath);
190+
if (!$allowShared && $folder->isShared()) {
191+
$notesPath = $userFolder->getNonExistingName($notesPath);
192+
$folder = $userFolder->newFolder($notesPath);
193+
$updateNotesPath = true;
194+
}
195+
} elseif ($create) {
196+
$folder = $userFolder->newFolder($notesPath);
197+
$updateNotesPath = true;
198+
}
199+
200+
if (!($folder instanceof Folder)) {
201+
throw new NotesFolderException($notesPath . ' is not a folder');
202+
}
203+
204+
if ($updateNotesPath) {
205+
$this->settingsService->set($userId, [
206+
'notesPath' => $notesPath,
207+
], true);
178208
}
179209

180210
return $folder;

lib/Service/NotesService.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,8 @@ public function getTitleFromContent(string $content) : string {
149149
return $this->noteUtil->getSafeTitle($content);
150150
}
151151

152-
153-
154-
155-
156-
157-
/**
158-
* @param string $userId the user id
159-
* @return Folder
160-
*/
161152
private function getNotesFolder(string $userId, bool $create = true) : Folder {
162-
$userPath = $this->noteUtil->getRoot()->getUserFolder($userId)->getPath();
163-
$path = $userPath . '/' . $this->settings->get($userId, 'notesPath');
164-
$folder = $this->noteUtil->getOrCreateFolder($path, $create);
165-
return $folder;
153+
return $this->noteUtil->getOrCreateNotesFolder($userId, $create);
166154
}
167155

168156
/**

lib/Service/SettingsService.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private function getListAttrs(string $attributeName, array $values) : array {
8181
];
8282
}
8383

84-
private function getDefaultNotesPath(string $uid) : string {
84+
public function getDefaultNotesPath(string $uid) : string {
8585
$defaultFolder = $this->config->getAppValue(Application::APP_ID, 'defaultFolder', 'Notes');
8686
$defaultExists = $this->root->getUserFolder($uid)->nodeExists($defaultFolder);
8787
if ($defaultExists) {
@@ -94,7 +94,7 @@ private function getDefaultNotesPath(string $uid) : string {
9494
/**
9595
* @throws \OCP\PreConditionNotMetException
9696
*/
97-
public function set(string $uid, array $settings) : void {
97+
public function set(string $uid, array $settings, bool $writeDefaults = false) : void {
9898
// load existing values for missing attributes
9999
$oldSettings = $this->getSettingsFromDB($uid);
100100
foreach ($oldSettings as $name => $value) {
@@ -107,10 +107,11 @@ public function set(string $uid, array $settings) : void {
107107
if ($value !== null && array_key_exists($name, $this->attrs)) {
108108
$settings[$name] = $value = $this->attrs[$name]['validate']($value);
109109
}
110-
if (!array_key_exists($name, $this->attrs)
110+
$default = is_callable($this->attrs[$name]['default']) ? $this->attrs[$name]['default']($uid) : $this->attrs[$name]['default'];
111+
if (!$writeDefaults && (!array_key_exists($name, $this->attrs)
111112
|| $value === null
112-
|| $value === $this->attrs[$name]['default']
113-
) {
113+
|| $value === $default
114+
)) {
114115
unset($settings[$name]);
115116
}
116117
}

0 commit comments

Comments
 (0)