]>
Commit | Line | Data |
---|---|---|
1 | import { Observable, ReplaySubject } from 'rxjs' | |
2 | import { catchError, map, tap } from 'rxjs/operators' | |
3 | import { HttpClient } from '@angular/common/http' | |
4 | import { Injectable } from '@angular/core' | |
5 | import { RestExtractor } from '@app/core' | |
6 | import { Account as ServerAccount, VideoChannel as ServerVideoChannel } from '@shared/models' | |
7 | import { environment } from '../../../../environments/environment' | |
8 | ||
9 | type KeysOfUnion<T> = T extends T ? keyof T: never | |
10 | type ServerActor = KeysOfUnion<ServerAccount | ServerVideoChannel> | |
11 | ||
12 | @Injectable() | |
13 | export class ActorService { | |
14 | static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/' | |
15 | ||
16 | actorLoaded = new ReplaySubject<string>(1) | |
17 | ||
18 | constructor ( | |
19 | private authHttp: HttpClient, | |
20 | private restExtractor: RestExtractor | |
21 | ) {} | |
22 | ||
23 | getActorType (actorName: string): Observable<string> { | |
24 | return this.authHttp.get<ServerActor>(ActorService.BASE_ACTOR_API_URL + actorName) | |
25 | .pipe( | |
26 | map(actorHash => { | |
27 | if (actorHash[ 'userId' ]) { | |
28 | return 'Account' | |
29 | } | |
30 | ||
31 | return 'VideoChannel' | |
32 | }), | |
33 | tap(actor => this.actorLoaded.next(actor)), | |
34 | catchError(res => this.restExtractor.handleError(res)) | |
35 | ) | |
36 | } | |
37 | } |