OpenSlides/client/src/app/core/services/openslides.service.ts

137 lines
4.7 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { OpenSlidesComponent } from 'app/openslides.component';
import { WebsocketService } from './websocket.service';
import { OperatorService } from './operator.service';
2018-10-26 10:23:14 +02:00
import { StorageService } from './storage.service';
import { AutoupdateService } from './autoupdate.service';
2018-09-13 14:40:04 +02:00
import { DataStoreService } from './data-store.service';
/**
* Handles the bootup/showdown of this application.
*/
@Injectable({
providedIn: 'root'
})
export class OpenSlidesService extends OpenSlidesComponent {
/**
* if the user tries to access a certain URL without being authenticated, the URL will be stored here
*/
public redirectUrl: string;
/**
2018-10-26 10:23:14 +02:00
* Constructor to create the OpenSlidesService. Registers itself to the WebsocketService.
* @param storageService
* @param operator
* @param websocketService
* @param router
* @param autoupdateService
2018-10-26 10:23:14 +02:00
* @param DS
*/
2018-08-29 13:21:25 +02:00
public constructor(
2018-10-26 10:23:14 +02:00
private storageService: StorageService,
private operator: OperatorService,
private websocketService: WebsocketService,
private router: Router,
2018-09-13 14:40:04 +02:00
private autoupdateService: AutoupdateService,
private DS: DataStoreService
) {
super();
// Handler that gets called, if the websocket connection reconnects after a disconnection.
// There might have changed something on the server, so we check the operator, if he changed.
websocketService.reconnectEvent.subscribe(() => {
this.checkOperator();
});
2018-09-13 14:40:04 +02:00
this.bootup();
}
/**
* the bootup-sequence: Do a whoami request and if it was successful, do
* {@method afterLoginBootup}. If not, redirect the user to the login page.
*/
2018-10-26 10:23:14 +02:00
public async bootup(): Promise<void> {
// start autoupdate if the user is logged in:
2018-10-26 10:23:14 +02:00
const response = await this.operator.whoAmI();
this.operator.guestsEnabled = response.guest_enabled;
if (!response.user && !response.guest_enabled) {
this.redirectUrl = location.pathname;
// Goto login, if the user isn't login and guests are not allowed
this.router.navigate(['/login']);
} else {
await this.afterLoginBootup(response.user_id);
}
}
/**
* the login bootup-sequence: Check (and maybe clear) the cache und setup the DataStore
2018-10-26 10:23:14 +02:00
* and websocket. This "login" also may be the "login" of an anonymous when he is using
* OpenSlides as a guest.
* @param userId
*/
2018-10-26 10:23:14 +02:00
public async afterLoginBootup(userId: number): Promise<void> {
// Else, check, which user was logged in last time
2018-10-26 10:23:14 +02:00
const lastUserId = await this.storageService.get<number>('lastUserLoggedIn');
console.log('user transition:', lastUserId, '->', userId);
// if the user changed, reset the cache and save the new user.
if (userId !== lastUserId) {
await this.DS.clear();
await this.storageService.set('lastUserLoggedIn', userId);
}
await this.setupDataStoreAndWebSocket();
}
/**
* Init DS from cache and after this start the websocket service.
*/
2018-10-26 10:23:14 +02:00
private async setupDataStoreAndWebSocket(): Promise<void> {
let changeId = await this.DS.initFromStorage();
console.log('change ID on DS setup', changeId);
if (changeId > 0) {
changeId += 1;
}
this.websocketService.connect({ changeId: changeId }); // Request changes after changeId.
}
/**
2018-10-26 10:23:14 +02:00
* Shuts OpenSlides down. The websocket is closed and the operator is not set.
*/
public shutdown(): void {
this.websocketService.close();
this.operator.user = null;
}
/**
* Shutdown and bootup.
*/
2018-10-26 10:23:14 +02:00
public async reboot(): Promise<void> {
this.shutdown();
2018-10-26 10:23:14 +02:00
await this.bootup();
}
/**
2018-10-26 10:23:14 +02:00
* Verify that the operator is the same as it was before. Should be alled on a reconnect.
*/
2018-10-26 10:23:14 +02:00
private async checkOperator(): Promise<void> {
const response = await this.operator.whoAmI();
// User logged off.
if (!response.user && !response.guest_enabled) {
this.shutdown();
this.router.navigate(['/login']);
} else {
if (
(this.operator.user && this.operator.user.id !== response.user_id) ||
(!this.operator.user && response.user_id)
) {
// user changed
await this.reboot();
} else {
2018-10-26 10:23:14 +02:00
// User is still the same, but check for missed autoupdates.
this.autoupdateService.requestChanges();
}
2018-10-26 10:23:14 +02:00
}
}
}