From 639284fd8534ae70ca93a490e3d6ece19510f2ca Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 30 Oct 2018 22:18:20 +0100 Subject: [PATCH] changing title directly from relation map --- src/entities/note.js | 5 +++++ .../services/note_detail_relation_map.js | 11 +++++----- src/routes/api/notes.js | 22 ++++++++++++++++++- src/routes/routes.js | 1 + src/services/notes.js | 4 ++++ src/services/repository.js | 1 + 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/entities/note.js b/src/entities/note.js index 005ef1ad8..68d659860 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -36,9 +36,14 @@ class Note extends Entity { super(row); this.isProtected = !!this.isProtected; + /* true if content (meaning any kind of potentially encrypted content) is either not encrypted + * or encrypted, but with available protected session (so effectively decrypted) */ + this.isContentAvailable = true; // check if there's noteId, otherwise this is a new entity which wasn't encrypted yet if (this.isProtected && this.noteId) { + this.isContentAvailable = protectedSessionService.isProtectedSessionAvailable(); + protectedSessionService.decryptNote(this); } diff --git a/src/public/javascripts/services/note_detail_relation_map.js b/src/public/javascripts/services/note_detail_relation_map.js index 39c5f72e9..2b5c286ab 100644 --- a/src/public/javascripts/services/note_detail_relation_map.js +++ b/src/public/javascripts/services/note_detail_relation_map.js @@ -2,6 +2,7 @@ import server from "./server.js"; import noteDetailService from "./note_detail.js"; import linkService from "./link.js"; import libraryLoader from "./library_loader.js"; +import treeService from "./tree.js"; const $noteDetailRelationMap = $("#note-detail-relation-map"); const $relationMapCanvas = $("#relation-map-canvas"); @@ -267,18 +268,18 @@ async function noteContextMenuHandler(event, ui) { saveData(); } else if (ui.cmd === "edit-title") { - const title = prompt("Enter new note title:"); + const $title = $noteBox.find(".title a"); + const title = prompt("Enter new note title:", $title.text()); if (!title) { return; } - const note = mapData.notes.find(note => note.id === noteId); - note.title = title; + await server.put(`notes/${noteId}/change-title`, { title }); - $noteBox.find(".title").text(note.title); + treeService.setNoteTitle(noteId, title); - saveData(); + $title.text(title); } } diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index 60c8f260a..20413d0cf 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -122,6 +122,25 @@ async function getRelationMap(req) { return resp; } +async function changeTitle(req) { + const noteId = req.params.noteId; + const title = req.body.title; + + const note = await repository.getNote(noteId); + + if (!note) { + return [404, `Note ${noteId} has not been found`]; + } + + if (!note.isContentAvailable) { + return [400, `Note ${noteId} is not available for change`]; + } + + note.title = title; + + await note.save(); +} + module.exports = { getNote, updateNote, @@ -130,5 +149,6 @@ module.exports = { protectSubtree, setNoteTypeMime, getChildren, - getRelationMap + getRelationMap, + changeTitle }; \ No newline at end of file diff --git a/src/routes/routes.js b/src/routes/routes.js index e500202b9..187c8eec5 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -122,6 +122,7 @@ function register(app) { apiRoute(PUT, /\/api\/notes\/(.*)\/type\/(.*)\/mime\/(.*)/, notesApiRoute.setNoteTypeMime); apiRoute(GET, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.getNoteRevisions); apiRoute(POST, '/api/notes/relation-map', notesApiRoute.getRelationMap); + apiRoute(PUT, '/api/notes/:noteId/change-title', notesApiRoute.changeTitle); apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent); apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter); diff --git a/src/services/notes.js b/src/services/notes.js index 03054af70..b48506bb0 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -222,6 +222,10 @@ async function saveNoteRevision(note) { async function updateNote(noteId, noteUpdates) { const note = await repository.getNote(noteId); + if (!note.isContentAvailable) { + throw new Error(`Note ${noteId} is not available for change!`); + } + if (note.type === 'file') { // for update file, newNote doesn't contain file payloads noteUpdates.content = note.content; diff --git a/src/services/repository.js b/src/services/repository.js index 7021bbe09..c7720cfb5 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -78,6 +78,7 @@ async function updateEntity(entity) { delete clone.isOwned; delete clone.isChanged; delete clone.origParentNoteId; + delete clone.isContentAvailable; delete clone.__attributeCache; for (const key in clone) {