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

199 lines
6.9 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { AutoupdateService } from './autoupdate.service';
import { ConstantsService } from './constants.service';
import { DataStoreService } from './data-store.service';
import { OperatorService } from './operator.service';
import { StorageService } from './storage.service';
import { WebsocketService } from './websocket.service';
/**
* Handles the bootup/showdown of this application.
*/
@Injectable({
providedIn: 'root'
})
2019-02-08 17:24:32 +01:00
export class OpenSlidesService {
/**
2019-03-07 10:47:03 +01:00
* If the user tries to access a certain URL without being authenticated, the URL will be stored here
*/
public redirectUrl: string;
/**
* Subject to hold the flag `booted`.
*/
public readonly booted = new BehaviorSubject(false);
2019-03-07 10:47:03 +01:00
/**
* Saves, if OpenSlides is fully booted. This means, that a user must be logged in
* (Anonymous is also a user in this case). This is the case after `afterLoginBootup`.
*/
public get isBooted(): boolean {
return this.booted.value;
2019-03-07 10:47:03 +01:00
}
/**
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,
private constantsService: ConstantsService
) {
// 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.retryReconnectEvent.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:
let response = await this.operator.whoAmIFromStorage();
const needToCheckOperator = !!response;
if (!response) {
response = await this.operator.whoAmI();
}
2018-10-26 10:23:14 +02:00
if (!response.user && !response.guest_enabled) {
if (!location.pathname.includes('error')) {
this.redirectUrl = location.pathname;
}
this.redirectToLoginIfNotSubpage();
2018-10-26 10:23:14 +02:00
} else {
await this.afterLoginBootup(response.user_id);
}
2019-03-04 11:45:15 +01:00
if (needToCheckOperator) {
2019-03-04 11:45:15 +01:00
// Check for the operator via a async whoami (so no await here)
// to validate, that the cache was correct.
this.checkOperator(false);
2018-10-26 10:23:14 +02:00
}
}
/**
* Redirects the user to /login, if he isn't on a subpage.
*/
private redirectToLoginIfNotSubpage(): void {
2019-03-15 15:29:02 +01:00
if (!this.redirectUrl || !this.redirectUrl.includes('/login/')) {
// Goto login, if the user isn't on a subpage like
// legal notice or reset passwort view.
// If other routing requests are active (e.g. to `/` or `/error`)
// wait for the authguard to finish to navigate to /login. This
// redirect is more important than the other ones.
setTimeout(() => {
this.router.navigate(['/login']);
});
}
}
/**
* 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.
2019-03-07 10:47:03 +01:00
* @param userId the id or null for guest
*/
2019-03-07 10:47:03 +01:00
public async afterLoginBootup(userId: number | null): Promise<void> {
2019-03-04 11:45:15 +01:00
// Check, which user was logged in last time
2018-10-26 10:23:14 +02:00
const lastUserId = await this.storageService.get<number>('lastUserLoggedIn');
// 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();
2019-03-07 10:47:03 +01:00
// Now finally booted.
this.booted.next(true);
}
/**
* 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();
if (changeId > 0) {
changeId += 1;
}
2019-03-04 11:45:15 +01:00
// disconnect the WS connection, if there was one. This is needed
// to update the connection parameters, namely the cookies. If the user
// is changed, the WS needs to reconnect, so the new connection holds the new
// user information.
if (this.websocketService.isConnected) {
await this.websocketService.close(); // Wait for the disconnect.
2019-03-04 11:45:15 +01:00
}
await this.websocketService.connect({ changeId: changeId }); // Request changes after changeId.
}
/**
* Shuts down OpenSlides. The websocket connection is closed and the operator is not set.
*/
public async shutdown(): Promise<void> {
await this.websocketService.close();
this.booted.next(false);
}
/**
* Shutdown and bootup.
*/
2018-10-26 10:23:14 +02:00
public async reboot(): Promise<void> {
await this.shutdown();
await this.bootup();
}
/**
* Clears the client cache and restarts OpenSlides. Results in "flickering" of the
* login mask, because the cached operator is also cleared.
*/
public async reset(): Promise<void> {
await this.shutdown();
await this.storageService.clear();
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.
*/
2019-03-04 11:45:15 +01:00
private async checkOperator(requestChanges: boolean = true): Promise<void> {
2018-10-26 10:23:14 +02:00
const response = await this.operator.whoAmI();
// User logged off.
if (!response.user && !response.guest_enabled) {
this.websocketService.cancelReconnectenRetry();
2019-03-04 11:45:15 +01:00
await this.shutdown();
this.redirectToLoginIfNotSubpage();
2018-10-26 10:23:14 +02:00
} else {
if (
(this.operator.user && this.operator.user.id !== response.user_id) ||
(!this.operator.user && response.user_id)
) {
// user changed
2019-03-04 11:45:15 +01:00
await this.DS.clear();
2018-10-26 10:23:14 +02:00
await this.reboot();
2019-03-04 11:45:15 +01:00
} else if (requestChanges) {
2018-10-26 10:23:14 +02:00
// User is still the same, but check for missed autoupdates.
this.autoupdateService.requestChanges();
this.constantsService.refresh();
}
2018-10-26 10:23:14 +02:00
}
}
}