From 724f4b43b7985403897d3d4b5508db1a1c501fdb Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 28 Oct 2017 13:19:12 -0400 Subject: [PATCH] added document_id for sync identification --- app.js | 5 +++++ migrations/0017__document_id.sql | 1 + services/migration.js | 2 +- services/sql.js | 9 +++++++-- services/sync.js | 17 +++++++++++++++++ services/utils.js | 2 +- 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 migrations/0017__document_id.sql diff --git a/app.js b/app.js index 520f26e72..fa50b982b 100644 --- a/app.js +++ b/app.js @@ -95,6 +95,11 @@ app.use((req, res, next) => { // error handler app.use((err, req, res, next) => { log.error(err.message); + + res.status(err.status || 500); + res.send({ + message: err.message + }); }); // triggers sync timer diff --git a/migrations/0017__document_id.sql b/migrations/0017__document_id.sql new file mode 100644 index 000000000..9bced5a9b --- /dev/null +++ b/migrations/0017__document_id.sql @@ -0,0 +1 @@ +INSERT INTO options (opt_name, opt_value) VALUES ('document_id', ''); \ No newline at end of file diff --git a/services/migration.js b/services/migration.js index db3f56549..86ebb49db 100644 --- a/services/migration.js +++ b/services/migration.js @@ -3,7 +3,7 @@ const sql = require('./sql'); const fs = require('fs-extra'); const log = require('./log'); -const APP_DB_VERSION = 16; +const APP_DB_VERSION = 17; const MIGRATIONS_DIR = "./migrations"; async function migrate() { diff --git a/services/sql.js b/services/sql.js index 0b8d0c07d..e1870f236 100644 --- a/services/sql.js +++ b/services/sql.js @@ -34,9 +34,14 @@ async function rollback() { } async function getOption(optName) { - const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]); + try { + const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]); - return row['opt_value']; + return row['opt_value']; + } + catch (e) { + throw new Error("Option " + optName + " doesn't exist"); + } } async function setOption(optName, optValue) { diff --git a/services/sync.js b/services/sync.js index c54efc49e..97c9d686c 100644 --- a/services/sync.js +++ b/services/sync.js @@ -112,6 +112,7 @@ async function sync() { async function getChangedSince(since) { return { + 'documentId': await getDocumentId(), 'syncTimestamp': utils.nowTimestamp(), 'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]), 'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]), @@ -167,6 +168,22 @@ async function putNote(note) { log.info("Update/sync note " + note.detail.note_id); } +let documentIdCache = null; + +async function getDocumentId() { + if (!documentIdCache) { + documentIdCache = await sql.getOption('document_id'); + + if (!documentIdCache) { + documentIdCache = utils.randomString(16); + + await sql.setOption('document_id', documentIdCache); + } + } + + return documentIdCache; +} + if (SYNC_SERVER) { log.info("Setting up sync"); diff --git a/services/utils.js b/services/utils.js index 1c3ef3253..ba4548e77 100644 --- a/services/utils.js +++ b/services/utils.js @@ -6,7 +6,7 @@ function newNoteId() { const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; -function randomString(length, chars) { +function randomString(length) { let result = ''; for (let i = length; i > 0; --i) {