]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/user-subscription/user-subscription.service.ts
Skip videos count on client if we don't use it
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / user-subscription / user-subscription.service.ts
CommitLineData
aa55a4da 1import { bufferTime, catchError, filter, first, map, share, switchMap } from 'rxjs/operators'
f37dc0dd 2import { HttpClient, HttpParams } from '@angular/common/http'
22a16e36
C
3import { Injectable } from '@angular/core'
4import { ResultList } from '../../../../../shared'
5import { environment } from '../../../environments/environment'
f37dc0dd
C
6import { RestExtractor, RestService } from '../rest'
7import { Observable, ReplaySubject, Subject } from 'rxjs'
22a16e36
C
8import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
9import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
10import { VideoChannel as VideoChannelServer } from '../../../../../shared/models/videos'
440d39c5 11import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model'
22a16e36 12
f37dc0dd
C
13type SubscriptionExistResult = { [ uri: string ]: boolean }
14
22a16e36
C
15@Injectable()
16export class UserSubscriptionService {
17 static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions'
18
f37dc0dd
C
19 // Use a replay subject because we "next" a value before subscribing
20 private existsSubject: Subject<string> = new ReplaySubject(1)
aa55a4da 21 private readonly existsObservable: Observable<SubscriptionExistResult>
f37dc0dd 22
22a16e36
C
23 constructor (
24 private authHttp: HttpClient,
f37dc0dd
C
25 private restExtractor: RestExtractor,
26 private restService: RestService
22a16e36 27 ) {
f37dc0dd 28 this.existsObservable = this.existsSubject.pipe(
f37dc0dd
C
29 bufferTime(500),
30 filter(uris => uris.length !== 0),
f0a39880 31 switchMap(uris => this.doSubscriptionsExist(uris)),
f37dc0dd
C
32 share()
33 )
22a16e36
C
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
440d39c5 57 listSubscriptions (componentPagination: ComponentPaginationLight): Observable<ResultList<VideoChannel>> {
22a16e36
C
58 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
59
aa55a4da
C
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 })
22a16e36
C
66 .pipe(
67 map(res => VideoChannelService.extractVideoChannels(res)),
68 catchError(err => this.restExtractor.handleError(err))
69 )
70 }
71
f0a39880 72 doesSubscriptionExist (nameWithHost: string) {
f37dc0dd 73 this.existsSubject.next(nameWithHost)
22a16e36 74
aa55a4da 75 return this.existsObservable.pipe(first())
f37dc0dd 76 }
22a16e36 77
f0a39880 78 private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> {
f37dc0dd
C
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)))
22a16e36
C
86 }
87}