1 import { bufferTime, catchError, filter, first, map, share, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { ResultList } from '../../../../../shared'
5 import { environment } from '../../../environments/environment'
6 import { RestExtractor, RestService } from '../rest'
7 import { Observable, ReplaySubject, Subject } from 'rxjs'
8 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
9 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
10 import { VideoChannel as VideoChannelServer } from '../../../../../shared/models/videos'
11 import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model'
13 type SubscriptionExistResult = { [ uri: string ]: boolean }
16 export class UserSubscriptionService {
17 static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions'
19 // Use a replay subject because we "next" a value before subscribing
20 private existsSubject: Subject<string> = new ReplaySubject(1)
21 private readonly existsObservable: Observable<SubscriptionExistResult>
24 private authHttp: HttpClient,
25 private restExtractor: RestExtractor,
26 private restService: RestService
28 this.existsObservable = this.existsSubject.pipe(
30 filter(uris => uris.length !== 0),
31 switchMap(uris => this.doSubscriptionsExist(uris)),
36 deleteSubscription (nameWithHost: string) {
37 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost
39 return this.authHttp.delete(url)
41 map(this.restExtractor.extractDataBool),
42 catchError(err => this.restExtractor.handleError(err))
46 addSubscription (nameWithHost: string) {
47 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
49 const body = { uri: nameWithHost }
50 return this.authHttp.post(url, body)
52 map(this.restExtractor.extractDataBool),
53 catchError(err => this.restExtractor.handleError(err))
57 listSubscriptions (componentPagination: ComponentPaginationLight): Observable<ResultList<VideoChannel>> {
58 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
60 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
62 let params = new HttpParams()
63 params = this.restService.addRestGetParams(params, pagination)
65 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
67 map(res => VideoChannelService.extractVideoChannels(res)),
68 catchError(err => this.restExtractor.handleError(err))
72 doesSubscriptionExist (nameWithHost: string) {
73 this.existsSubject.next(nameWithHost)
75 return this.existsObservable.pipe(first())
78 private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> {
79 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/exist'
80 let params = new HttpParams()
82 params = this.restService.addObjectParams(params, { uris })
84 return this.authHttp.get<SubscriptionExistResult>(url, { params })
85 .pipe(catchError(err => this.restExtractor.handleError(err)))