aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-main/account/actor.service.ts
blob: a789b6f5b363f0eb81115814c0ab3d128e191a49 (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
38
39
40
41
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'
import { Account } from './account.model'
import { VideoChannel } from '../video-channel/video-channel.model'

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

  actorLoaded = new ReplaySubject<Account | VideoChannel>(1)

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

  getActor (actorName: string): Observable<Account | VideoChannel> {
    return this.authHttp.get<ServerAccount | ServerVideoChannel>(ActorService.BASE_ACTOR_API_URL + actorName)
                .pipe(
                  map(actorHash => {
                    const isAccount = /\/accounts\/.+/.test(actorHash.url)
                    const isVideoChannel = /\/video-channels\/.+/.test(actorHash.url)

                    if (isAccount) {
                      return new Account(actorHash)
                    }

                    if (isVideoChannel) {
                      return new VideoChannel(actorHash)
                    }
                  }),
                  tap(actor => this.actorLoaded.next(actor)),
                  catchError(res => this.restExtractor.handleError(res))
                )
  }
}