Skip to content

Commit

Permalink
fix(BookmarkService): Prevent creation of duplicate bookmarks
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Nov 28, 2024
1 parent 42dd0e9 commit b55a250
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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
12 changes: 11 additions & 1 deletion lib/Service/BookmarkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down 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());
}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();
}
Expand Down

0 comments on commit b55a250

Please sign in to comment.