-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparticipant.service.ts
111 lines (96 loc) · 3.49 KB
/
participant.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { Injectable } from '@angular/core';
import { Participant, ParticipantAdapter } from '../../models/Participant';
import { SettingsService } from '../settings/settings.service';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable()
export class ParticipantService {
participants!: Participant[];
lastSync: Date = new Date('2000-01-01T00:00:00.000Z');
constructor(public settings: SettingsService, private http: HttpClient, private adapter: ParticipantAdapter) {
this.getLastSync();
}
getLastSync() {
const localSync = localStorage.getItem('lastSync');
if (localSync == null) {
console.debug('never synced');
} else {
console.info('Last sync: ' + localSync);
}
}
setLastSync(d: Date) {
localStorage.setItem('lastSync', d.toISOString());
}
getParticipants() {
if (localStorage.getItem('participants') === null) {
this.participants = [];
} else {
// TODO: check if the localstorage Entry is valid JSON before returning
this.participants = JSON.parse(localStorage.getItem('participants'));
}
return this.participants;
}
addParticipant(participant: Participant) {
this.participants.unshift(participant);
localStorage.setItem('participants', JSON.stringify(this.participants));
}
/**
* Remove specified participant from the
* @param participant Participant to be removed
*/
removeParticipant(participant: Participant): boolean {
let removed = false;
for (let i = 0; i < this.participants.length; i++) {
if (participant.name === this.participants[i].name && participant.init === this.participants[i].init ) {
this.participants.splice(i, 1);
removed = true;
}
}
localStorage.setItem('participants', JSON.stringify(this.participants));
return removed;
}
/**
* Remove all participants in the list
*/
purgeParticipants() {
this.participants.splice(0);
localStorage.setItem('participants', JSON.stringify(this.participants));
}
exportParticipants() {
return JSON.stringify(this.participants);
}
// overwrite localstorage, and refresh local list of participants from there...
importParticipants(jsonParticipants: string) {
localStorage.setItem('participants', jsonParticipants);
this.exportParticipants();
}
getRemoteParticipants(): Observable<Participant[]> {
if (this.settings.usesRemoteParticipantList()) {
// console.debug('remote participant list affirmative. Checking URL');
const durationSinceLastSync = (Date.now()).valueOf() - this.lastSync.valueOf();
// If more than 15 hours since sync, update
if (durationSinceLastSync > 54000) {
const url = this.settings.getRemoteParticipantListURL();
// console.debug('Seconds since last sync: ' + durationSinceLastSync + ' syncing from ' + url + ' . . .');
return this.http
.get(url)
.pipe(map((data: any[]) => data.map((item) => this.adapter.adapt(item))));
} else {
console.error(new Error('not syncing'));
}
}
/* else { // TODO: throw exception ?
// console.info('using local participant list');
throw new Error('not syncing');
} */
}
updateRemoteParticipants(){
this.getRemoteParticipants().subscribe(resp => {
// display its headers
this.participants = resp;
localStorage.setItem('participants', JSON.stringify(this.participants));
this.setLastSync(new Date());
});
}
}