mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	reload note cache after import, closes #293
This commit is contained in:
		@@ -7,6 +7,7 @@ const tarImportService = require('../../services/import/tar');
 | 
				
			|||||||
const singleImportService = require('../../services/import/single');
 | 
					const singleImportService = require('../../services/import/single');
 | 
				
			||||||
const cls = require('../../services/cls');
 | 
					const cls = require('../../services/cls');
 | 
				
			||||||
const path = require('path');
 | 
					const path = require('path');
 | 
				
			||||||
 | 
					const noteCacheService = require('../../services/note_cache');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function importToBranch(req) {
 | 
					async function importToBranch(req) {
 | 
				
			||||||
    const parentNoteId = req.params.parentNoteId;
 | 
					    const parentNoteId = req.params.parentNoteId;
 | 
				
			||||||
@@ -28,24 +29,32 @@ async function importToBranch(req) {
 | 
				
			|||||||
    // and may produce unintended consequences
 | 
					    // and may produce unintended consequences
 | 
				
			||||||
    cls.disableEntityEvents();
 | 
					    cls.disableEntityEvents();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let note; // typically root of the import - client can show it after finishing the import
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (extension === '.tar') {
 | 
					    if (extension === '.tar') {
 | 
				
			||||||
        return await tarImportService.importTar(file.buffer, parentNote);
 | 
					        note = await tarImportService.importTar(file.buffer, parentNote);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else if (extension === '.opml') {
 | 
					    else if (extension === '.opml') {
 | 
				
			||||||
        return await opmlImportService.importOpml(file.buffer, parentNote);
 | 
					        note = await opmlImportService.importOpml(file.buffer, parentNote);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else if (extension === '.md') {
 | 
					    else if (extension === '.md') {
 | 
				
			||||||
        return await singleImportService.importMarkdown(file, parentNote);
 | 
					        note = await singleImportService.importMarkdown(file, parentNote);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else if (extension === '.html' || extension === '.htm') {
 | 
					    else if (extension === '.html' || extension === '.htm') {
 | 
				
			||||||
        return await singleImportService.importHtml(file, parentNote);
 | 
					        note = await singleImportService.importHtml(file, parentNote);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else if (extension === '.enex') {
 | 
					    else if (extension === '.enex') {
 | 
				
			||||||
        return await enexImportService.importEnex(file, parentNote);
 | 
					        note = await enexImportService.importEnex(file, parentNote);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else {
 | 
					    else {
 | 
				
			||||||
        return [400, `Unrecognized extension ${extension}, must be .tar or .opml`];
 | 
					        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 = {
 | 
					module.exports = {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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'`);
 | 
					    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;
 | 
					    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) {
 | 
					function highlightResults(results, allTokens) {
 | 
				
			||||||
    // we remove < signs because they can cause trouble in matching and overwriting existing highlighted chunks
 | 
					    // we remove < signs because they can cause trouble in matching and overwriting existing highlighted chunks
 | 
				
			||||||
    // which would make the resulting HTML string invalid.
 | 
					    // which would make the resulting HTML string invalid.
 | 
				
			||||||
@@ -313,8 +325,17 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED
 | 
				
			|||||||
            delete noteTitles[note.noteId];
 | 
					            delete noteTitles[note.noteId];
 | 
				
			||||||
            delete childToParent[note.noteId];
 | 
					            delete childToParent[note.noteId];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        else {
 | 
				
			||||||
 | 
					            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 {
 | 
					            else {
 | 
				
			||||||
                noteTitles[note.noteId] = note.title;
 | 
					                noteTitles[note.noteId] = note.title;
 | 
				
			||||||
 | 
					                delete protectedNoteTitles[note.noteId];
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else if (entityName === 'branches') {
 | 
					    else if (entityName === 'branches') {
 | 
				
			||||||
@@ -355,15 +376,9 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, async () => {
 | 
					eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
 | 
				
			||||||
    if (!loaded) {
 | 
					    if (loaded) {
 | 
				
			||||||
        return;
 | 
					        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]);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -372,5 +387,6 @@ sqlInit.dbReady.then(() => utils.stopWatch("Autocomplete load", load));
 | 
				
			|||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
    findNotes,
 | 
					    findNotes,
 | 
				
			||||||
    getNotePath,
 | 
					    getNotePath,
 | 
				
			||||||
    getNoteTitleForPath
 | 
					    getNoteTitleForPath,
 | 
				
			||||||
 | 
					    load
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
		Reference in New Issue
	
	Block a user