From 586d6b4557a12a43856c0f2244f07d46a7202429 Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 26 Apr 2020 14:26:57 +0200 Subject: [PATCH] in web version use local client time instead of server time for recording dateModified etc. --- src/public/app/services/server.js | 1 + src/public/app/services/tab_context.js | 1 + src/public/app/services/utils.js | 5 +++++ src/routes/routes.js | 3 +++ src/routes/setup.js | 3 ++- src/services/cls.js | 5 +++++ src/services/date_utils.js | 18 +++++++++++++++--- src/services/env.js | 2 +- 8 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/public/app/services/server.js b/src/public/app/services/server.js index 8167622ae..7d886eb4e 100644 --- a/src/public/app/services/server.js +++ b/src/public/app/services/server.js @@ -8,6 +8,7 @@ function getHeaders(headers) { // also avoiding using underscores instead of dashes since nginx filters them out by default const allHeaders = { 'trilium-source-id': glob.sourceId, + 'trilium-local-now-datetime': utils.localNowDateTime(), 'x-csrf-token': glob.csrfToken }; diff --git a/src/public/app/services/tab_context.js b/src/public/app/services/tab_context.js index dec5ebf0c..9d46e2f6d 100644 --- a/src/public/app/services/tab_context.js +++ b/src/public/app/services/tab_context.js @@ -58,6 +58,7 @@ class TabContext extends Component { this.autoBookDisabled = false; this.textPreviewDisabled = false; + this.codePreviewDisabled = false; setTimeout(async () => { // we include the note into recent list only if the user stayed on the note at least 5 seconds diff --git a/src/public/app/services/utils.js b/src/public/app/services/utils.js index ad490c982..51244f38f 100644 --- a/src/public/app/services/utils.js +++ b/src/public/app/services/utils.js @@ -40,6 +40,10 @@ function formatDateTime(date) { return formatDate(date) + " " + formatTime(date); } +function localNowDateTime() { + return dayjs().format('YYYY-MM-DD HH:mm:ss.SSSZZ') +} + function now() { return formatTimeWithSeconds(new Date()); } @@ -321,6 +325,7 @@ export default { formatDate, formatDateISO, formatDateTime, + localNowDateTime, now, isElectron, isMac, diff --git a/src/routes/routes.js b/src/routes/routes.js index 02bf1cabc..13783ec35 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -1,3 +1,5 @@ +"use strict"; + const setupRoute = require('./setup'); const loginRoute = require('./login'); const indexRoute = require('./index'); @@ -82,6 +84,7 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio try { const result = await cls.init(async () => { cls.namespace.set('sourceId', req.headers['trilium-source-id']); + cls.namespace.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']); protectedSessionService.setProtectedSessionId(req); if (transactional) { diff --git a/src/routes/setup.js b/src/routes/setup.js index 0d8c4f968..d57b619b9 100644 --- a/src/routes/setup.js +++ b/src/routes/setup.js @@ -3,10 +3,11 @@ const sqlInit = require('../services/sql_init'); const setupService = require('../services/setup'); const utils = require('../services/utils'); -const windowService = require('../services/window'); async function setupPage(req, res) { if (await sqlInit.isDbInitialized()) { + const windowService = require('../services/window'); + if (utils.isElectron()) { await windowService.createMainWindow(); windowService.closeSetupWindow(); diff --git a/src/services/cls.js b/src/services/cls.js index d0d3c8a17..39092b7a0 100644 --- a/src/services/cls.js +++ b/src/services/cls.js @@ -13,6 +13,10 @@ function getSourceId() { return namespace.get('sourceId'); } +function getLocalNowDateTime() { + return namespace.get('localNowDateTime'); +} + function disableEntityEvents() { namespace.set('disableEntityEvents', true); } @@ -50,6 +54,7 @@ module.exports = { wrap, namespace, getSourceId, + getLocalNowDateTime, disableEntityEvents, isEntityEventsDisabled, reset, diff --git a/src/services/date_utils.js b/src/services/date_utils.js index 1ea14a87c..cc84ca5b0 100644 --- a/src/services/date_utils.js +++ b/src/services/date_utils.js @@ -1,17 +1,29 @@ const dayjs = require('dayjs'); +const cls = require('./cls'); function utcNowDateTime() { return utcDateStr(new Date()); } +// CLS date time is important in web deployments - server often runs in different time zone than user is located in +// so we'd prefer client timezone to be used to record local dates. For this reason requests from client contain +// "trilium-local-now-datetime" header which is then stored in CLS function localNowDateTime() { - return dayjs().format('YYYY-MM-DD HH:mm:ss.SSSZZ') + return cls.getLocalNowDateTime() + || dayjs().format('YYYY-MM-DD HH:mm:ss.SSSZZ') } function localNowDate() { - const date = new Date(); + const clsDateTime = cls.getLocalNowDateTime(); - return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()); + if (clsDateTime) { + return clsDateTime.substr(0, 10); + } + else { + const date = new Date(); + + return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()); + } } function pad(num) { diff --git a/src/services/env.js b/src/services/env.js index 22fa4cbee..e7fa6caf8 100644 --- a/src/services/env.js +++ b/src/services/env.js @@ -1,5 +1,5 @@ module.exports = { isDev: function () { - return process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev'; + return !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev'); } }; \ No newline at end of file