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
fixes for dates in sync
azivnerf68ffe1581d6
diff --git a/migrations/0050__string_dates.sql b/migrations/0050__string_dates.sqlindex 6750f68952..499c6c3064 100644--- a/migrations/0050__string_dates.sql+++ b/migrations/0050__string_dates.sql@@ -119,7 +119,7 @@ CREATE TABLE `source_ids` (DROP 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,`date_accessed` TEXT NOT NULL,is_deleted INTdiff --git a/routes/api/login.js b/routes/api/login.jsindex 39565f3f91..2efb9cf46a 100644--- a/routes/api/login.js+++ b/routes/api/login.js@@ -11,11 +11,15 @@ const protected_session = require('../../services/protected_session');const app_info = require('../../services/app_info');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.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 expectedHash = utils.hmac(documentSecret, timestamp);+ const expectedHash = utils.hmac(documentSecret, timestampStr);const givenHash = req.body.hash;if (expectedHash !== givenHash) {res.status(400);- res.send({ message: "Hash doesn't match" });+ res.send({ message: "Sync login hash doesn't match" });}req.session.loggedIn = true;diff --git a/services/sync.js b/services/sync.jsindex e7e5719e56..1d1e658ad8 100644--- a/services/sync.js+++ b/services/sync.js@@ -84,7 +84,7 @@ async function sync() {}async function login() {- const timestamp = utils.nowTimestamp();+ const timestamp = utils.nowDate();const documentSecret = await options.getOption('document_secret');const hash = utils.hmac(documentSecret, timestamp);@@ -135,8 +135,7 @@ async function pullSync(syncContext) {if (!resp) {log.error("Empty response to pull for " + sync.entity_name + ", id=" + sync.entity_id);}-- if (sync.entity_name === 'notes') {+ else if (sync.entity_name === 'notes') {await syncUpdate.updateNote(resp.entity, syncContext.sourceId);}else if (sync.entity_name === 'notes_tree') {diff --git a/services/sync_update.js b/services/sync_update.jsindex 5b486cd284..f735614454 100644--- a/services/sync_update.js+++ b/services/sync_update.js@@ -20,7 +20,7 @@ async function updateNote(entity, sourceId) {log.info("Update/sync note " + entity.note_id);}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);}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);}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);}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));}});}diff --git a/services/utils.js b/services/utils.jsindex 4097aefe9a..09f5190d20 100644--- a/services/utils.js+++ b/services/utils.js@@ -23,10 +23,6 @@ function randomSecureToken(bytes = 32) {return crypto.randomBytes(bytes).toString('base64');}-function nowTimestamp() {- return Math.floor(Date.now() / 1000);-}-function nowDate() {return dateStr(new Date());}@@ -66,14 +62,8 @@ function isElectron() {return !!process.versions['electron'];}-function formatDateTimeFromTS(timestamp) {- const date = new Date(timestamp * 1000);-- return date.toISOString();-}--function formatTwoTimestamps(origTS, newTS) {- return "orig: " + formatDateTimeFromTS(origTS) + ", new: " + formatDateTimeFromTS(newTS);+function formatTwoDates(origDate, newDate) {+ return "orig: " + origDate + ", new: " + newDate;}function hash(text) {@@ -88,7 +78,6 @@ function isEmptyOrWhitespace(str) {module.exports = {randomSecureToken,randomString,- nowTimestamp,nowDate,dateStr,parseDate,@@ -99,7 +88,7 @@ module.exports = {fromBase64,hmac,isElectron,- formatTwoTimestamps,+ formatTwoDates,hash,isEmptyOrWhitespace};\ No newline at end of file