From f054842857e1e1675a4cbf58bbde81e6e7f2714c Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Fri, 20 May 2022 19:47:45 -0300 Subject: [PATCH] Use display name of users to share dialog and notes. See issue #49 --- js/qn-dialogs.js | 10 +++--- js/script.js | 14 ++++++-- js/templates/note-item.handlebars | 2 +- js/templates/notes.handlebars | 4 +-- js/templates/shares.handlebars | 2 +- lib/Db/NoteShare.php | 8 ++++- lib/Service/NoteService.php | 56 ++++++++++++++++++++++++++----- 7 files changed, 76 insertions(+), 20 deletions(-) diff --git a/js/qn-dialogs.js b/js/qn-dialogs.js index e075257..3b65737 100644 --- a/js/qn-dialogs.js +++ b/js/qn-dialogs.js @@ -1,5 +1,5 @@ /* - * @copyright 2019-2020 Matias De lellis + * @copyright 2019-2022 Matias De lellis * * @author 2019 Matias De lellis * @@ -159,7 +159,7 @@ const QnDialogs = { var data = []; availableUsers.forEach(function (item, index) { // Select2 expect id, text. - data.push({id: item, text: item}); + data.push({id: item[0], text: item[1]}); }); return data; }, @@ -168,7 +168,7 @@ const QnDialogs = { } }); - input.val(selectedUsers.map(function (value) { return value.shared_user })); + input.val(selectedUsers.map(function (value) { return value.id })); input.trigger("change"); $('.select2-input').on("keyup", function (event) { @@ -208,7 +208,9 @@ const QnDialogs = { // Quicknotes shares expect id, shared_user newUsers = input.select2("data"); newUsers.forEach(function (item) { - item['shared_user'] = item.text; + item['shared_user'] = item.id; + item['display_name'] = item.text; + users.push(item); }); callback(true, users); diff --git a/js/script.js b/js/script.js index d365b01..ef7c6f2 100644 --- a/js/script.js +++ b/js/script.js @@ -1,5 +1,5 @@ /* - * @copyright 2016-2020 Matias De lellis + * @copyright 2016-2022 Matias De lellis * * @author 2016 Matias De lellis * @@ -169,10 +169,10 @@ Notes.prototype = { }).done(function (shares) { var users = []; $.each(shares.ocs.data.exact.users, function(index, user) { - users.push(user.value.shareWith); + users.push([user.value.shareWith, user.label]); }); $.each(shares.ocs.data.users, function(index, user) { - users.push(user.value.shareWith); + users.push([user.value.shareWith, user.label]); }); self._usersSharing = users; }).fail(function () { @@ -233,6 +233,7 @@ View.prototype = { tags: this._editableTags(), sharedWith: this._editableShares() }; + var self = this; this._notes.update(fakeNote).done(function (note) { // Create an new note and replace in grid. @@ -530,6 +531,12 @@ View.prototype = { $('#modal-note-div #tag-button').trigger( "click"); }); + // Handle shares on modal + $('#modal-note-div').on("click", ".slim-share", function (event) { + event.stopPropagation(); + $('#modal-note-div #share-button').trigger( "click"); + }); + // handle tags button. $('#modal-note-div').on("click", "#share-button", function (event) { event.stopPropagation(); @@ -545,6 +552,7 @@ View.prototype = { ); }); + // handle color button. $('#modal-note-div').on("click", "#color-button", function (event) { event.stopPropagation(); self._colorPick.toggle(); diff --git a/js/templates/note-item.handlebars b/js/templates/note-item.handlebars index 1c57fbb..bc23b76 100644 --- a/js/templates/note-item.handlebars +++ b/js/templates/note-item.handlebars @@ -26,7 +26,7 @@
{{#each sharedWith}} - + {{/each}}
diff --git a/js/templates/notes.handlebars b/js/templates/notes.handlebars index b55b8ac..d4886e0 100644 --- a/js/templates/notes.handlebars +++ b/js/templates/notes.handlebars @@ -15,7 +15,7 @@
{{#if sharedBy}} -
+
{{else}} {{#if isPinned}} @@ -34,7 +34,7 @@
{{#each sharedWith}} - + {{/each}}
diff --git a/js/templates/shares.handlebars b/js/templates/shares.handlebars index 542c009..5d5f2f8 100644 --- a/js/templates/shares.handlebars +++ b/js/templates/shares.handlebars @@ -1,5 +1,5 @@
{{#each sharedWith}} - + {{/each}}
\ No newline at end of file diff --git a/lib/Db/NoteShare.php b/lib/Db/NoteShare.php index 6a44d3d..a6fbd66 100644 --- a/lib/Db/NoteShare.php +++ b/lib/Db/NoteShare.php @@ -20,18 +20,24 @@ class NoteShare extends Entity implements JsonSerializable { protected $sharedGroup; protected $userId; + protected $displayname; public function setUserId (string $userId): void { $this->userId = $userId; } + public function setDisplayName (string $displayName): void { + $this->displayName = $displayName; + } + public function jsonSerialize() { return [ 'id' => $this->id, 'note_id' => $this->noteId, 'shared_user' => $this->sharedUser, 'shared_group' => $this->sharedGroup, - 'user_id' => $this->userId + 'user_id' => $this->userId, + 'display_name' => $this->displayName ]; } diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index 9f20e96..5533dfe 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -1,6 +1,6 @@ + * @copyright 2016-2022 Matias De lellis * * @author 2016 Matias De lellis * @@ -45,6 +45,8 @@ use OCA\QuickNotes\Service\SettingsService; use OCP\AppFramework\Db\DoesNotExistException; +use OCP\IUserManager; + class NoteService { private $notemapper; @@ -56,6 +58,9 @@ class NoteService { private $fileService; private $settingsService; + /** @var IUserManager */ + private $userManager; + public function __construct(NoteMapper $notemapper, NoteTagMapper $notetagmapper, NoteShareMapper $noteShareMapper, @@ -63,7 +68,8 @@ class NoteService { AttachMapper $attachMapper, TagMapper $tagmapper, FileService $fileService, - SettingsService $settingsService) + SettingsService $settingsService, + IUserManager $userManager) { $this->notemapper = $notemapper; $this->notetagmapper = $notetagmapper; @@ -73,6 +79,7 @@ class NoteService { $this->tagmapper = $tagmapper; $this->fileService = $fileService; $this->settingsService = $settingsService; + $this->userManager = $userManager; } /** @@ -83,7 +90,19 @@ class NoteService { // Set shares with others. foreach($notes as $note) { - $note->setSharedWith($this->noteShareMapper->getSharesForNote($note->getId())); + $sharedWith = []; + $sharedEntries = $this->noteShareMapper->getSharesForNote($note->getId()); + foreach ($sharedEntries as $sharedEntry) { + $sharedUid = $sharedEntry->getSharedUser(); + $user = $this->userManager->get($sharedUid); + if (!$user) { + // TODO: Debug that... + continue; + } + $sharedEntry->setDisplayName($user->getDisplayName()); + $sharedWith[] = $sharedEntry; + } + $note->setSharedWith($sharedWith); } // Get shares from others. @@ -91,7 +110,15 @@ class NoteService { $sharedEntries = $this->noteShareMapper->findForUser($userId); foreach($sharedEntries as $sharedEntry) { $sharedNote = $this->notemapper->findShared($sharedEntry->getNoteId()); - $sharedEntry->setUserId($sharedNote->getUserId()); + + $uid = $sharedNote->getUserId(); + $sharedEntry->setUserId($uid); + $user = $this->userManager->get($uid); + if (!$user) { + // TODO: Debug that... + continue; + } + $sharedEntry->setDisplayName($user->getDisplayName()); $sharedNote->setSharedBy([$sharedEntry]); $shares[] = $sharedNote; } @@ -300,7 +327,7 @@ class NoteService { foreach ($dbShares as $dbShare) { $delete = true; foreach ($sharedWith as $share) { - if ($dbShare->getSharedUser() === $share['shared_user']) { + if ($dbShare->getSharedUser() === $share['id']) { $delete = false; break; } @@ -312,10 +339,10 @@ class NoteService { // Add new shares foreach ($sharedWith as $share) { - if (!$this->noteShareMapper->existsByNoteAndUser($id, $share['shared_user'])) { + if (!$this->noteShareMapper->existsByNoteAndUser($id, $share['id'])) { $hShare = new NoteShare(); $hShare->setNoteId($id); - $hShare->setSharedUser($share['shared_user']); + $hShare->setSharedUser($share['id']); $this->noteShareMapper->insert($hShare); } } @@ -384,7 +411,20 @@ class NoteService { $newnote->setAttachts($attachts); // Fill shared with with others - $newnote->setSharedWith($this->noteShareMapper->getSharesForNote($newnote->getId())); + $sharedWith = []; + $sharedEntries = $this->noteShareMapper->getSharesForNote($note->getId()); + foreach ($sharedEntries as $sharedEntry) { + $sharedUid = $sharedEntry->getSharedUser(); + $user = $this->userManager->get($sharedUid); + if (!$user) { + // TODO: Debug that... + continue; + } + $sharedEntry->setDisplayName($user->getDisplayName()); + + $sharedWith[] = $sharedEntry; + } + $note->setSharedWith($sharedWith); // Remove old color if necessary if (($oldcolorid !== $hcolor->getId()) &&