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