]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-subscription/user-subscription.service.ts
Refactor sort middlewares
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-subscription / user-subscription.service.ts
CommitLineData
67ed6552 1import * as debug from 'debug'
3da38d6e
C
2import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
3import { catchError, filter, map, switchMap, tap } from 'rxjs/operators'
f37dc0dd 4import { HttpClient, HttpParams } from '@angular/common/http'
afb7d2d5 5import { Injectable } from '@angular/core'
67ed6552 6import { ComponentPaginationLight, RestExtractor, RestService } from '@app/core'
3da38d6e 7import { buildBulkObservable } from '@app/helpers'
67ed6552
C
8import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
9import { ResultList, VideoChannel as VideoChannelServer, VideoSortField } from '@shared/models'
22a16e36 10import { environment } from '../../../environments/environment'
9270ccf6
RK
11
12const logger = debug('peertube:subscriptions:UserSubscriptionService')
22a16e36 13
f37dc0dd 14type SubscriptionExistResult = { [ uri: string ]: boolean }
9270ccf6 15type SubscriptionExistResultObservable = { [ uri: string ]: Observable<boolean> }
f37dc0dd 16
22a16e36
C
17@Injectable()
18export class UserSubscriptionService {
19 static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions'
20
f37dc0dd 21 // Use a replay subject because we "next" a value before subscribing
9270ccf6 22 private existsSubject = new ReplaySubject<string>(1)
aa55a4da 23 private readonly existsObservable: Observable<SubscriptionExistResult>
f37dc0dd 24
9270ccf6
RK
25 private myAccountSubscriptionCache: SubscriptionExistResult = {}
26 private myAccountSubscriptionCacheObservable: SubscriptionExistResultObservable = {}
27 private myAccountSubscriptionCacheSubject = new Subject<SubscriptionExistResult>()
28
22a16e36
C
29 constructor (
30 private authHttp: HttpClient,
f37dc0dd 31 private restExtractor: RestExtractor,
67ed6552 32 private videoService: VideoService,
afb7d2d5 33 private restService: RestService
22a16e36 34 ) {
9270ccf6 35 this.existsObservable = merge(
3da38d6e
C
36 buildBulkObservable({
37 time: 500,
3da38d6e
C
38 notifierObservable: this.existsSubject,
39 bulkGet: this.doSubscriptionsExist.bind(this)
2cc276f9 40 }).pipe(map(r => r.response)),
9270ccf6
RK
41
42 this.myAccountSubscriptionCacheSubject
f37dc0dd 43 )
22a16e36
C
44 }
45
67ed6552 46 getUserSubscriptionVideos (parameters: {
9df52d66
C
47 videoPagination: ComponentPaginationLight
48 sort: VideoSortField
67ed6552
C
49 skipCount?: boolean
50 }): Observable<ResultList<Video>> {
51 const { videoPagination, sort, skipCount } = parameters
52 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
53
54 let params = new HttpParams()
55 params = this.restService.addRestGetParams(params, pagination, sort)
56
57 if (skipCount) params = params.set('skipCount', skipCount + '')
58
59 return this.authHttp
60 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
61 .pipe(
62 switchMap(res => this.videoService.extractVideos(res)),
63 catchError(err => this.restExtractor.handleError(err))
64 )
65 }
66
9270ccf6
RK
67 /**
68 * Subscription part
69 */
70
22a16e36
C
71 deleteSubscription (nameWithHost: string) {
72 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost
73
74 return this.authHttp.delete(url)
75 .pipe(
76 map(this.restExtractor.extractDataBool),
9270ccf6
RK
77 tap(() => {
78 this.myAccountSubscriptionCache[nameWithHost] = false
79
80 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
81 }),
22a16e36
C
82 catchError(err => this.restExtractor.handleError(err))
83 )
84 }
85
86 addSubscription (nameWithHost: string) {
87 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
88
89 const body = { uri: nameWithHost }
90 return this.authHttp.post(url, body)
91 .pipe(
92 map(this.restExtractor.extractDataBool),
9270ccf6
RK
93 tap(() => {
94 this.myAccountSubscriptionCache[nameWithHost] = true
95
96 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
97 }),
22a16e36
C
98 catchError(err => this.restExtractor.handleError(err))
99 )
100 }
101
4f5d0459
RK
102 listSubscriptions (parameters: {
103 pagination: ComponentPaginationLight
104 search: string
105 }): Observable<ResultList<VideoChannel>> {
106 const { pagination, search } = parameters
22a16e36
C
107 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
108
4f5d0459 109 const restPagination = this.restService.componentPaginationToRestPagination(pagination)
aa55a4da
C
110
111 let params = new HttpParams()
4f5d0459
RK
112 params = this.restService.addRestGetParams(params, restPagination)
113 if (search) params = params.append('search', search)
aa55a4da
C
114
115 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
22a16e36
C
116 .pipe(
117 map(res => VideoChannelService.extractVideoChannels(res)),
118 catchError(err => this.restExtractor.handleError(err))
119 )
120 }
121
9270ccf6
RK
122 /**
123 * SubscriptionExist part
124 */
125
126 listenToMyAccountSubscriptionCacheSubject () {
127 return this.myAccountSubscriptionCacheSubject.asObservable()
128 }
129
130 listenToSubscriptionCacheChange (nameWithHost: string) {
131 if (nameWithHost in this.myAccountSubscriptionCacheObservable) {
9df52d66 132 return this.myAccountSubscriptionCacheObservable[nameWithHost]
9270ccf6
RK
133 }
134
135 const obs = this.existsObservable
136 .pipe(
9df52d66
C
137 filter(existsResult => existsResult[nameWithHost] !== undefined),
138 map(existsResult => existsResult[nameWithHost])
9270ccf6
RK
139 )
140
9df52d66 141 this.myAccountSubscriptionCacheObservable[nameWithHost] = obs
9270ccf6
RK
142 return obs
143 }
144
f0a39880 145 doesSubscriptionExist (nameWithHost: string) {
9270ccf6
RK
146 logger('Running subscription check for %d.', nameWithHost)
147
148 if (nameWithHost in this.myAccountSubscriptionCache) {
149 logger('Found cache for %d.', nameWithHost)
150
9df52d66 151 return of(this.myAccountSubscriptionCache[nameWithHost])
9270ccf6
RK
152 }
153
f37dc0dd 154 this.existsSubject.next(nameWithHost)
22a16e36 155
9270ccf6
RK
156 logger('Fetching from network for %d.', nameWithHost)
157 return this.existsObservable.pipe(
9df52d66
C
158 filter(existsResult => existsResult[nameWithHost] !== undefined),
159 map(existsResult => existsResult[nameWithHost]),
160 tap(result => this.myAccountSubscriptionCache[nameWithHost] = result)
9270ccf6 161 )
f37dc0dd 162 }
22a16e36 163
f0a39880 164 private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> {
f37dc0dd
C
165 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/exist'
166 let params = new HttpParams()
167
168 params = this.restService.addObjectParams(params, { uris })
169
170 return this.authHttp.get<SubscriptionExistResult>(url, { params })
9270ccf6
RK
171 .pipe(
172 tap(res => {
173 this.myAccountSubscriptionCache = {
174 ...this.myAccountSubscriptionCache,
175 ...res
176 }
177 }),
178 catchError(err => this.restExtractor.handleError(err))
179 )
22a16e36
C
180 }
181}