Skip to content

Commit

Permalink
Merge pull request #1320 from nextcloud/fix/shared-notes
Browse files Browse the repository at this point in the history
fix/shared notes
  • Loading branch information
luka-nextcloud committed Jul 15, 2024
2 parents 4e6ae0d + d8e6271 commit 0c29eaa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
3 changes: 1 addition & 2 deletions lib/AppInfo/BeforeShareCreatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function overwriteShareTarget(IShare $share): void {
$receiver = $share->getSharedWith();
$receiverPath = $this->noteUtil->getRoot()->getUserFolder($receiver)->getPath();
$receiverNotesInternalPath = $this->settings->get($receiver, 'notesPath');
$receiverNotesPath = $receiverPath . '/' . $receiverNotesInternalPath;
$this->noteUtil->getOrCreateFolder($receiverNotesPath);
$this->noteUtil->getOrCreateNotesFolder($receiver);

if ($itemType !== 'file' || strpos($fileSourcePath, $ownerNotesPath) !== 0) {
return;
Expand Down
38 changes: 34 additions & 4 deletions lib/Service/NoteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ class NoteUtil {
private TagService $tagService;
private IManager $shareManager;
private IUserSession $userSession;
private SettingsService $settingsService;

public function __construct(
Util $util,
IRootFolder $root,
IDBConnection $db,
TagService $tagService,
IManager $shareManager,
IUserSession $userSession
IUserSession $userSession,
SettingsService $settingsService
) {
$this->util = $util;
$this->root = $root;
$this->db = $db;
$this->tagService = $tagService;
$this->shareManager = $shareManager;
$this->userSession = $userSession;
$this->settingsService = $settingsService;
}

public function getRoot() : IRootFolder {
Expand Down Expand Up @@ -172,9 +175,36 @@ public function getOrCreateFolder(string $path, bool $create = true) : Folder {
throw new NotesFolderException($path.' is not a folder');
}
if ($folder->isShared()) {
$folderName = $this->root->getNonExistingName($path);
$folder = $this->root->newFolder($folderName);
return $folder;
}
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;
$updateNotesPath = false;
if ($userFolder->nodeExists($notesPath)) {
$folder = $userFolder->get($notesPath);
if (!$allowShared && $folder->isShared()) {
$notesPath = $userFolder->getNonExistingName($notesPath);
$folder = $userFolder->newFolder($notesPath);
$updateNotesPath = true;
}
} elseif ($create) {
$folder = $userFolder->newFolder($notesPath);
$updateNotesPath = true;
}
if (!($folder instanceof Folder)) {
throw new NotesFolderException($notesPath . ' is not a folder');
}
if ($updateNotesPath) {
$this->settingsService->set($userId, [
'notesPath' => $notesPath,
], true);
}
return $folder;
Expand Down
14 changes: 1 addition & 13 deletions lib/Service/NotesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,8 @@ public function getTitleFromContent(string $content) : string {
return $this->noteUtil->getSafeTitle($content);
}






/**
* @param string $userId the user id
* @return Folder
*/
private function getNotesFolder(string $userId, bool $create = true) : Folder {
$userPath = $this->noteUtil->getRoot()->getUserFolder($userId)->getPath();
$path = $userPath . '/' . $this->settings->get($userId, 'notesPath');
$folder = $this->noteUtil->getOrCreateFolder($path, $create);
return $folder;
return $this->noteUtil->getOrCreateNotesFolder($userId, $create);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private function getListAttrs(string $attributeName, array $values) : array {
];
}

private function getDefaultNotesPath(string $uid) : string {
public function getDefaultNotesPath(string $uid) : string {
$defaultFolder = $this->config->getAppValue(Application::APP_ID, 'defaultFolder', 'Notes');

Check warning on line 85 in lib/Service/SettingsService.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Call to deprecated function \OCP\IConfig::getAppValue() defined at vendor/nextcloud/ocp/OCP/IConfig.php:134 (Deprecated because: 29.0.0 Use {@see IAppConfig} directly)
$defaultExists = $this->root->getUserFolder($uid)->nodeExists($defaultFolder);
if ($defaultExists) {
Expand All @@ -94,7 +94,7 @@ private function getDefaultNotesPath(string $uid) : string {
/**
* @throws \OCP\PreConditionNotMetException
*/
public function set(string $uid, array $settings) : void {
public function set(string $uid, array $settings, bool $writeDefaults = false) : void {
// load existing values for missing attributes
$oldSettings = $this->getSettingsFromDB($uid);
foreach ($oldSettings as $name => $value) {
Expand All @@ -107,10 +107,11 @@ public function set(string $uid, array $settings) : void {
if ($value !== null && array_key_exists($name, $this->attrs)) {
$settings[$name] = $value = $this->attrs[$name]['validate']($value);
}
if (!array_key_exists($name, $this->attrs)
$default = is_callable($this->attrs[$name]['default']) ? $this->attrs[$name]['default']($uid) : $this->attrs[$name]['default'];

Check warning on line 110 in lib/Service/SettingsService.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 139 characters

Check warning on line 110 in lib/Service/SettingsService.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 139 characters
if (!$writeDefaults && (!array_key_exists($name, $this->attrs)
|| $value === null
|| $value === $this->attrs[$name]['default']
) {
|| $value === $default
)) {
unset($settings[$name]);
}
}
Expand Down

0 comments on commit 0c29eaa

Please sign in to comment.