diff --git a/routes/api/status.js b/routes/api/status.js index 9874044b1..44b116afb 100644 --- a/routes/api/status.js +++ b/routes/api/status.js @@ -16,8 +16,8 @@ router.post('', auth.checkApiAuth, async (req, res, next) => { const browserId = req.get('x-browser-id'); const noteTreeChangesCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " + - "AND date_modified >= ? AND category IN (?, ?, ?)", [browserId, treeLoadTime, - audit_category.UPDATE_TITLE, audit_category.CHANGE_PARENT, audit_category.CHANGE_POSITION]); + "AND date_modified >= ? AND category IN (?, ?, ?, ?)", [browserId, treeLoadTime, + audit_category.UPDATE_TITLE, audit_category.CHANGE_PARENT, audit_category.CHANGE_POSITION, audit_category.DELETE_NOTE]); const currentNoteChangesCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " + "AND date_modified >= ? AND note_id = ? AND category IN (?, ?)", [browserId, currentNoteLoadTime, currentNoteId, diff --git a/routes/api/tree.js b/routes/api/tree.js index 664dc9445..2b4a3dfea 100644 --- a/routes/api/tree.js +++ b/routes/api/tree.js @@ -6,6 +6,7 @@ const sql = require('../../services/sql'); const options = require('../../services/options'); const utils = require('../../services/utils'); const auth = require('../../services/auth'); +const log = require('../../services/log'); router.get('/', auth.checkApiAuth, async (req, res, next) => { const notes = await sql.getResults("select " @@ -24,33 +25,37 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { const notes_map = {}; for (const note of notes) { - note['children'] = []; + note.children = []; - if (!note['note_pid']) { + if (!note.note_pid) { root_notes.push(note); } - notes_map[note['note_id']] = note; + notes_map[note.note_id] = note; } for (const note of notes) { - if (note['note_pid'] !== "") { - const parent = notes_map[note['note_pid']]; + if (note.note_pid !== "") { + const parent = notes_map[note.note_pid]; - parent['children'].push(note); - parent['folder'] = true; + if (!parent) { + log.error("Parent " + note.note_pid + ' has not been found'); + continue; + } + parent.children.push(note); + parent.folder = true; } } res.send({ - 'notes': root_notes, - 'start_note_id': await options.getOption('start_node'), - 'password_verification_salt': await options.getOption('password_verification_salt'), - 'password_derived_key_salt': await options.getOption('password_derived_key_salt'), - 'encrypted_data_key': await options.getOption('encrypted_data_key'), - 'encryption_session_timeout': await options.getOption('encryption_session_timeout'), - 'browser_id': utils.randomString(12), - 'tree_load_time': utils.nowTimestamp() + notes: root_notes, + start_note_id: await options.getOption('start_node'), + password_verification_salt: await options.getOption('password_verification_salt'), + password_derived_key_salt: await options.getOption('password_derived_key_salt'), + encrypted_data_key: await options.getOption('encrypted_data_key'), + encryption_session_timeout: await options.getOption('encryption_session_timeout'), + browser_id: utils.randomString(12), + tree_load_time: utils.nowTimestamp() }); }); diff --git a/services/notes.js b/services/notes.js index 8805f931f..b62fe8651 100644 --- a/services/notes.js +++ b/services/notes.js @@ -37,6 +37,8 @@ async function createNewNote(parentNoteId, note, browserId) { await sql.doInTransaction(async () => { await sql.addAudit(audit_category.CREATE_NOTE, browserId, noteId); + await sql.addNoteTreeSync(noteId, browserId); + await sql.addNoteSync(noteId, browserId); const now = utils.nowTimestamp(); @@ -127,25 +129,28 @@ async function updateNote(noteId, newNote, browserId) { await sql.insert("links", link); } + await sql.addNoteTreeSync(noteId); await sql.addNoteSync(noteId); }); } async function addNoteAudits(origNote, newNote, browserId) { - const noteId = origNote.note_id; + const noteId = newNote.note_id; - if (newNote.note_title !== origNote.note_title) { + if (!origNote || newNote.note_title !== origNote.note_title) { await sql.deleteRecentAudits(audit_category.UPDATE_TITLE, browserId, noteId); await sql.addAudit(audit_category.UPDATE_TITLE, browserId, noteId); } - if (newNote.note_text !== origNote.note_text) { + if (!origNote || newNote.note_text !== origNote.note_text) { await sql.deleteRecentAudits(audit_category.UPDATE_CONTENT, browserId, noteId); await sql.addAudit(audit_category.UPDATE_CONTENT, browserId, noteId); } - if (newNote.encryption !== origNote.encryption) { - await sql.addAudit(audit_category.ENCRYPTION, browserId, noteId, origNote.encryption, newNote.encryption); + if (!origNote || newNote.encryption !== origNote.encryption) { + const origEncryption = origNote ? origNote.encryption : null; + + await sql.addAudit(audit_category.ENCRYPTION, browserId, noteId, origEncryption, newNote.encryption); } } @@ -162,6 +167,9 @@ async function deleteNote(noteId, browserId) { await sql.execute("update notes_tree set is_deleted = 1, date_modified = ? where note_id = ?", [now, noteId]); await sql.execute("update notes set is_deleted = 1, date_modified = ? where note_id = ?", [now, noteId]); + await sql.addNoteTreeSync(noteId, browserId); + await sql.addNoteSync(noteId, browserId); + await sql.addAudit(audit_category.DELETE_NOTE, browserId, noteId); } diff --git a/services/sync.js b/services/sync.js index 77c9ce75a..1068c9b93 100644 --- a/services/sync.js +++ b/services/sync.js @@ -225,8 +225,10 @@ async function sync() { }; } + let syncContext; + try { - const syncContext = await login(); + syncContext = await login(); } catch (e) { if (e.message.indexOf('ECONNREFUSED') !== -1) {