diff --git a/routes/api/sync.js b/routes/api/sync.js index 4ab84ebf8..1cf872181 100644 --- a/routes/api/sync.js +++ b/routes/api/sync.js @@ -17,7 +17,7 @@ router.post('/now', auth.checkApiAuth, async (req, res, next) => { router.get('/changed', auth.checkApiAuth, async (req, res, next) => { const lastSyncId = parseInt(req.query.lastSyncId); - const sourceId = parseInt(req.query.sourceId); + const sourceId = req.query.sourceId; const result = await sync.getChanged(lastSyncId, sourceId); diff --git a/services/auth.js b/services/auth.js index 8fbd09ebc..2d5404acd 100644 --- a/services/auth.js +++ b/services/auth.js @@ -25,19 +25,19 @@ async function checkAuthWithoutMigration(req, res, next) { async function checkApiAuth(req, res, next) { if (!req.session.loggedIn) { - res.status(401).send({}); + res.status(401).send("Not authorized"); } else if (await migration.isDbUpToDate()) { next(); } else { - res.status(409).send({}); // need better response than that + res.status(409).send("Mismatched app versions"); // need better response than that } } async function checkApiAuthWithoutMigration(req, res, next) { if (!req.session.loggedIn) { - res.status(401).send({}); + res.status(401).send("Not authorized"); } else { next(); diff --git a/services/sync.js b/services/sync.js index d04901b5f..3dea51fd3 100644 --- a/services/sync.js +++ b/services/sync.js @@ -19,6 +19,8 @@ async function pullSync(cookieJar, syncLog) { let syncRows; try { + logSync("Pulling changes: " + SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID); + syncRows = await rp({ uri: SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID, jar: cookieJar, @@ -45,24 +47,24 @@ async function pullSync(cookieJar, syncLog) { throw new Error("Can't pull " + sync.entity_name + " " + sync.entity_id + ", inner exception: " + e.stack); } - await sql.doInTransaction(async () => { - if (sync.entity_name === 'notes') { - await updateNote(resp.entity, resp.links, sync.source_id, syncLog) - } - else if (sync.entity_name === 'notes_tree') { - await updateNoteTree(resp.entity, sync.source_id, syncLog) - } - else if (sync.entity_name === 'notes_history') { - await updateNoteHistory(resp.entity, sync.source_id, syncLog) - } - else { - logSync("Unrecognized entity type " + sync.entity_name, syncLog); + // TODO: ideally this should be in transaction - throw new Error("Unrecognized entity type " + sync.entity_name); - } + if (sync.entity_name === 'notes') { + await updateNote(resp.entity, resp.links, sync.source_id, syncLog) + } + else if (sync.entity_name === 'notes_tree') { + await updateNoteTree(resp, sync.source_id, syncLog) + } + else if (sync.entity_name === 'notes_history') { + await updateNoteHistory(resp, sync.source_id, syncLog) + } + else { + logSync("Unrecognized entity type " + sync.entity_name, syncLog); - await sql.setOption('last_synced_pull', sync.id); - }); + throw new Error("Unrecognized entity type " + sync.entity_name); + } + + await sql.setOption('last_synced_pull', sync.id); } logSync("Finished pull"); @@ -210,6 +212,8 @@ function logSync(message, syncLog) { } async function getChanged(lastSyncId, sourceId) { + console.log([lastSyncId, sourceId]); + return await sql.getResults("SELECT * FROM sync WHERE id > ? AND source_id != ?", [lastSyncId, sourceId]); }