From 8b48f4ddb4e2c80101cfe1208a72173cf18e2940 Mon Sep 17 00:00:00 2001
From: paf <paf@titelfrei.de>
Date: Sun, 6 Dec 2020 14:41:45 +0100
Subject: [PATCH] restores the comparism on local & server clock

---
 src/app/app.component.ts     | 68 +++++++++++++++++++++++-------------
 src/app/config/app.config.ts |  9 ++---
 2 files changed, 49 insertions(+), 28 deletions(-)

diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 90ed536d..5bfb25e0 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -58,6 +58,16 @@ export class AppComponent implements OnInit, OnDestroy {
     return true;
   }
 
+  private static localTime(date: Date): string {
+    const year = date.getFullYear();
+    const month = (`0${date.getMonth() + 1}`).slice(-2);
+    const day = (`0${date.getDate()}`).slice(-2);
+    const hours = (`0${date.getHours()}`).slice(-2);
+    const minutes = (`0${date.getMinutes()}`).slice(-2);
+    const seconds = (`0${date.getSeconds()}`).slice(-2);
+    return `${day}.${month}.${year} ${hours}:${minutes}:${seconds}`;
+  }
+
   closeErrorBox(): void {
     this.showError = false;
   }
@@ -86,33 +96,43 @@ export class AppComponent implements OnInit, OnDestroy {
       this.setupFocusListeners();
 
       this.bs.getSysConfig().subscribe(sysConfig => {
-        if (sysConfig) {
-          this.cts.addCustomTexts(sysConfig.customTexts);
-          const authData = MainDataService.getAuthData();
-          if (authData) {
-            this.cts.addCustomTexts(authData.customTexts);
-          }
-
-          if (sysConfig.broadcastingService && sysConfig.broadcastingService.status) {
-            this.mds.broadcastingServiceInfo = sysConfig.broadcastingService;
-          }
-          this.mds.isApiValid = AppComponent.isValidVersion(this.expectedApiVersion, sysConfig.version);
-          this.mds.apiVersion = sysConfig.version;
-
-          if (!this.mds.isApiValid) {
-            this.mds.appError$.next({
-              label: 'Server-Problem: API-Version ungültig',
-              description: `erwartet: ${this.expectedApiVersion}, gefunden: ${sysConfig.version}`,
-              category: 'FATAL'
-            });
-          }
+        if (!sysConfig) {
+          this.mds.isApiValid = false; // push on this.mds.appError$ ?
+          return;
+        }
+        this.cts.addCustomTexts(sysConfig.customTexts);
+        const authData = MainDataService.getAuthData();
+        if (authData) {
+          this.cts.addCustomTexts(authData.customTexts);
+        }
 
-          // TODO implement SysConfig.mainLogo
+        if (sysConfig.broadcastingService && sysConfig.broadcastingService.status) {
+          this.mds.broadcastingServiceInfo = sysConfig.broadcastingService;
+        }
+        this.mds.isApiValid = AppComponent.isValidVersion(this.expectedApiVersion, sysConfig.version);
+        this.mds.apiVersion = sysConfig.version;
+
+        if (!this.mds.isApiValid) {
+          this.mds.appError$.next({
+            label: 'Server-Problem: API-Version ungültig',
+            description: `erwartet: ${this.expectedApiVersion}, gefunden: ${sysConfig.version}`,
+            category: 'FATAL'
+          });
+        }
 
-          this.mds.setTestConfig(sysConfig.testConfig);
-        } else {
-          this.mds.isApiValid = false;
+        // TODO implement SysConfig.mainLogo
+
+        const clientTime = new Date();
+        const serverTime = new Date(sysConfig.serverTimestamp);
+        if (Math.abs(sysConfig.serverTimestamp - clientTime.getTime()) > 90000) {
+          this.mds.appError$.next({
+            label: 'Server- und Client-Uhr stimmen nicht überein.',
+            description: `Server-Zeit: ${AppComponent.localTime(serverTime)}, 
+            Client-Zeit: ${AppComponent.localTime(clientTime)}`,
+            category: 'FATAL'
+          });
         }
+        this.mds.setTestConfig(sysConfig.testConfig);
       });
 
       this.bs.getSysCheckInfo().subscribe(sysCheckConfigs => {
diff --git a/src/app/config/app.config.ts b/src/app/config/app.config.ts
index 205d02f7..c33ec354 100644
--- a/src/app/config/app.config.ts
+++ b/src/app/config/app.config.ts
@@ -7,6 +7,7 @@ export interface SysConfig {
   version: string;
   mainLogo: string;
   testConfig: KeyValuePairs;
+  serverTimestamp: number;
   broadcastingService: BroadCastingServiceInfo;
 }
 
@@ -22,11 +23,11 @@ export class AppConfig {
   ) {
   }
 
-  setDefaultCustomTexts() {
+  setDefaultCustomTexts(): void {
     const ctDefaults = {};
-    for (const k of Object.keys(customTextsDefault)) {
-      ctDefaults[k] = customTextsDefault[k].defaultvalue;
-    }
+    Object.keys(customTextsDefault).forEach(key => {
+      ctDefaults[key] = customTextsDefault[key].defaultvalue;
+    });
     this.cts.addCustomTexts(ctDefaults);
   }
 }
-- 
GitLab