aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/user-subscription/user-subscription.service.ts
blob: cf622019f638e9d05325340c9ece9937e44d44ea (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
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
import { bufferTime, catchError, filter, map, share, switchMap, tap } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { ResultList } from '../../../../../shared'
import { environment } from '../../../environments/environment'
import { RestExtractor, RestService } from '../rest'
import { Observable, ReplaySubject, Subject } from 'rxjs'
import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
import { VideoChannel as VideoChannelServer } from '../../../../../shared/models/videos'

type SubscriptionExistResult = { [ uri: string ]: boolean }

@Injectable()
export class UserSubscriptionService {
  static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions'

  // Use a replay subject because we "next" a value before subscribing
  private existsSubject: Subject<string> = new ReplaySubject(1)
  private existsObservable: Observable<SubscriptionExistResult>

  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor,
    private restService: RestService
  ) {
    this.existsObservable = this.existsSubject.pipe(
      tap(u => console.log(u)),
      bufferTime(500),
      filter(uris => uris.length !== 0),
      switchMap(uris => this.areSubscriptionExist(uris)),
      share()
    )
  }

  deleteSubscription (nameWithHost: string) {
    const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost

    return this.authHttp.delete(url)
               .pipe(
                 map(this.restExtractor.extractDataBool),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  addSubscription (nameWithHost: string) {
    const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL

    const body = { uri: nameWithHost }
    return this.authHttp.post(url, body)
               .pipe(
                 map(this.restExtractor.extractDataBool),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  listSubscriptions (): Observable<ResultList<VideoChannel>> {
    const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL

    return this.authHttp.get<ResultList<VideoChannelServer>>(url)
               .pipe(
                 map(res => VideoChannelService.extractVideoChannels(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  isSubscriptionExists (nameWithHost: string) {
    this.existsSubject.next(nameWithHost)

    return this.existsObservable
  }

  private areSubscriptionExist (uris: string[]): Observable<SubscriptionExistResult> {
    console.log(uris)
    const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/exist'
    let params = new HttpParams()

    params = this.restService.addObjectParams(params, { uris })

    return this.authHttp.get<SubscriptionExistResult>(url, { params })
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }
}