]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/user-subscription/user-subscription.service.ts
Update translations
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / user-subscription / user-subscription.service.ts
1 import { bufferTime, catchError, filter, map, tap, share, switchMap } from 'rxjs/operators'
2 import { Observable, ReplaySubject, Subject, of, merge } from 'rxjs'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { ResultList } from '../../../../../shared'
6 import { environment } from '../../../environments/environment'
7 import { RestExtractor, RestService } from '../rest'
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 { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model'
12 import { uniq } from 'lodash-es'
13 import * as debug from 'debug'
14
15 const logger = debug('peertube:subscriptions:UserSubscriptionService')
16
17 type SubscriptionExistResult = { [ uri: string ]: boolean }
18 type SubscriptionExistResultObservable = { [ uri: string ]: Observable<boolean> }
19
20 @Injectable()
21 export class UserSubscriptionService {
22 static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions'
23
24 // Use a replay subject because we "next" a value before subscribing
25 private existsSubject = new ReplaySubject<string>(1)
26 private readonly existsObservable: Observable<SubscriptionExistResult>
27
28 private myAccountSubscriptionCache: SubscriptionExistResult = {}
29 private myAccountSubscriptionCacheObservable: SubscriptionExistResultObservable = {}
30 private myAccountSubscriptionCacheSubject = new Subject<SubscriptionExistResult>()
31
32 constructor (
33 private authHttp: HttpClient,
34 private restExtractor: RestExtractor,
35 private restService: RestService
36 ) {
37 this.existsObservable = merge(
38 this.existsSubject.pipe(
39 bufferTime(500),
40 filter(uris => uris.length !== 0),
41 map(uris => uniq(uris)),
42 switchMap(uris => this.doSubscriptionsExist(uris)),
43 share()
44 ),
45
46 this.myAccountSubscriptionCacheSubject
47 )
48 }
49
50 /**
51 * Subscription part
52 */
53
54 deleteSubscription (nameWithHost: string) {
55 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost
56
57 return this.authHttp.delete(url)
58 .pipe(
59 map(this.restExtractor.extractDataBool),
60 tap(() => {
61 this.myAccountSubscriptionCache[nameWithHost] = false
62
63 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
64 }),
65 catchError(err => this.restExtractor.handleError(err))
66 )
67 }
68
69 addSubscription (nameWithHost: string) {
70 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
71
72 const body = { uri: nameWithHost }
73 return this.authHttp.post(url, body)
74 .pipe(
75 map(this.restExtractor.extractDataBool),
76 tap(() => {
77 this.myAccountSubscriptionCache[nameWithHost] = true
78
79 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
80 }),
81 catchError(err => this.restExtractor.handleError(err))
82 )
83 }
84
85 listSubscriptions (componentPagination: ComponentPaginationLight): Observable<ResultList<VideoChannel>> {
86 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
87
88 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
89
90 let params = new HttpParams()
91 params = this.restService.addRestGetParams(params, pagination)
92
93 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
94 .pipe(
95 map(res => VideoChannelService.extractVideoChannels(res)),
96 catchError(err => this.restExtractor.handleError(err))
97 )
98 }
99
100 /**
101 * SubscriptionExist part
102 */
103
104 listenToMyAccountSubscriptionCacheSubject () {
105 return this.myAccountSubscriptionCacheSubject.asObservable()
106 }
107
108 listenToSubscriptionCacheChange (nameWithHost: string) {
109 if (nameWithHost in this.myAccountSubscriptionCacheObservable) {
110 return this.myAccountSubscriptionCacheObservable[ nameWithHost ]
111 }
112
113 const obs = this.existsObservable
114 .pipe(
115 filter(existsResult => existsResult[ nameWithHost ] !== undefined),
116 map(existsResult => existsResult[ nameWithHost ])
117 )
118
119 this.myAccountSubscriptionCacheObservable[ nameWithHost ] = obs
120 return obs
121 }
122
123 doesSubscriptionExist (nameWithHost: string) {
124 logger('Running subscription check for %d.', nameWithHost)
125
126 if (nameWithHost in this.myAccountSubscriptionCache) {
127 logger('Found cache for %d.', nameWithHost)
128
129 return of(this.myAccountSubscriptionCache[ nameWithHost ])
130 }
131
132 this.existsSubject.next(nameWithHost)
133
134 logger('Fetching from network for %d.', nameWithHost)
135 return this.existsObservable.pipe(
136 filter(existsResult => existsResult[ nameWithHost ] !== undefined),
137 map(existsResult => existsResult[ nameWithHost ]),
138 tap(result => this.myAccountSubscriptionCache[ nameWithHost ] = result)
139 )
140 }
141
142 private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> {
143 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/exist'
144 let params = new HttpParams()
145
146 params = this.restService.addObjectParams(params, { uris })
147
148 return this.authHttp.get<SubscriptionExistResult>(url, { params })
149 .pipe(
150 tap(res => {
151 this.myAccountSubscriptionCache = {
152 ...this.myAccountSubscriptionCache,
153 ...res
154 }
155 }),
156 catchError(err => this.restExtractor.handleError(err))
157 )
158 }
159 }