2018-07-04 17:51:31 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
import { OpenSlidesComponent } from 'app/openslides.component';
|
|
|
|
import { WebsocketService } from './websocket.service';
|
|
|
|
// the Models
|
2018-07-23 16:42:17 +02:00
|
|
|
import { Item } from 'app/shared/models/agenda/item';
|
|
|
|
import { Assignment } from 'app/shared/models/assignments/assignment';
|
|
|
|
import { ChatMessage } from 'app/shared/models/core/chat-message';
|
|
|
|
import { Config } from 'app/shared/models/core/config';
|
|
|
|
import { Countdown } from 'app/shared/models/core/countdown';
|
|
|
|
import { ProjectorMessage } from 'app/shared/models/core/projector-message';
|
|
|
|
import { Projector } from 'app/shared/models/core/projector';
|
|
|
|
import { Tag } from 'app/shared/models/core/tag';
|
|
|
|
import { Mediafile } from 'app/shared/models/mediafiles/mediafile';
|
|
|
|
import { Category } from 'app/shared/models/motions/category';
|
|
|
|
import { MotionBlock } from 'app/shared/models/motions/motion-block';
|
|
|
|
import { MotionChangeReco } from 'app/shared/models/motions/motion-change-reco';
|
|
|
|
import { Motion } from 'app/shared/models/motions/motion';
|
|
|
|
import { Workflow } from 'app/shared/models/motions/workflow';
|
|
|
|
import { Topic } from 'app/shared/models/topics/topic';
|
|
|
|
import { Group } from 'app/shared/models/users/group';
|
|
|
|
import { PersonalNote } from 'app/shared/models/users/personal-note';
|
|
|
|
import { User } from 'app/shared/models/users/user';
|
2018-07-04 17:51:31 +02:00
|
|
|
|
|
|
|
/**
|
2018-07-12 14:11:31 +02:00
|
|
|
* Handles the initial update and automatic updates using the {@link WebsocketService}
|
|
|
|
* Incoming objects, usually BaseModels, will be saved in the dataStore (`this.DS`)
|
|
|
|
* This service usually creates all models
|
|
|
|
*
|
|
|
|
* The dataStore will injected over the parent class: {@link OpenSlidesComponent}.
|
2018-07-04 17:51:31 +02:00
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2018-07-12 14:11:31 +02:00
|
|
|
export class AutoupdateService extends OpenSlidesComponent {
|
|
|
|
/**
|
|
|
|
* Stores the to create the socket created using {@link WebsocketService}.
|
|
|
|
*/
|
2018-07-04 17:51:31 +02:00
|
|
|
private socket;
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Constructor to create the AutoupdateService. Calls the constructor of the parent class.
|
|
|
|
* @param websocketService
|
|
|
|
*/
|
2018-07-04 17:51:31 +02:00
|
|
|
constructor(private websocketService: WebsocketService) {
|
|
|
|
super();
|
2018-07-06 09:38:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Function to start the automatic update process
|
|
|
|
* will build up a websocket connection using {@link WebsocketService}
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
startAutoupdate(): void {
|
2018-07-04 17:51:31 +02:00
|
|
|
this.socket = this.websocketService.connect();
|
|
|
|
this.socket.subscribe(response => {
|
|
|
|
this.storeResponse(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Handle the answer of incoming data via {@link WebsocketService}.
|
|
|
|
*
|
|
|
|
* Detects the Class of an incomming model, creates a new empty object and assigns
|
|
|
|
* the data to it using the deserialize function.
|
|
|
|
*
|
|
|
|
* Saves models in DataStore.
|
|
|
|
*/
|
2018-07-04 17:51:31 +02:00
|
|
|
storeResponse(socketResponse): void {
|
2018-07-12 14:11:31 +02:00
|
|
|
socketResponse.forEach(jsonObj => {
|
|
|
|
const targetClass = this.getClassFromCollectionString(jsonObj.collection);
|
2018-08-22 16:03:49 +02:00
|
|
|
if (jsonObj.action === 'deleted') {
|
|
|
|
console.log('storeResponse detect delete');
|
|
|
|
|
|
|
|
this.DS.remove(jsonObj.collection, jsonObj.id);
|
|
|
|
} else {
|
|
|
|
this.DS.add(new targetClass().deserialize(jsonObj.data));
|
|
|
|
}
|
2018-07-04 17:51:31 +02:00
|
|
|
});
|
|
|
|
}
|
2018-07-12 14:11:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* helper function to return the correct class from a collection string
|
|
|
|
*/
|
|
|
|
getClassFromCollectionString(collection: string): any {
|
|
|
|
switch (collection) {
|
|
|
|
case 'core/projector': {
|
|
|
|
return Projector;
|
|
|
|
}
|
|
|
|
case 'core/chat-message': {
|
|
|
|
return ChatMessage;
|
|
|
|
}
|
|
|
|
case 'core/tag': {
|
|
|
|
return Tag;
|
|
|
|
}
|
|
|
|
case 'core/projector-message': {
|
|
|
|
return ProjectorMessage;
|
|
|
|
}
|
|
|
|
case 'core/countdown': {
|
|
|
|
return Countdown;
|
|
|
|
}
|
|
|
|
case 'core/config': {
|
|
|
|
return Config;
|
|
|
|
}
|
|
|
|
case 'users/user': {
|
|
|
|
return User;
|
|
|
|
}
|
|
|
|
case 'users/group': {
|
|
|
|
return Group;
|
|
|
|
}
|
|
|
|
case 'users/personal-note': {
|
|
|
|
return PersonalNote;
|
|
|
|
}
|
|
|
|
case 'agenda/item': {
|
|
|
|
return Item;
|
|
|
|
}
|
|
|
|
case 'topics/topic': {
|
|
|
|
return Topic;
|
|
|
|
}
|
|
|
|
case 'motions/category': {
|
|
|
|
return Category;
|
|
|
|
}
|
|
|
|
case 'motions/motion': {
|
|
|
|
return Motion;
|
|
|
|
}
|
|
|
|
case 'motions/motion-block': {
|
|
|
|
return MotionBlock;
|
|
|
|
}
|
|
|
|
case 'motions/workflow': {
|
|
|
|
return Workflow;
|
|
|
|
}
|
|
|
|
case 'motions/motion-change-recommendation': {
|
|
|
|
return MotionChangeReco;
|
|
|
|
}
|
|
|
|
case 'assignments/assignment': {
|
|
|
|
return Assignment;
|
|
|
|
}
|
|
|
|
case 'mediafiles/mediafile': {
|
|
|
|
return Mediafile;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
console.error('No rule for ', collection);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-04 17:51:31 +02:00
|
|
|
}
|