aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-main/account/actor.service.ts
blob: 464ed45195507e412e207426a6a761d2bfd31133 (plain) (blame)
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
import { Observable, ReplaySubject } from 'rxjs'
import { catchError, map, tap } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor } from '@app/core'
import { Account as ServerAccount, VideoChannel as ServerVideoChannel } from '@shared/models'
import { environment } from '../../../../environments/environment'

type KeysOfUnion<T> = T extends T ? keyof T: never
type ServerActor = KeysOfUnion<ServerAccount | ServerVideoChannel>

@Injectable()
export class ActorService {
  static BASE_ACTOR_API_URL = environment.apiUrl + '/api/v1/actors/'

  actorLoaded = new ReplaySubject<string>(1)

  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor
  ) {}

  getActorType (actorName: string): Observable<string> {
    return this.authHttp.get<ServerActor>(ActorService.BASE_ACTOR_API_URL + actorName)
                .pipe(
                  map(actorHash => {
                    if (actorHash[ 'userId' ]) {
                      return 'Account'
                    }

                    return 'VideoChannel'
                  }),
                  tap(actor => this.actorLoaded.next(actor)),
                  catchError(res => this.restExtractor.handleError(res))
                )
  }
}