diff --git a/lib/Db/TreeMapper.php b/lib/Db/TreeMapper.php index ef2151c80..6008aa133 100644 --- a/lib/Db/TreeMapper.php +++ b/lib/Db/TreeMapper.php @@ -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); } } diff --git a/lib/Service/BookmarkService.php b/lib/Service/BookmarkService.php index e3f8c0155..7c98a4667 100644 --- a/lib/Service/BookmarkService.php +++ b/lib/Service/BookmarkService.php @@ -179,7 +179,7 @@ public function create(string $userId, string $url = '', ?string $title = null, * @throws UserLimitExceededError * @throws DoesNotExistException */ - private function _addBookmark($userId, $url, ?string $title = null, ?string $description = null, ?array $tags = null, array $folders = []): Bookmark { + private function _addBookmark($userId, $url, ?string $title = null, ?string $description = null, ?array $tags = null, array $folders = []): Bookmark { $bookmark = null; try { @@ -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()); + }else if ($oldBookmark->getId() !== $bookmark->getId()) { + throw new AlreadyExistsError('Bookmark already exists'); + } + }catch (DoesNotExistException $e) { + // pass + } if (!preg_match(self::PROTOCOLS_REGEX, $url)) { throw new UrlParseError(); }