From e53926738c9c69153a25601bb50f62daebcf1660 Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 16 Nov 2017 22:18:25 -0500 Subject: [PATCH] sync fixes --- ...0035__root_notes_will_have_note_id_root.sql | 1 + migrations/0036__clear_sync_table.sql | 1 + routes/api/notes_move.js | 18 +++++++++--------- routes/api/tree.js | 4 ++-- services/data_encryption.js | 4 ---- services/migration.js | 2 +- services/notes.js | 4 ---- services/source_id.js | 13 ++++++++----- services/sync.js | 2 ++ services/sync_table.js | 4 ++-- 10 files changed, 26 insertions(+), 27 deletions(-) create mode 100644 migrations/0035__root_notes_will_have_note_id_root.sql create mode 100644 migrations/0036__clear_sync_table.sql diff --git a/migrations/0035__root_notes_will_have_note_id_root.sql b/migrations/0035__root_notes_will_have_note_id_root.sql new file mode 100644 index 000000000..a6d7bedcd --- /dev/null +++ b/migrations/0035__root_notes_will_have_note_id_root.sql @@ -0,0 +1 @@ +UPDATE notes_tree SET note_pid = 'root' WHERE note_pid = '' \ No newline at end of file diff --git a/migrations/0036__clear_sync_table.sql b/migrations/0036__clear_sync_table.sql new file mode 100644 index 000000000..b6849242e --- /dev/null +++ b/migrations/0036__clear_sync_table.sql @@ -0,0 +1 @@ +DELETE FROM sync; \ No newline at end of file diff --git a/routes/api/notes_move.js b/routes/api/notes_move.js index 1ce454394..ec0e9688a 100644 --- a/routes/api/notes_move.js +++ b/routes/api/notes_move.js @@ -13,7 +13,7 @@ router.put('/:noteId/moveTo/:parentId', auth.checkApiAuth, async (req, res, next let parentId = req.params.parentId; const row = await sql.getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ? and is_deleted = 0', [parentId]); - const maxNotePos = row['max_note_pos']; + const maxNotePos = row.max_note_pos; let newNotePos = 0; if (maxNotePos === null) // no children yet @@ -44,16 +44,16 @@ router.put('/:noteId/moveBefore/:beforeNoteId', async (req, res, next) => { await sql.doInTransaction(async () => { // we don't change date_modified so other changes are prioritized in case of conflict await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos >= ? and is_deleted = 0", - [beforeNote['note_pid'], beforeNote['note_pos']]); + [beforeNote.note_pid, beforeNote.note_pos]); const now = utils.nowTimestamp(); await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", - [beforeNote['note_pid'], beforeNote['note_pos'], now, noteId]); + [beforeNote.note_pid, beforeNote.note_pos, now, noteId]); await sync_table.addNoteTreeSync(noteId); - await sync_table.addNoteReorderingSync(beforeNote['note_pid']); - await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), beforeNote['note_pid']); + await sync_table.addNoteReorderingSync(beforeNote.note_pid); + await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), beforeNote.note_pid); }); } @@ -70,16 +70,16 @@ router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => { await sql.doInTransaction(async () => { // we don't change date_modified so other changes are prioritized in case of conflict await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ? and is_deleted = 0", - [afterNote['note_pid'], afterNote['note_pos']]); + [afterNote.note_pid, afterNote.note_pos]); const now = utils.nowTimestamp(); await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?", - [afterNote['note_pid'], afterNote['note_pos'] + 1, now, noteId]); + [afterNote.note_pid, afterNote.note_pos + 1, now, noteId]); await sync_table.addNoteTreeSync(noteId); - await sync_table.addNoteReorderingSync(afterNote['note_pid']); - await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), afterNote['note_pid']); + await sync_table.addNoteReorderingSync(afterNote.note_pid); + await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), afterNote.note_pid); }); } diff --git a/routes/api/tree.js b/routes/api/tree.js index 6bcaa8e16..5129945fc 100644 --- a/routes/api/tree.js +++ b/routes/api/tree.js @@ -33,7 +33,7 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { note.children = []; - if (!note.note_pid) { + if (note.note_pid === "root") { root_notes.push(note); } @@ -41,7 +41,7 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { } for (const note of notes) { - if (note.note_pid !== "") { + if (note.note_pid !== "root") { const parent = notes_map[note.note_pid]; if (!parent) { diff --git a/services/data_encryption.js b/services/data_encryption.js index 82f0b86dd..7f3679dc5 100644 --- a/services/data_encryption.js +++ b/services/data_encryption.js @@ -104,10 +104,6 @@ function decryptCbc(key, iv, cipherText) { return "[protected]"; } - console.log("key:", key); - console.log("iv:", iv); - console.log("cipherText:", cipherText); - const decipher = crypto.createDecipheriv('aes-128-cbc', pad(key), pad(iv)); const cipherTextBuffer = Buffer.from(cipherText, 'base64'); diff --git a/services/migration.js b/services/migration.js index 64d2cd25c..8c0f50979 100644 --- a/services/migration.js +++ b/services/migration.js @@ -4,7 +4,7 @@ const options = require('./options'); const fs = require('fs-extra'); const log = require('./log'); -const APP_DB_VERSION = 34; +const APP_DB_VERSION = 36; const MIGRATIONS_DIR = "migrations"; async function migrate() { diff --git a/services/notes.js b/services/notes.js index 6faaddd09..78fafde8d 100644 --- a/services/notes.js +++ b/services/notes.js @@ -9,10 +9,6 @@ const sync_table = require('./sync_table'); async function createNewNote(parentNoteId, note, browserId) { const noteId = utils.newNoteId(); - if (parentNoteId === "root") { - parentNoteId = ""; - } - let newNotePos = 0; if (note.target === 'into') { diff --git a/services/source_id.js b/services/source_id.js index 34410d383..19767d017 100644 --- a/services/source_id.js +++ b/services/source_id.js @@ -9,12 +9,15 @@ log.info("Using sourceId=" + currentSourceId); let allSourceIds = []; sql.dbReady.then(async () => { - sql.insert("source_ids", { - source_id: currentSourceId, - date_created: utils.nowTimestamp() - }); + try { + sql.insert("source_ids", { + source_id: currentSourceId, + date_created: utils.nowTimestamp() + }); - allSourceIds = await sql.getFlattenedResults("source_id", "SELECT source_id FROM source_ids"); + allSourceIds = await sql.getFlattenedResults("source_id", "SELECT source_id FROM source_ids"); + } + catch (e) {} }); function isLocalSourceId(srcId) { diff --git a/services/sync.js b/services/sync.js index 4c54bef9e..f73d4032f 100644 --- a/services/sync.js +++ b/services/sync.js @@ -113,6 +113,8 @@ async function pullSync(syncContext) { continue; } + console.log("Pulling ", sync); + const resp = await syncRequest(syncContext, 'GET', "/api/sync/" + sync.entity_name + "/" + sync.entity_id); if (sync.entity_name === 'notes') { diff --git a/services/sync_table.js b/services/sync_table.js index cfcbd3447..fa7f8645f 100644 --- a/services/sync_table.js +++ b/services/sync_table.js @@ -10,8 +10,8 @@ async function addNoteTreeSync(noteId, sourceId) { await addEntitySync("notes_tree", noteId, sourceId) } -async function addNoteReorderingSync(noteId, sourceId) { - await addEntitySync("notes_reordering", noteId, sourceId) +async function addNoteReorderingSync(parentNoteId, sourceId) { + await addEntitySync("notes_reordering", parentNoteId, sourceId) } async function addNoteHistorySync(noteHistoryId, sourceId) {