in web version use local client time instead of server time for recording dateModified etc.

This commit is contained in:
zadam 2020-04-26 14:26:57 +02:00
parent 8f68ff1932
commit 586d6b4557
8 changed files with 33 additions and 5 deletions

View File

@ -8,6 +8,7 @@ function getHeaders(headers) {
// also avoiding using underscores instead of dashes since nginx filters them out by default // also avoiding using underscores instead of dashes since nginx filters them out by default
const allHeaders = { const allHeaders = {
'trilium-source-id': glob.sourceId, 'trilium-source-id': glob.sourceId,
'trilium-local-now-datetime': utils.localNowDateTime(),
'x-csrf-token': glob.csrfToken 'x-csrf-token': glob.csrfToken
}; };

View File

@ -58,6 +58,7 @@ class TabContext extends Component {
this.autoBookDisabled = false; this.autoBookDisabled = false;
this.textPreviewDisabled = false; this.textPreviewDisabled = false;
this.codePreviewDisabled = false;
setTimeout(async () => { setTimeout(async () => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds // we include the note into recent list only if the user stayed on the note at least 5 seconds

View File

@ -40,6 +40,10 @@ function formatDateTime(date) {
return formatDate(date) + " " + formatTime(date); return formatDate(date) + " " + formatTime(date);
} }
function localNowDateTime() {
return dayjs().format('YYYY-MM-DD HH:mm:ss.SSSZZ')
}
function now() { function now() {
return formatTimeWithSeconds(new Date()); return formatTimeWithSeconds(new Date());
} }
@ -321,6 +325,7 @@ export default {
formatDate, formatDate,
formatDateISO, formatDateISO,
formatDateTime, formatDateTime,
localNowDateTime,
now, now,
isElectron, isElectron,
isMac, isMac,

View File

@ -1,3 +1,5 @@
"use strict";
const setupRoute = require('./setup'); const setupRoute = require('./setup');
const loginRoute = require('./login'); const loginRoute = require('./login');
const indexRoute = require('./index'); const indexRoute = require('./index');
@ -82,6 +84,7 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
try { try {
const result = await cls.init(async () => { const result = await cls.init(async () => {
cls.namespace.set('sourceId', req.headers['trilium-source-id']); cls.namespace.set('sourceId', req.headers['trilium-source-id']);
cls.namespace.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']);
protectedSessionService.setProtectedSessionId(req); protectedSessionService.setProtectedSessionId(req);
if (transactional) { if (transactional) {

View File

@ -3,10 +3,11 @@
const sqlInit = require('../services/sql_init'); const sqlInit = require('../services/sql_init');
const setupService = require('../services/setup'); const setupService = require('../services/setup');
const utils = require('../services/utils'); const utils = require('../services/utils');
const windowService = require('../services/window');
async function setupPage(req, res) { async function setupPage(req, res) {
if (await sqlInit.isDbInitialized()) { if (await sqlInit.isDbInitialized()) {
const windowService = require('../services/window');
if (utils.isElectron()) { if (utils.isElectron()) {
await windowService.createMainWindow(); await windowService.createMainWindow();
windowService.closeSetupWindow(); windowService.closeSetupWindow();

View File

@ -13,6 +13,10 @@ function getSourceId() {
return namespace.get('sourceId'); return namespace.get('sourceId');
} }
function getLocalNowDateTime() {
return namespace.get('localNowDateTime');
}
function disableEntityEvents() { function disableEntityEvents() {
namespace.set('disableEntityEvents', true); namespace.set('disableEntityEvents', true);
} }
@ -50,6 +54,7 @@ module.exports = {
wrap, wrap,
namespace, namespace,
getSourceId, getSourceId,
getLocalNowDateTime,
disableEntityEvents, disableEntityEvents,
isEntityEventsDisabled, isEntityEventsDisabled,
reset, reset,

View File

@ -1,17 +1,29 @@
const dayjs = require('dayjs'); const dayjs = require('dayjs');
const cls = require('./cls');
function utcNowDateTime() { function utcNowDateTime() {
return utcDateStr(new Date()); 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() { 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() { 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) { function pad(num) {

View File

@ -1,5 +1,5 @@
module.exports = { module.exports = {
isDev: function () { isDev: function () {
return process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev'; return !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev');
} }
}; };