1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

Desktop: Fixes #3729: Fix lock issue when device does not have the right time

This commit is contained in:
Laurent Cozic
2020-09-08 23:57:48 +01:00
parent 3a33e5f416
commit 1f70a76c7e
5 changed files with 203 additions and 4 deletions

View File

@ -1,4 +1,6 @@
import { Dirnames } from './utils/types';
import ntpDate from 'lib/ntpDate';
const JoplinError = require('lib/JoplinError');
const { time } = require('lib/time-utils');
const { fileExtension, filename } = require('lib/path-utils.js');
@ -98,8 +100,8 @@ export default class LockHandler {
return output;
}
private lockIsActive(lock:Lock):boolean {
return Date.now() - lock.updatedTime < this.lockTtl;
private lockIsActive(lock:Lock, currentDate:Date):boolean {
return currentDate.getTime() - lock.updatedTime < this.lockTtl;
}
async hasActiveLock(lockType:LockType, clientType:string = null, clientId:string = null) {
@ -112,11 +114,12 @@ export default class LockHandler {
// of that type instead.
async activeLock(lockType:LockType, clientType:string = null, clientId:string = null) {
const locks = await this.locks(lockType);
const currentDate = await ntpDate();
if (lockType === LockType.Exclusive) {
const activeLocks = locks
.slice()
.filter((lock:Lock) => this.lockIsActive(lock))
.filter((lock:Lock) => this.lockIsActive(lock, currentDate))
.sort((a:Lock, b:Lock) => {
if (a.updatedTime === b.updatedTime) {
return a.clientId < b.clientId ? -1 : +1;
@ -134,7 +137,7 @@ export default class LockHandler {
for (const lock of locks) {
if (clientType && lock.clientType !== clientType) continue;
if (clientId && lock.clientId !== clientId) continue;
if (this.lockIsActive(lock)) return lock;
if (this.lockIsActive(lock, currentDate)) return lock;
}
return null;
}