From 38860f784f15d8d6d8bfe084434f257a66ea46b7 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 31 May 2022 14:11:56 -0300 Subject: [PATCH] Fix unable to forget shared note... Part of issue #72 --- js/script.js | 2 +- lib/Controller/ShareController.php | 10 +++------- lib/Db/NoteShareMapper.php | 26 ++++++++++++++++++-------- lib/Service/NoteService.php | 12 ++++++------ 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/js/script.js b/js/script.js index 47efd03..ce72a4b 100644 --- a/js/script.js +++ b/js/script.js @@ -370,7 +370,7 @@ View.prototype = { t('quicknotes', 'Delete note'), function(result) { if (result) { - if (!note.is_shared) { + if (!note.sharedBy.length) { self._notes.remove(note).done(function () { if (self._notes.length() > 0) { self._isotope.remove(gridnote.parent()) diff --git a/lib/Controller/ShareController.php b/lib/Controller/ShareController.php index 47c93da..a7d0455 100644 --- a/lib/Controller/ShareController.php +++ b/lib/Controller/ShareController.php @@ -55,15 +55,11 @@ class ShareController extends Controller { * @param int $noteId */ public function destroy(int $noteId): JSONResponse { - try { - $noteShare = $this->noteShareMapper->findByNoteAndUser($noteId, $this->userId); - } catch (DoesNotExistException $e) { + if ($this->noteShareMapper->forgetShareByNoteIdAndSharedUser($noteId, $this->userId)) { + return new JSONResponse([], Http::STATUS_OK); + } else { return new JSONResponse([], Http::STATUS_NOT_FOUND); } - - $this->noteShareMapper->delete($noteShare); - - return new JSONResponse([]); } } diff --git a/lib/Db/NoteShareMapper.php b/lib/Db/NoteShareMapper.php index d608eed..0bc3dd3 100644 --- a/lib/Db/NoteShareMapper.php +++ b/lib/Db/NoteShareMapper.php @@ -14,7 +14,7 @@ class NoteShareMapper extends QBMapper { parent::__construct($db, 'quicknotes_shares', NoteShare::class); } - public function findForUser(string $userId): array { + public function findSharesForUserId(string $userId): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) @@ -24,7 +24,7 @@ class NoteShareMapper extends QBMapper { return $this->findEntities($qb); } - public function findForGroup(string $groupId): array { + public function findSharesForGroupId(string $groupId): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) @@ -34,7 +34,7 @@ class NoteShareMapper extends QBMapper { return $this->findEntities($qb); } - public function findByNoteAndUser(int $noteId, string $userId): NoteShare { + public function findSharesByNoteIsAndSharedUser(int $noteId, string $userId): NoteShare { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) @@ -45,7 +45,7 @@ class NoteShareMapper extends QBMapper { return $this->findEntity($qb); } - public function findByNoteAndGroup(int $noteId, string $groupId): NoteShare { + public function findSharesByNoteIdAndGroupId(int $noteId, string $groupId): NoteShare { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) @@ -56,7 +56,7 @@ class NoteShareMapper extends QBMapper { return $this->findEntity($qb); } - public function getSharesForNote(int $noteId): array { + public function findSharesForNoteId(int $noteId): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) @@ -66,19 +66,29 @@ class NoteShareMapper extends QBMapper { return $this->findEntities($qb); } - public function deleteByNoteId(int $noteId): void { + public function forgetSharesByNoteId(int $noteId): void { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) ->where($qb->expr()->eq('note_id', $qb->createNamedParameter($noteId))) ->execute(); } + public function forgetShareByNoteIdAndSharedUser(int $noteId, string $userId) { + try { + $noteShare = $this->findSharesByNoteIsAndSharedUser($noteId, $userId); + } catch (DoesNotExistException $e) { + return false; + } + $this->delete($noteShare); + return true; + } + /** * @return bool */ - public function existsByNoteAndUser(int $noteId, string $userId) { + public function existsByNoteAndSharedUser(int $noteId, string $userId) { try { - $this->findByNoteAndUser($noteId, $userId); + $this->findSharesByNoteIsAndSharedUser($noteId, $userId); } catch (DoesNotExistException $e) { return false; } diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index 5533dfe..e506c60 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -91,7 +91,7 @@ class NoteService { // Set shares with others. foreach($notes as $note) { $sharedWith = []; - $sharedEntries = $this->noteShareMapper->getSharesForNote($note->getId()); + $sharedEntries = $this->noteShareMapper->findSharesForNoteId($note->getId()); foreach ($sharedEntries as $sharedEntry) { $sharedUid = $sharedEntry->getSharedUser(); $user = $this->userManager->get($sharedUid); @@ -107,7 +107,7 @@ class NoteService { // Get shares from others. $shares = []; - $sharedEntries = $this->noteShareMapper->findForUser($userId); + $sharedEntries = $this->noteShareMapper->findSharesForUserId($userId); foreach($sharedEntries as $sharedEntry) { $sharedNote = $this->notemapper->findShared($sharedEntry->getNoteId()); @@ -323,7 +323,7 @@ class NoteService { } // Delete old shares - $dbShares = $this->noteShareMapper->getSharesForNote($id); + $dbShares = $this->noteShareMapper->findSharesForNoteId($id); foreach ($dbShares as $dbShare) { $delete = true; foreach ($sharedWith as $share) { @@ -339,7 +339,7 @@ class NoteService { // Add new shares foreach ($sharedWith as $share) { - if (!$this->noteShareMapper->existsByNoteAndUser($id, $share['id'])) { + if (!$this->noteShareMapper->existsByNoteAndSharedUser($id, $share['id'])) { $hShare = new NoteShare(); $hShare->setNoteId($id); $hShare->setSharedUser($share['id']); @@ -412,7 +412,7 @@ class NoteService { // Fill shared with with others $sharedWith = []; - $sharedEntries = $this->noteShareMapper->getSharesForNote($note->getId()); + $sharedEntries = $this->noteShareMapper->findSharesForNoteId($note->getId()); foreach ($sharedEntries as $sharedEntry) { $sharedUid = $sharedEntry->getSharedUser(); $user = $this->userManager->get($sharedUid); @@ -454,7 +454,7 @@ class NoteService { } $oldcolorid = $note->getColorId(); - $this->noteShareMapper->deleteByNoteId($note->getId()); + $this->noteShareMapper->forgetSharesByNoteId($note->getId()); // Delete note. $this->notemapper->delete($note);