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
const allHeaders = {
'trilium-source-id': glob.sourceId,
'trilium-local-now-datetime': utils.localNowDateTime(),
'x-csrf-token': glob.csrfToken
};

View File

@ -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

View File

@ -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,

View File

@ -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) {

View File

@ -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();

View File

@ -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,

View File

@ -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) {

View File

@ -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');
}
};