1 import * as debug from 'debug'
2 import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
3 import { catchError, filter, map, switchMap, tap } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { ComponentPaginationLight, RestExtractor, RestService } from '@app/core'
7 import { buildBulkObservable } from '@app/helpers'
8 import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
9 import { ResultList, VideoChannel as VideoChannelServer, VideoSortField } from '@shared/models'
10 import { environment } from '../../../environments/environment'
12 const logger = debug('peertube:subscriptions:UserSubscriptionService')
14 type SubscriptionExistResult = { [ uri: string ]: boolean }
15 type SubscriptionExistResultObservable = { [ uri: string ]: Observable<boolean> }
18 export class UserSubscriptionService {
19 static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions'
21 // Use a replay subject because we "next" a value before subscribing
22 private existsSubject = new ReplaySubject<string>(1)
23 private readonly existsObservable: Observable<SubscriptionExistResult>
25 private myAccountSubscriptionCache: SubscriptionExistResult = {}
26 private myAccountSubscriptionCacheObservable: SubscriptionExistResultObservable = {}
27 private myAccountSubscriptionCacheSubject = new Subject<SubscriptionExistResult>()
30 private authHttp: HttpClient,
31 private restExtractor: RestExtractor,
32 private videoService: VideoService,
33 private restService: RestService
35 this.existsObservable = merge(
38 notifierObservable: this.existsSubject,
39 bulkGet: this.doSubscriptionsExist.bind(this)
40 }).pipe(map(r => r.response)),
42 this.myAccountSubscriptionCacheSubject
46 getUserSubscriptionVideos (parameters: {
47 videoPagination: ComponentPaginationLight
50 }): Observable<ResultList<Video>> {
51 const { videoPagination, sort, skipCount } = parameters
52 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
54 let params = new HttpParams()
55 params = this.restService.addRestGetParams(params, pagination, sort)
57 if (skipCount) params = params.set('skipCount', skipCount + '')
60 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
62 switchMap(res => this.videoService.extractVideos(res)),
63 catchError(err => this.restExtractor.handleError(err))
71 deleteSubscription (nameWithHost: string) {
72 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost
74 return this.authHttp.delete(url)
76 map(this.restExtractor.extractDataBool),
78 this.myAccountSubscriptionCache[nameWithHost] = false
80 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
82 catchError(err => this.restExtractor.handleError(err))
86 addSubscription (nameWithHost: string) {
87 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
89 const body = { uri: nameWithHost }
90 return this.authHttp.post(url, body)
92 map(this.restExtractor.extractDataBool),
94 this.myAccountSubscriptionCache[nameWithHost] = true
96 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
98 catchError(err => this.restExtractor.handleError(err))
102 listSubscriptions (parameters: {
103 pagination: ComponentPaginationLight
105 }): Observable<ResultList<VideoChannel>> {
106 const { pagination, search } = parameters
107 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
109 const restPagination = this.restService.componentPaginationToRestPagination(pagination)
111 let params = new HttpParams()
112 params = this.restService.addRestGetParams(params, restPagination)
113 if (search) params = params.append('search', search)
115 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
117 map(res => VideoChannelService.extractVideoChannels(res)),
118 catchError(err => this.restExtractor.handleError(err))
123 * SubscriptionExist part
126 listenToMyAccountSubscriptionCacheSubject () {
127 return this.myAccountSubscriptionCacheSubject.asObservable()
130 listenToSubscriptionCacheChange (nameWithHost: string) {
131 if (nameWithHost in this.myAccountSubscriptionCacheObservable) {
132 return this.myAccountSubscriptionCacheObservable[nameWithHost]
135 const obs = this.existsObservable
137 filter(existsResult => existsResult[nameWithHost] !== undefined),
138 map(existsResult => existsResult[nameWithHost])
141 this.myAccountSubscriptionCacheObservable[nameWithHost] = obs
145 doesSubscriptionExist (nameWithHost: string) {
146 logger('Running subscription check for %d.', nameWithHost)
148 if (nameWithHost in this.myAccountSubscriptionCache) {
149 logger('Found cache for %d.', nameWithHost)
151 return of(this.myAccountSubscriptionCache[nameWithHost])
154 this.existsSubject.next(nameWithHost)
156 logger('Fetching from network for %d.', nameWithHost)
157 return this.existsObservable.pipe(
158 filter(existsResult => existsResult[nameWithHost] !== undefined),
159 map(existsResult => existsResult[nameWithHost]),
160 tap(result => this.myAccountSubscriptionCache[nameWithHost] = result)
164 private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> {
165 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/exist'
166 let params = new HttpParams()
168 params = this.restService.addObjectParams(params, { uris })
170 return this.authHttp.get<SubscriptionExistResult>(url, { params })
173 this.myAccountSubscriptionCache = {
174 ...this.myAccountSubscriptionCache,
178 catchError(err => this.restExtractor.handleError(err))