mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	refactor exportcontext to taskcontext
This commit is contained in:
		| @@ -57,7 +57,6 @@ | ||||
|     <index id="24" parent="6" name="sqlite_autoindex_api_tokens_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>apiTokenId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <key id="25" parent="6"> | ||||
| @@ -126,25 +125,20 @@ | ||||
|     <index id="37" parent="7" name="sqlite_autoindex_attributes_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>attributeId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <index id="38" parent="7" name="IDX_attributes_noteId_index"> | ||||
|       <ColNames>noteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <index id="39" parent="7" name="IDX_attributes_name_value"> | ||||
|       <ColNames>name | ||||
| value</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <index id="40" parent="7" name="IDX_attributes_name_index"> | ||||
|       <ColNames>name</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <index id="41" parent="7" name="IDX_attributes_value_index"> | ||||
|       <ColNames>value</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <key id="42" parent="7"> | ||||
|       <ColNames>attributeId</ColNames> | ||||
| @@ -204,21 +198,17 @@ value</ColNames> | ||||
|     <index id="53" parent="8" name="sqlite_autoindex_branches_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>branchId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <index id="54" parent="8" name="IDX_branches_noteId_parentNoteId"> | ||||
|       <ColNames>noteId | ||||
| parentNoteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <index id="55" parent="8" name="IDX_branches_noteId"> | ||||
|       <ColNames>noteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <index id="56" parent="8" name="IDX_branches_parentNoteId"> | ||||
|       <ColNames>parentNoteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <key id="57" parent="8"> | ||||
|       <ColNames>branchId</ColNames> | ||||
| @@ -246,7 +236,6 @@ parentNoteId</ColNames> | ||||
|     <index id="62" parent="9" name="sqlite_autoindex_event_log_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>eventId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <key id="63" parent="9"> | ||||
| @@ -278,7 +267,6 @@ parentNoteId</ColNames> | ||||
|     <index id="68" parent="10" name="sqlite_autoindex_note_contents_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>noteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <key id="69" parent="10"> | ||||
| @@ -351,20 +339,16 @@ parentNoteId</ColNames> | ||||
|     <index id="82" parent="11" name="sqlite_autoindex_note_revisions_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>noteRevisionId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <index id="83" parent="11" name="IDX_note_revisions_noteId"> | ||||
|       <ColNames>noteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <index id="84" parent="11" name="IDX_note_revisions_dateModifiedFrom"> | ||||
|       <ColNames>utcDateModifiedFrom</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <index id="85" parent="11" name="IDX_note_revisions_dateModifiedTo"> | ||||
|       <ColNames>utcDateModifiedTo</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <key id="86" parent="11"> | ||||
|       <ColNames>noteRevisionId</ColNames> | ||||
| @@ -435,7 +419,6 @@ parentNoteId</ColNames> | ||||
|     <index id="98" parent="12" name="sqlite_autoindex_notes_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>noteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <key id="99" parent="12"> | ||||
| @@ -477,7 +460,6 @@ parentNoteId</ColNames> | ||||
|     <index id="106" parent="13" name="sqlite_autoindex_options_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>name</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <key id="107" parent="13"> | ||||
| @@ -513,7 +495,6 @@ parentNoteId</ColNames> | ||||
|     <index id="113" parent="14" name="sqlite_autoindex_recent_notes_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>noteId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <key id="114" parent="14"> | ||||
| @@ -534,7 +515,6 @@ parentNoteId</ColNames> | ||||
|     <index id="117" parent="15" name="sqlite_autoindex_source_ids_1"> | ||||
|       <NameSurrogate>1</NameSurrogate> | ||||
|       <ColNames>sourceId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <key id="118" parent="15"> | ||||
| @@ -597,12 +577,10 @@ parentNoteId</ColNames> | ||||
|     <index id="131" parent="18" name="IDX_sync_entityName_entityId"> | ||||
|       <ColNames>entityName | ||||
| entityId</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|       <Unique>1</Unique> | ||||
|     </index> | ||||
|     <index id="132" parent="18" name="IDX_sync_utcSyncDate"> | ||||
|       <ColNames>utcSyncDate</ColNames> | ||||
|       <ColumnCollations></ColumnCollations> | ||||
|     </index> | ||||
|     <key id="133" parent="18"> | ||||
|       <ColNames>id</ColNames> | ||||
|   | ||||
| @@ -15,14 +15,14 @@ const $exportProgressCount = $("#export-progress-count"); | ||||
| const $exportButton = $("#export-button"); | ||||
| const $opmlVersions = $("#opml-versions"); | ||||
|  | ||||
| let exportId = ''; | ||||
| let taskId = ''; | ||||
| let branchId = null; | ||||
|  | ||||
| export async function showDialog(node, defaultType) { | ||||
|     utils.closeActiveDialog(); | ||||
|  | ||||
|     // each opening of the dialog resets the exportId so we don't associate it with previous exports anymore | ||||
|     exportId = ''; | ||||
|     // each opening of the dialog resets the taskId so we don't associate it with previous exports anymore | ||||
|     taskId = ''; | ||||
|     $exportButton.removeAttr("disabled"); | ||||
|     $exportProgressWrapper.hide(); | ||||
|     $exportProgressCount.text('0'); | ||||
| @@ -77,9 +77,9 @@ $form.submit(() => { | ||||
| }); | ||||
|  | ||||
| function exportBranch(branchId, type, format, version) { | ||||
|     exportId = utils.randomString(10); | ||||
|     taskId = utils.randomString(10); | ||||
|  | ||||
|     const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}/${version}/${exportId}`; | ||||
|     const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}/${version}/${taskId}`; | ||||
|  | ||||
|     utils.download(url); | ||||
| } | ||||
| @@ -113,23 +113,23 @@ $('input[name=export-subtree-format]').change(function () { | ||||
| }); | ||||
|  | ||||
| ws.subscribeToMessages(async message => { | ||||
|     if (message.type === 'export-error') { | ||||
|     if (message.type === 'task-error' && message.taskType === 'export') { | ||||
|         infoService.showError(message.message); | ||||
|         $dialog.modal('hide'); | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     if (!message.exportId || message.exportId !== exportId) { | ||||
|     if (!message.taskId || message.taskId !== taskId) { | ||||
|         // incoming messages must correspond to this export instance | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     if (message.type === 'export-progress-count') { | ||||
|     if (message.type === 'task-progress-count' && message.taskType === 'export') { | ||||
|         $exportProgressWrapper.slideDown(); | ||||
|  | ||||
|         $exportProgressCount.text(message.progressCount); | ||||
|     } | ||||
|     else if (message.type === 'export-finished') { | ||||
|     else if (message.type === 'task-succeeded' && message.taskType === 'export') { | ||||
|         $dialog.modal('hide'); | ||||
|  | ||||
|         infoService.showMessage("Export finished successfully."); | ||||
|   | ||||
| @@ -4,64 +4,24 @@ const tarExportService = require('../../services/export/tar'); | ||||
| const singleExportService = require('../../services/export/single'); | ||||
| const opmlExportService = require('../../services/export/opml'); | ||||
| const repository = require("../../services/repository"); | ||||
| const ws = require("../../services/ws.js"); | ||||
| const TaskContext = require("../../services/task_context"); | ||||
| const log = require("../../services/log"); | ||||
|  | ||||
| class ExportContext { | ||||
|     constructor(exportId) { | ||||
|         // exportId is to distinguish between different export events - it is possible (though not recommended) | ||||
|         // to have multiple exports going at the same time | ||||
|         this.exportId = exportId; | ||||
|         // count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress | ||||
|         this.progressCount = 0; | ||||
|         this.lastSentCountTs = Date.now(); | ||||
|     } | ||||
|  | ||||
|     async increaseProgressCount() { | ||||
|         this.progressCount++; | ||||
|  | ||||
|         if (Date.now() - this.lastSentCountTs >= 500) { | ||||
|             this.lastSentCountTs = Date.now(); | ||||
|  | ||||
|             await ws.sendMessageToAllClients({ | ||||
|                 exportId: this.exportId, | ||||
|                 type: 'export-progress-count', | ||||
|                 progressCount: this.progressCount | ||||
|             }); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     async exportFinished() { | ||||
|         await ws.sendMessageToAllClients({ | ||||
|             exportId: this.exportId, | ||||
|             type: 'export-finished' | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     // must remaing non-static | ||||
|     async reportError(message) { | ||||
|         await ws.sendMessageToAllClients({ | ||||
|             type: 'export-error', | ||||
|             message: message | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|  | ||||
| async function exportBranch(req, res) { | ||||
|     const {branchId, type, format, version, exportId} = req.params; | ||||
|     const {branchId, type, format, version, taskId} = req.params; | ||||
|     const branch = await repository.getBranch(branchId); | ||||
|  | ||||
|     const exportContext = new ExportContext(exportId); | ||||
|     const taskContext = new TaskContext(taskId, 'export'); | ||||
|  | ||||
|     try { | ||||
|         if (type === 'subtree' && (format === 'html' || format === 'markdown')) { | ||||
|             await tarExportService.exportToTar(exportContext, branch, format, res); | ||||
|             await tarExportService.exportToTar(taskContext, branch, format, res); | ||||
|         } | ||||
|         else if (type === 'single') { | ||||
|             await singleExportService.exportSingleNote(exportContext, branch, format, res); | ||||
|             await singleExportService.exportSingleNote(taskContext, branch, format, res); | ||||
|         } | ||||
|         else if (format === 'opml') { | ||||
|             await opmlExportService.exportToOpml(exportContext, branch, version, res); | ||||
|             await opmlExportService.exportToOpml(taskContext, branch, version, res); | ||||
|         } | ||||
|         else { | ||||
|             return [404, "Unrecognized export format " + format]; | ||||
| @@ -69,7 +29,7 @@ async function exportBranch(req, res) { | ||||
|     } | ||||
|     catch (e) { | ||||
|         const message = "Export failed with following error: '" + e.message + "'. More details might be in the logs."; | ||||
|         exportContext.reportError(message); | ||||
|         taskContext.reportError(message); | ||||
|  | ||||
|         log.error(message + e.stack); | ||||
|  | ||||
|   | ||||
| @@ -3,6 +3,8 @@ | ||||
| const noteService = require('../../services/notes'); | ||||
| const treeService = require('../../services/tree'); | ||||
| const repository = require('../../services/repository'); | ||||
| const utils = require('../../services/utils'); | ||||
| const TaskContext = require('../../services/task_context'); | ||||
|  | ||||
| async function getNote(req) { | ||||
|     const noteId = req.params.noteId; | ||||
| @@ -76,8 +78,10 @@ async function deleteNote(req) { | ||||
|  | ||||
|     const note = await repository.getNote(noteId); | ||||
|  | ||||
|     const taskContext = new TaskContext(utils.randomString(10), 'delete-note'); | ||||
|  | ||||
|     for (const branch of await note.getBranches()) { | ||||
|         await noteService.deleteBranch(branch); | ||||
|         await noteService.deleteBranch(branch, taskContext); | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -141,7 +141,7 @@ function register(app) { | ||||
|     apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent); | ||||
|     apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter); | ||||
|  | ||||
|     route(GET, '/api/notes/:branchId/export/:type/:format/:version/:exportId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch); | ||||
|     route(GET, '/api/notes/:branchId/export/:type/:format/:version/:taskId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch); | ||||
|     route(POST, '/api/notes/:parentNoteId/import', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware], importRoute.importToBranch, apiResultHandler); | ||||
|  | ||||
|     route(POST, '/api/notes/:parentNoteId/upload', [auth.checkApiAuthOrElectron, uploadMiddleware, csrfMiddleware], | ||||
|   | ||||
| @@ -3,7 +3,7 @@ | ||||
| const repository = require("../repository"); | ||||
| const utils = require('../utils'); | ||||
|  | ||||
| async function exportToOpml(exportContext, branch, version, res) { | ||||
| async function exportToOpml(taskContext, branch, version, res) { | ||||
|     if (!['1.0', '2.0'].includes(version)) { | ||||
|         throw new Error("Unrecognized OPML version " + version); | ||||
|     } | ||||
| @@ -38,7 +38,7 @@ async function exportToOpml(exportContext, branch, version, res) { | ||||
|             throw new Error("Unrecognized OPML version " + opmlVersion); | ||||
|         } | ||||
|  | ||||
|         exportContext.increaseProgressCount(); | ||||
|         taskContext.increaseProgressCount(); | ||||
|  | ||||
|         for (const child of await note.getChildBranches()) { | ||||
|             await exportNoteInner(child.branchId); | ||||
| @@ -66,7 +66,7 @@ async function exportToOpml(exportContext, branch, version, res) { | ||||
| </opml>`); | ||||
|     res.end(); | ||||
|  | ||||
|     exportContext.exportFinished(); | ||||
|     taskContext.taskSucceeded(); | ||||
| } | ||||
|  | ||||
| function prepareText(text) { | ||||
|   | ||||
| @@ -5,7 +5,7 @@ const html = require('html'); | ||||
| const utils = require('../utils'); | ||||
| const mdService = require('./md'); | ||||
|  | ||||
| async function exportSingleNote(exportContext, branch, format, res) { | ||||
| async function exportSingleNote(taskContext, branch, format, res) { | ||||
|     const note = await branch.getNote(); | ||||
|  | ||||
|     if (note.type === 'image' || note.type === 'file') { | ||||
| @@ -54,8 +54,8 @@ async function exportSingleNote(exportContext, branch, format, res) { | ||||
|  | ||||
|     res.send(payload); | ||||
|  | ||||
|     exportContext.increaseProgressCount(); | ||||
|     exportContext.exportFinished(); | ||||
|     taskContext.increaseProgressCount(); | ||||
|     taskContext.taskSucceeded(); | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   | ||||
| @@ -12,11 +12,11 @@ const protectedSessionService = require('../protected_session'); | ||||
| const sanitize = require("sanitize-filename"); | ||||
|  | ||||
| /** | ||||
|  * @param {ExportContext} exportContext | ||||
|  * @param {TaskContext} taskContext | ||||
|  * @param {Branch} branch | ||||
|  * @param {string} format - 'html' or 'markdown' | ||||
|  */ | ||||
| async function exportToTar(exportContext, branch, format, res) { | ||||
| async function exportToTar(taskContext, branch, format, res) { | ||||
|     const pack = tar.pack(); | ||||
|  | ||||
|     const noteIdToMeta = {}; | ||||
| @@ -124,7 +124,7 @@ async function exportToTar(exportContext, branch, format, res) { | ||||
|             }) | ||||
|         }; | ||||
|  | ||||
|         exportContext.increaseProgressCount(); | ||||
|         taskContext.increaseProgressCount(); | ||||
|  | ||||
|         if (note.type === 'text') { | ||||
|             meta.format = format; | ||||
| @@ -268,7 +268,7 @@ ${content} | ||||
|             pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content); | ||||
|         } | ||||
|  | ||||
|         exportContext.increaseProgressCount(); | ||||
|         taskContext.increaseProgressCount(); | ||||
|  | ||||
|         if (noteMeta.children && noteMeta.children.length > 0) { | ||||
|             const directoryPath = filePathPrefix + noteMeta.dirFileName; | ||||
| @@ -315,7 +315,7 @@ ${content} | ||||
|  | ||||
|     pack.pipe(res); | ||||
|  | ||||
|     exportContext.exportFinished(); | ||||
|     taskContext.taskSucceeded(); | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|   | ||||
| @@ -390,7 +390,9 @@ async function updateNote(noteId, noteUpdates) { | ||||
| } | ||||
|  | ||||
| /** @return {boolean} - true if note has been deleted, false otherwise */ | ||||
| async function deleteBranch(branch) { | ||||
| async function deleteBranch(branch, taskContext) { | ||||
|     taskContext.increaseProgressCount(); | ||||
|  | ||||
|     if (!branch || branch.isDeleted) { | ||||
|         return false; | ||||
|     } | ||||
| @@ -413,7 +415,7 @@ async function deleteBranch(branch) { | ||||
|         await note.save(); | ||||
|  | ||||
|         for (const childBranch of await note.getChildBranches()) { | ||||
|             await deleteBranch(childBranch); | ||||
|             await deleteBranch(childBranch, taskContext); | ||||
|         } | ||||
|  | ||||
|         for (const attribute of await note.getOwnedAttributes()) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user