Skip to content

Commit

Permalink
Merge pull request #2245 from nextcloud/fix/could-not-add-bookmark
Browse files Browse the repository at this point in the history
fix(BookmarkService): Prevent creation of duplicate bookmarks
  • Loading branch information
marcelklehr authored Nov 28, 2024
2 parents 42dd0e9 + bb28d61 commit 65ceb01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/Db/TreeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,12 @@ public function deleteEntry(string $type, int $id, ?int $folderId = null): void
}

if ($type === TreeMapper::TYPE_BOOKMARK) {
$this->removeFromFolders(TreeMapper::TYPE_BOOKMARK, $id, [$folderId]);
if ($folderId === null) {
$folders = array_map(fn (Folder $folder) => $folder->getId(), $this->findParentsOf(TreeMapper::TYPE_BOOKMARK, $id, true));
} else {
$folders = [$folderId];
}
$this->removeFromFolders(TreeMapper::TYPE_BOOKMARK, $id, $folders);
}
}

Expand Down
10 changes: 10 additions & 0 deletions lib/Service/BookmarkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ public function update(string $userId, int $id, ?string $url = null, ?string $ti
}

if ($url !== null) {
try {
$oldBookmark = $this->bookmarkMapper->findByUrl($userId, $url);
if (count($this->treeMapper->findParentsOf(TreeMapper::TYPE_BOOKMARK, $oldBookmark->getId(), false)) === 0) {
$this->treeMapper->deleteEntry(TreeMapper::TYPE_BOOKMARK, $oldBookmark->getId());
} elseif ($oldBookmark->getId() !== $bookmark->getId()) {
throw new AlreadyExistsError('Bookmark already exists');
}
} catch (DoesNotExistException $e) {
// pass
}
if (!preg_match(self::PROTOCOLS_REGEX, $url)) {
throw new UrlParseError();
}
Expand Down

0 comments on commit 65ceb01

Please sign in to comment.