Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
protect/unprotect tree reports progress via notifications
zadam00bb1236ce20
diff --git a/src/public/javascripts/services/protected_session.js b/src/public/javascripts/services/protected_session.jsindex 1dec6a8947..860d7de140 100644--- a/src/public/javascripts/services/protected_session.js+++ b/src/public/javascripts/services/protected_session.js@@ -4,6 +4,7 @@ import utils from './utils.js';import server from './server.js';import protectedSessionHolder from './protected_session_holder.js';import infoService from "./info.js";+import ws from "./ws.js";const $enterProtectedSessionButton = $("#enter-protected-session-button");const $leaveProtectedSessionButton = $("#leave-protected-session-button");@@ -117,12 +118,39 @@ async function protectSubtree(noteId, protect) {await server.put('notes/' + noteId + "/protect/" + (protect ? 1 : 0));- infoService.showMessage("Request to un/protect sub tree has finished successfully");-treeService.reload();noteDetailService.reload();}+function makeToast(message, protectingLabel, text) {+ return {+ id: message.taskId,+ title: protectingLabel + " status",+ message: text,+ icon: message.data.protect ? "shield-check" : "shield-close"+ };+}++ws.subscribeToMessages(async message => {+ if (message.taskType !== 'protect-notes') {+ return;+ }++ const protectingLabel = message.data.protect ? "Protecting" : "Unprotecting";++ if (message.type === 'task-error') {+ infoService.closePersistent(message.taskId);+ infoService.showError(message.message);+ } else if (message.type === 'task-progress-count') {+ infoService.showPersistent(makeToast(message, protectingLabel,protectingLabel + " in progress: " + message.progressCount));+ } else if (message.type === 'task-succeeded') {+ const toast = makeToast(message, protectingLabel, protectingLabel + " finished successfully.");+ toast.closeAfter = 3000;++ infoService.showPersistent(toast);+ }+});+export default {protectSubtree,enterProtectedSession,diff --git a/src/routes/api/notes.js b/src/routes/api/notes.jsindex 6a2f7862ba..c9ee09b428 100644--- a/src/routes/api/notes.js+++ b/src/routes/api/notes.js@@ -102,7 +102,11 @@ async function protectSubtree(req) {const note = await repository.getNote(noteId);const protect = !!parseInt(req.params.isProtected);- await noteService.protectNoteRecursively(note, protect);+ const taskContext = new TaskContext(utils.randomString(10), 'protect-notes', {protect});++ await noteService.protectNoteRecursively(note, protect, taskContext);++ taskContext.taskSucceeded();}async function setNoteTypeMime(req) {diff --git a/src/services/notes.js b/src/services/notes.jsindex 7123f639e2..7678c7a23a 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -177,11 +177,13 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})return {note, branch};}-async function protectNoteRecursively(note, protect) {+async function protectNoteRecursively(note, protect, taskContext) {await protectNote(note, protect);+ taskContext.increaseProgressCount();+for (const child of await note.getChildNotes()) {- await protectNoteRecursively(child, protect);+ await protectNoteRecursively(child, protect, taskContext);}}diff --git a/src/services/task_context.js b/src/services/task_context.jsindex 018f9f5aa8..3dd4d7cfb0 100644--- a/src/services/task_context.js+++ b/src/services/task_context.js@@ -35,6 +35,7 @@ class TaskContext {type: 'task-progress-count',taskId: this.taskId,taskType: this.taskType,+ data: this.data,progressCount: this.progressCount});}@@ -45,6 +46,7 @@ class TaskContext {type: 'task-error',taskId: this.taskId,taskType: this.taskType,+ data: this.data,message: message});}@@ -54,6 +56,7 @@ class TaskContext {type: 'task-succeeded',taskId: this.taskId,taskType: this.taskType,+ data: this.data,result: result});}