]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/user-subscription/user-subscription.service.ts
cfd5b100fe7ca500ae34ffcccc3d6c11c0a980ec
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / user-subscription / user-subscription.service.ts
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 { ComponentPagination } from '@app/shared/rest/component-pagination.model'
12
13 type SubscriptionExistResult = { [ uri: string ]: boolean }
14
15 @Injectable()
16 export class UserSubscriptionService {
17 static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions'
18
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>
22
23 constructor (
24 private authHttp: HttpClient,
25 private restExtractor: RestExtractor,
26 private restService: RestService
27 ) {
28 this.existsObservable = this.existsSubject.pipe(
29 bufferTime(500),
30 filter(uris => uris.length !== 0),
31 switchMap(uris => this.doSubscriptionsExist(uris)),
32 share()
33 )
34 }
35
36 deleteSubscription (nameWithHost: string) {
37 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost
38
39 return this.authHttp.delete(url)
40 .pipe(
41 map(this.restExtractor.extractDataBool),
42 catchError(err => this.restExtractor.handleError(err))
43 )
44 }
45
46 addSubscription (nameWithHost: string) {
47 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
48
49 const body = { uri: nameWithHost }
50 return this.authHttp.post(url, body)
51 .pipe(
52 map(this.restExtractor.extractDataBool),
53 catchError(err => this.restExtractor.handleError(err))
54 )
55 }
56
57 listSubscriptions (componentPagination: ComponentPagination): Observable<ResultList<VideoChannel>> {
58 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
59
60 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
61
62 let params = new HttpParams()
63 params = this.restService.addRestGetParams(params, pagination)
64
65 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
66 .pipe(
67 map(res => VideoChannelService.extractVideoChannels(res)),
68 catchError(err => this.restExtractor.handleError(err))
69 )
70 }
71
72 doesSubscriptionExist (nameWithHost: string) {
73 this.existsSubject.next(nameWithHost)
74
75 return this.existsObservable.pipe(first())
76 }
77
78 private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> {
79 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/exist'
80 let params = new HttpParams()
81
82 params = this.restService.addObjectParams(params, { uris })
83
84 return this.authHttp.get<SubscriptionExistResult>(url, { params })
85 .pipe(catchError(err => this.restExtractor.handleError(err)))
86 }
87 }