From 00bb1236ce2025b63056601e0849773a4d418b06 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 19 Oct 2019 09:58:18 +0200 Subject: [PATCH] protect/unprotect tree reports progress via notifications --- .../javascripts/services/protected_session.js | 32 +++++++++++++++++-- src/routes/api/notes.js | 6 +++- src/services/notes.js | 6 ++-- src/services/task_context.js | 3 ++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/public/javascripts/services/protected_session.js b/src/public/javascripts/services/protected_session.js index 1dec6a894..860d7de14 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.js index 6a2f7862b..c9ee09b42 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.js index 7123f639e..7678c7a23 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.js index 018f9f5aa..3dd4d7cfb 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 }); }