fixes for dates in sync

This commit is contained in:
azivner 2017-12-10 15:45:17 -05:00
parent cba457bd61
commit f68ffe1581
5 changed files with 19 additions and 27 deletions

View File

@ -119,7 +119,7 @@ CREATE TABLE `source_ids` (
DROP TABLE recent_notes; DROP TABLE recent_notes;
CREATE TABLE `recent_notes` ( CREATE TABLE `recent_notes` (
'note_tree_id'TEXT NOT NULL PRIMARY KEY, `note_tree_id` TEXT NOT NULL PRIMARY KEY,
`note_path` TEXT NOT NULL, `note_path` TEXT NOT NULL,
`date_accessed` TEXT NOT NULL, `date_accessed` TEXT NOT NULL,
is_deleted INT is_deleted INT

View File

@ -11,11 +11,15 @@ const protected_session = require('../../services/protected_session');
const app_info = require('../../services/app_info'); const app_info = require('../../services/app_info');
router.post('/sync', async (req, res, next) => { router.post('/sync', async (req, res, next) => {
const timestamp = req.body.timestamp; const timestampStr = req.body.timestamp;
const now = utils.nowTimestamp(); console.log(req.body);
if (Math.abs(timestamp - now) > 5) { const timestamp = utils.parseDate(timestampStr);
const now = new Date();
if (Math.abs(timestamp.getTime() - now.getTime()) > 5000) {
res.status(400); res.status(400);
res.send({ message: 'Auth request time is out of sync' }); res.send({ message: 'Auth request time is out of sync' });
} }
@ -28,13 +32,13 @@ router.post('/sync', async (req, res, next) => {
} }
const documentSecret = await options.getOption('document_secret'); const documentSecret = await options.getOption('document_secret');
const expectedHash = utils.hmac(documentSecret, timestamp); const expectedHash = utils.hmac(documentSecret, timestampStr);
const givenHash = req.body.hash; const givenHash = req.body.hash;
if (expectedHash !== givenHash) { if (expectedHash !== givenHash) {
res.status(400); res.status(400);
res.send({ message: "Hash doesn't match" }); res.send({ message: "Sync login hash doesn't match" });
} }
req.session.loggedIn = true; req.session.loggedIn = true;

View File

@ -84,7 +84,7 @@ async function sync() {
} }
async function login() { async function login() {
const timestamp = utils.nowTimestamp(); const timestamp = utils.nowDate();
const documentSecret = await options.getOption('document_secret'); const documentSecret = await options.getOption('document_secret');
const hash = utils.hmac(documentSecret, timestamp); const hash = utils.hmac(documentSecret, timestamp);
@ -135,8 +135,7 @@ async function pullSync(syncContext) {
if (!resp) { if (!resp) {
log.error("Empty response to pull for " + sync.entity_name + ", id=" + sync.entity_id); log.error("Empty response to pull for " + sync.entity_name + ", id=" + sync.entity_id);
} }
else if (sync.entity_name === 'notes') {
if (sync.entity_name === 'notes') {
await syncUpdate.updateNote(resp.entity, syncContext.sourceId); await syncUpdate.updateNote(resp.entity, syncContext.sourceId);
} }
else if (sync.entity_name === 'notes_tree') { else if (sync.entity_name === 'notes_tree') {

View File

@ -20,7 +20,7 @@ async function updateNote(entity, sourceId) {
log.info("Update/sync note " + entity.note_id); log.info("Update/sync note " + entity.note_id);
} }
else { else {
await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note <note>, " + utils.formatTwoTimestamps(origNote.date_modified, entity.date_modified)); await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note <note>, " + utils.formatTwoDates(origNote.date_modified, entity.date_modified));
} }
} }
@ -38,7 +38,7 @@ async function updateNoteTree(entity, sourceId) {
log.info("Update/sync note tree " + entity.note_tree_id); log.info("Update/sync note tree " + entity.note_tree_id);
} }
else { else {
await eventLog.addNoteEvent(entity.note_tree_id, "Sync conflict in note tree <note>, " + utils.formatTwoTimestamps(orig.date_modified, entity.date_modified)); await eventLog.addNoteEvent(entity.note_tree_id, "Sync conflict in note tree <note>, " + utils.formatTwoDates(orig.date_modified, entity.date_modified));
} }
}); });
} }
@ -55,7 +55,7 @@ async function updateNoteHistory(entity, sourceId) {
log.info("Update/sync note history " + entity.note_history_id); log.info("Update/sync note history " + entity.note_history_id);
} }
else { else {
await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note history for <note>, " + utils.formatTwoTimestamps(orig.date_modified_to, entity.date_modified_to)); await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note history for <note>, " + utils.formatTwoDates(orig.date_modified_to, entity.date_modified_to));
} }
}); });
} }
@ -86,7 +86,7 @@ async function updateOptions(entity, sourceId) {
await eventLog.addEvent("Synced option " + entity.opt_name); await eventLog.addEvent("Synced option " + entity.opt_name);
} }
else { else {
await eventLog.addEvent("Sync conflict in options for " + entity.opt_name + ", " + utils.formatTwoTimestamps(orig.date_modified, entity.date_modified)); await eventLog.addEvent("Sync conflict in options for " + entity.opt_name + ", " + utils.formatTwoDates(orig.date_modified, entity.date_modified));
} }
}); });
} }

View File

@ -23,10 +23,6 @@ function randomSecureToken(bytes = 32) {
return crypto.randomBytes(bytes).toString('base64'); return crypto.randomBytes(bytes).toString('base64');
} }
function nowTimestamp() {
return Math.floor(Date.now() / 1000);
}
function nowDate() { function nowDate() {
return dateStr(new Date()); return dateStr(new Date());
} }
@ -66,14 +62,8 @@ function isElectron() {
return !!process.versions['electron']; return !!process.versions['electron'];
} }
function formatDateTimeFromTS(timestamp) { function formatTwoDates(origDate, newDate) {
const date = new Date(timestamp * 1000); return "orig: " + origDate + ", new: " + newDate;
return date.toISOString();
}
function formatTwoTimestamps(origTS, newTS) {
return "orig: " + formatDateTimeFromTS(origTS) + ", new: " + formatDateTimeFromTS(newTS);
} }
function hash(text) { function hash(text) {
@ -88,7 +78,6 @@ function isEmptyOrWhitespace(str) {
module.exports = { module.exports = {
randomSecureToken, randomSecureToken,
randomString, randomString,
nowTimestamp,
nowDate, nowDate,
dateStr, dateStr,
parseDate, parseDate,
@ -99,7 +88,7 @@ module.exports = {
fromBase64, fromBase64,
hmac, hmac,
isElectron, isElectron,
formatTwoTimestamps, formatTwoDates,
hash, hash,
isEmptyOrWhitespace isEmptyOrWhitespace
}; };