add back in time functions

pull/679/head
Ryan Tharp 6 years ago
parent 91a814b20a
commit 03efa0634b

@ -36,6 +36,42 @@ class LokiPublicChatFactoryAPI extends EventEmitter {
return thisServer; return thisServer;
} }
static async getServerTime() {
const url = `${window.getDefaultFileServer()}/loki/v1/time`;
let timestamp = NaN;
try {
const res = await nodeFetch(url);
if (res.ok) {
timestamp = await res.text();
}
} catch (e) {
return timestamp;
}
return Number(timestamp);
}
static async getTimeDifferential() {
// Get time differential between server and client in seconds
const serverTime = await this.getServerTime();
const clientTime = Math.ceil(Date.now() / 1000);
if (Number.isNaN(serverTime)) {
return 0;
}
return serverTime - clientTime;
}
static async setClockParams() {
// Set server-client time difference
const maxTimeDifferential = 30;
const timeDifferential = await this.getTimeDifferential();
window.clientClockSynced = Math.abs(timeDifferential) < maxTimeDifferential;
return window.clientClockSynced;
}
// channel getter/factory // channel getter/factory
async findOrCreateChannel(serverUrl, channelId, conversationId) { async findOrCreateChannel(serverUrl, channelId, conversationId) {
const server = await this.findOrCreateServer(serverUrl); const server = await this.findOrCreateServer(serverUrl);

Loading…
Cancel
Save