From 523ccdad6b183377216dabb020094c9952563fe0 Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 8 Jan 2019 20:19:41 +0100 Subject: [PATCH] reload note cache after import, closes #293 --- src/routes/api/import.js | 19 ++++++++++++++----- src/services/note_cache.js | 38 +++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/routes/api/import.js b/src/routes/api/import.js index 623b38897..a73e62550 100644 --- a/src/routes/api/import.js +++ b/src/routes/api/import.js @@ -7,6 +7,7 @@ const tarImportService = require('../../services/import/tar'); const singleImportService = require('../../services/import/single'); const cls = require('../../services/cls'); const path = require('path'); +const noteCacheService = require('../../services/note_cache'); async function importToBranch(req) { const parentNoteId = req.params.parentNoteId; @@ -28,24 +29,32 @@ async function importToBranch(req) { // and may produce unintended consequences cls.disableEntityEvents(); + let note; // typically root of the import - client can show it after finishing the import + if (extension === '.tar') { - return await tarImportService.importTar(file.buffer, parentNote); + note = await tarImportService.importTar(file.buffer, parentNote); } else if (extension === '.opml') { - return await opmlImportService.importOpml(file.buffer, parentNote); + note = await opmlImportService.importOpml(file.buffer, parentNote); } else if (extension === '.md') { - return await singleImportService.importMarkdown(file, parentNote); + note = await singleImportService.importMarkdown(file, parentNote); } else if (extension === '.html' || extension === '.htm') { - return await singleImportService.importHtml(file, parentNote); + note = await singleImportService.importHtml(file, parentNote); } else if (extension === '.enex') { - return await enexImportService.importEnex(file, parentNote); + note = await enexImportService.importEnex(file, parentNote); } else { return [400, `Unrecognized extension ${extension}, must be .tar or .opml`]; } + + // import has deactivated note events so note cache is not updated + // instead we force it to reload (can be async) + noteCacheService.load(); + + return note; } module.exports = { diff --git a/src/services/note_cache.js b/src/services/note_cache.js index e30aa87a5..60e6e0003 100644 --- a/src/services/note_cache.js +++ b/src/services/note_cache.js @@ -33,9 +33,21 @@ async function load() { archived = await sql.getMap(`SELECT noteId, isInheritable FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name = 'archived'`); + if (protectedSessionService.isProtectedSessionAvailable()) { + await loadProtectedNotes(); + } + loaded = true; } +async function loadProtectedNotes() { + protectedNoteTitles = await sql.getMap(`SELECT noteId, title FROM notes WHERE isDeleted = 0 AND isProtected = 1`); + + for (const noteId in protectedNoteTitles) { + protectedNoteTitles[noteId] = protectedSessionService.decryptNoteTitle(noteId, protectedNoteTitles[noteId]); + } +} + function highlightResults(results, allTokens) { // we remove < signs because they can cause trouble in matching and overwriting existing highlighted chunks // which would make the resulting HTML string invalid. @@ -314,7 +326,16 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED delete childToParent[note.noteId]; } else { - noteTitles[note.noteId] = note.title; + if (note.isProtected) { + // we can assume we have protected session since we managed to update + // removing from the maps is important when switching between protected & unprotected + protectedNoteTitles[note.noteId] = note.title; + delete noteTitles[note.noteId]; + } + else { + noteTitles[note.noteId] = note.title; + delete protectedNoteTitles[note.noteId]; + } } } else if (entityName === 'branches') { @@ -355,15 +376,9 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED } }); -eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, async () => { - if (!loaded) { - return; - } - - protectedNoteTitles = await sql.getMap(`SELECT noteId, title FROM notes WHERE isDeleted = 0 AND isProtected = 1`); - - for (const noteId in protectedNoteTitles) { - protectedNoteTitles[noteId] = protectedSessionService.decryptNoteTitle(noteId, protectedNoteTitles[noteId]); +eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { + if (loaded) { + loadProtectedNotes(); } }); @@ -372,5 +387,6 @@ sqlInit.dbReady.then(() => utils.stopWatch("Autocomplete load", load)); module.exports = { findNotes, getNotePath, - getNoteTitleForPath + getNoteTitleForPath, + load }; \ No newline at end of file