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
sync fixes
azivnere53926738c9c
diff --git a/migrations/0035__root_notes_will_have_note_id_root.sql b/migrations/0035__root_notes_will_have_note_id_root.sqlnew file mode 100644index 0000000000..a6d7bedcd7--- /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 filediff --git a/migrations/0036__clear_sync_table.sql b/migrations/0036__clear_sync_table.sqlnew file mode 100644index 0000000000..b6849242ec--- /dev/null+++ b/migrations/0036__clear_sync_table.sql@@ -0,0 +1 @@+DELETE FROM sync;\ No newline at end of filediff --git a/routes/api/notes_move.js b/routes/api/notes_move.jsindex 1ce454394a..ec0e9688af 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, nextlet 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 conflictawait 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 conflictawait 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.jsindex 6bcaa8e16a..5129945fc0 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.jsindex 82f0b86ddf..7f3679dc59 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.jsindex 64d2cd25c6..8c0f509799 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.jsindex 6faaddd09c..78fafde8d2 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.jsindex 34410d3839..19767d0172 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.jsindex 4c54bef9e4..f73d4032f6 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.jsindex cfcbd34478..fa7f8645f3 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) {