From 4ff7e0813dfabfa6801357cf290577716306b6db Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 24 Apr 2021 11:39:44 +0200 Subject: [PATCH] upload of modified open file WIP --- src/public/app/dialogs/export.js | 6 ++-- src/public/app/services/branches.js | 14 ++++----- src/public/app/services/file_watcher.js | 31 ++++++++++++++++++++ src/public/app/services/import.js | 6 ++-- src/public/app/services/protected_session.js | 8 ++--- src/public/app/services/tree.js | 2 +- src/public/app/services/ws.js | 2 +- src/routes/api/clipper.js | 2 +- src/routes/api/notes.js | 30 +++++++++++++++++-- src/routes/routes.js | 1 + src/services/task_context.js | 6 ++-- 11 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 src/public/app/services/file_watcher.js diff --git a/src/public/app/dialogs/export.js b/src/public/app/dialogs/export.js index bd1892044..690d43086 100644 --- a/src/public/app/dialogs/export.js +++ b/src/public/app/dialogs/export.js @@ -121,14 +121,14 @@ ws.subscribeToMessages(async message => { return; } - if (message.type === 'task-error') { + if (message.type === 'taskError') { toastService.closePersistent(message.taskId); toastService.showError(message.message); } - else if (message.type === 'task-progress-count') { + else if (message.type === 'taskProgressCount') { toastService.showPersistent(makeToast(message.taskId, "Export in progress: " + message.progressCount)); } - else if (message.type === 'task-succeeded') { + else if (message.type === 'taskSucceeded') { const toast = makeToast(message.taskId, "Export finished successfully."); toast.closeAfter = 5000; diff --git a/src/public/app/services/branches.js b/src/public/app/services/branches.js index 8c7c482d0..f001abdee 100644 --- a/src/public/app/services/branches.js +++ b/src/public/app/services/branches.js @@ -153,12 +153,12 @@ ws.subscribeToMessages(async message => { return; } - if (message.type === 'task-error') { + if (message.type === 'taskError') { toastService.closePersistent(message.taskId); toastService.showError(message.message); - } else if (message.type === 'task-progress-count') { + } else if (message.type === 'taskProgressCount') { toastService.showPersistent(makeToast(message.taskId, "Delete notes in progress: " + message.progressCount)); - } else if (message.type === 'task-succeeded') { + } else if (message.type === 'taskSucceeded') { const toast = makeToast(message.taskId, "Delete finished successfully."); toast.closeAfter = 5000; @@ -167,16 +167,16 @@ ws.subscribeToMessages(async message => { }); ws.subscribeToMessages(async message => { - if (message.taskType !== 'undelete-notes') { + if (message.taskType !== 'undeleteNotes') { return; } - if (message.type === 'task-error') { + if (message.type === 'taskError') { toastService.closePersistent(message.taskId); toastService.showError(message.message); - } else if (message.type === 'task-progress-count') { + } else if (message.type === 'taskProgressCount') { toastService.showPersistent(makeToast(message.taskId, "Undeleting notes in progress: " + message.progressCount)); - } else if (message.type === 'task-succeeded') { + } else if (message.type === 'taskSucceeded') { const toast = makeToast(message.taskId, "Undeleting notes finished successfully."); toast.closeAfter = 5000; diff --git a/src/public/app/services/file_watcher.js b/src/public/app/services/file_watcher.js new file mode 100644 index 000000000..2d53ce3c5 --- /dev/null +++ b/src/public/app/services/file_watcher.js @@ -0,0 +1,31 @@ +import ws from "./ws.js"; +import appContext from "./app_context.js"; + +const fileModificationStatus = {}; + +function getFileModificationStatus(noteId) { + return fileModificationStatus[noteId]; +} + +function fileModificationUploaded(noteId) { + delete fileModificationStatus[noteId]; +} + +ws.subscribeToMessages(async message => { + if (message.type !== 'openedFileUpdated') { + return; + } + + fileModificationStatus[message.noteId] = message; + + appContext.triggerEvent('openedFileUpdated', { + noteId: message.noteId, + lastModifiedMs: message.lastModifiedMs, + filePath: message.filePath + }); +}); + +export default { + getFileModificationStatus, + fileModificationUploaded +} diff --git a/src/public/app/services/import.js b/src/public/app/services/import.js index 367d4ffea..fcc8c1b1d 100644 --- a/src/public/app/services/import.js +++ b/src/public/app/services/import.js @@ -52,12 +52,12 @@ ws.subscribeToMessages(async message => { return; } - if (message.type === 'task-error') { + if (message.type === 'taskError') { toastService.closePersistent(message.taskId); toastService.showError(message.message); - } else if (message.type === 'task-progress-count') { + } else if (message.type === 'taskProgressCount') { toastService.showPersistent(makeToast(message.taskId, "Import in progress: " + message.progressCount)); - } else if (message.type === 'task-succeeded') { + } else if (message.type === 'taskSucceeded') { const toast = makeToast(message.taskId, "Import finished successfully."); toast.closeAfter = 5000; diff --git a/src/public/app/services/protected_session.js b/src/public/app/services/protected_session.js index 46b83b5a3..42550a14e 100644 --- a/src/public/app/services/protected_session.js +++ b/src/public/app/services/protected_session.js @@ -89,18 +89,18 @@ function makeToast(message, protectingLabel, text) { } ws.subscribeToMessages(async message => { - if (message.taskType !== 'protect-notes') { + if (message.taskType !== 'protectNotes') { return; } const protectingLabel = message.data.protect ? "Protecting" : "Unprotecting"; - if (message.type === 'task-error') { + if (message.type === 'taskError') { toastService.closePersistent(message.taskId); toastService.showError(message.message); - } else if (message.type === 'task-progress-count') { + } else if (message.type === 'taskProgressCount') { toastService.showPersistent(makeToast(message, protectingLabel,protectingLabel + " in progress: " + message.progressCount)); - } else if (message.type === 'task-succeeded') { + } else if (message.type === 'taskSucceeded') { const toast = makeToast(message, protectingLabel, protectingLabel + " finished successfully."); toast.closeAfter = 3000; diff --git a/src/public/app/services/tree.js b/src/public/app/services/tree.js index 1d7f40fb0..07c6badae 100644 --- a/src/public/app/services/tree.js +++ b/src/public/app/services/tree.js @@ -127,7 +127,7 @@ async function sortAlphabetically(noteId) { } ws.subscribeToMessages(message => { - if (message.type === 'open-note') { + if (message.type === 'openNote') { appContext.tabManager.activateOrOpenNote(message.noteId); if (utils.isElectron()) { diff --git a/src/public/app/services/ws.js b/src/public/app/services/ws.js index e2b171566..4a26de88b 100644 --- a/src/public/app/services/ws.js +++ b/src/public/app/services/ws.js @@ -59,7 +59,7 @@ async function handleMessage(event) { } if (message.type === 'frontend-update') { - let {entityChanges, lastSyncedPush} = message.data; + let {entityChanges} = message.data; lastPingTs = Date.now(); if (entityChanges.length > 0) { diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index 257e06bff..eeef65034 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -144,7 +144,7 @@ function processContent(images, note, content) { function openNote(req) { if (utils.isElectron()) { ws.sendMessageToAllClients({ - type: 'open-note', + type: 'openNote', noteId: req.params.noteId }); diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index 60eb242d9..a8df5b166 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -7,6 +7,8 @@ const sql = require('../../services/sql'); const utils = require('../../services/utils'); const log = require('../../services/log'); const TaskContext = require('../../services/task_context'); +const fs = require('fs'); +const noteRevisionService = require("../../services/note_revisions.js"); function getNote(req) { const noteId = req.params.noteId; @@ -80,7 +82,7 @@ function deleteNote(req) { function undeleteNote(req) { const note = repository.getNote(req.params.noteId); - const taskContext = TaskContext.getInstance(utils.randomString(10), 'undelete-notes'); + const taskContext = TaskContext.getInstance(utils.randomString(10), 'undeleteNotes'); noteService.undeleteNote(note, note.deleteId, taskContext); @@ -109,7 +111,7 @@ function protectNote(req) { const protect = !!parseInt(req.params.isProtected); const includingSubTree = !!parseInt(req.query.subtree); - const taskContext = new TaskContext(utils.randomString(10), 'protect-notes', {protect}); + const taskContext = new TaskContext(utils.randomString(10), 'protectNotes', {protect}); noteService.protectNoteRecursively(note, protect, includingSubTree, taskContext); @@ -273,6 +275,27 @@ function getDeleteNotesPreview(req) { }; } +function uploadModifiedFile(req) { + const noteId = req.params.noteId; + const {filePath} = req.body; + + const note = repository.getNote(noteId); + + if (!note) { + return [404, `Note ${noteId} has not been found`]; + } + + noteRevisionService.createNoteRevision(note); + + const fileContent = fs.readFileSync(filePath); + + if (!fileContent) { + return [400, `File ${fileContent} is empty`]; + } + + note.setContent(fileContent); +} + module.exports = { getNote, updateNote, @@ -286,5 +309,6 @@ module.exports = { changeTitle, duplicateSubtree, eraseDeletedNotesNow, - getDeleteNotesPreview + getDeleteNotesPreview, + uploadModifiedFile }; diff --git a/src/routes/routes.js b/src/routes/routes.js index 137d7b509..4c9e9a6c7 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -165,6 +165,7 @@ function register(app) { apiRoute(POST, '/api/notes/erase-deleted-notes-now', notesApiRoute.eraseDeletedNotesNow); apiRoute(PUT, '/api/notes/:noteId/change-title', notesApiRoute.changeTitle); apiRoute(POST, '/api/notes/:noteId/duplicate/:parentNoteId', notesApiRoute.duplicateSubtree); + apiRoute(POST, '/api/notes/:noteId/upload-modified-file', notesApiRoute.uploadModifiedFile); apiRoute(GET, '/api/edited-notes/:date', noteRevisionsApiRoute.getEditedNotesOnDate); diff --git a/src/services/task_context.js b/src/services/task_context.js index 0edd3c0bb..403409647 100644 --- a/src/services/task_context.js +++ b/src/services/task_context.js @@ -38,7 +38,7 @@ class TaskContext { this.lastSentCountTs = Date.now(); ws.sendMessageToAllClients({ - type: 'task-progress-count', + type: 'taskProgressCount', taskId: this.taskId, taskType: this.taskType, data: this.data, @@ -49,7 +49,7 @@ class TaskContext { reportError(message) { ws.sendMessageToAllClients({ - type: 'task-error', + type: 'taskError', taskId: this.taskId, taskType: this.taskType, data: this.data, @@ -59,7 +59,7 @@ class TaskContext { taskSucceeded(result) { ws.sendMessageToAllClients({ - type: 'task-succeeded', + type: 'taskSucceeded', taskId: this.taskId, taskType: this.taskType, data: this.data,