]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-subscription/user-subscription.service.ts
Increase global font size
[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 8import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
4beda9e1 9import { ActorFollow, 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'
4beda9e1
C
20 static BASE_VIDEO_CHANNELS_URL = environment.apiUrl + '/api/v1/video-channels'
21 static BASE_ACCOUNTS_URL = environment.apiUrl + '/api/v1/accounts'
22a16e36 22
f37dc0dd 23 // Use a replay subject because we "next" a value before subscribing
9270ccf6 24 private existsSubject = new ReplaySubject<string>(1)
aa55a4da 25 private readonly existsObservable: Observable<SubscriptionExistResult>
f37dc0dd 26
9270ccf6
RK
27 private myAccountSubscriptionCache: SubscriptionExistResult = {}
28 private myAccountSubscriptionCacheObservable: SubscriptionExistResultObservable = {}
29 private myAccountSubscriptionCacheSubject = new Subject<SubscriptionExistResult>()
30
22a16e36
C
31 constructor (
32 private authHttp: HttpClient,
f37dc0dd 33 private restExtractor: RestExtractor,
67ed6552 34 private videoService: VideoService,
afb7d2d5 35 private restService: RestService
22a16e36 36 ) {
9270ccf6 37 this.existsObservable = merge(
3da38d6e
C
38 buildBulkObservable({
39 time: 500,
3da38d6e
C
40 notifierObservable: this.existsSubject,
41 bulkGet: this.doSubscriptionsExist.bind(this)
2cc276f9 42 }).pipe(map(r => r.response)),
9270ccf6
RK
43
44 this.myAccountSubscriptionCacheSubject
f37dc0dd 45 )
22a16e36
C
46 }
47
4beda9e1
C
48 listFollowers (parameters: {
49 pagination: ComponentPaginationLight
50 nameWithHost: string
51 search?: string
52 }) {
53 const { pagination, nameWithHost, search } = parameters
54
55 let url = `${UserSubscriptionService.BASE_ACCOUNTS_URL}/${nameWithHost}/followers`
56
57 let params = new HttpParams()
58 params = this.restService.addRestGetParams(params, this.restService.componentToRestPagination(pagination), '-createdAt')
59
60 if (search) {
61 const filters = this.restService.parseQueryStringFilter(search, {
62 channel: {
63 prefix: 'channel:'
64 }
65 })
66
67 if (filters.channel) {
68 url = `${UserSubscriptionService.BASE_VIDEO_CHANNELS_URL}/${filters.channel}/followers`
69 }
70
71 params = this.restService.addObjectParams(params, { search: filters.search })
72 }
73
74 return this.authHttp
75 .get<ResultList<ActorFollow>>(url, { params })
76 .pipe(
77 catchError(err => this.restExtractor.handleError(err))
78 )
79 }
80
67ed6552 81 getUserSubscriptionVideos (parameters: {
9df52d66
C
82 videoPagination: ComponentPaginationLight
83 sort: VideoSortField
67ed6552
C
84 skipCount?: boolean
85 }): Observable<ResultList<Video>> {
86 const { videoPagination, sort, skipCount } = parameters
4beda9e1 87 const pagination = this.restService.componentToRestPagination(videoPagination)
67ed6552
C
88
89 let params = new HttpParams()
90 params = this.restService.addRestGetParams(params, pagination, sort)
91
92 if (skipCount) params = params.set('skipCount', skipCount + '')
93
94 return this.authHttp
95 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
96 .pipe(
97 switchMap(res => this.videoService.extractVideos(res)),
98 catchError(err => this.restExtractor.handleError(err))
99 )
100 }
101
9270ccf6
RK
102 /**
103 * Subscription part
104 */
105
22a16e36
C
106 deleteSubscription (nameWithHost: string) {
107 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost
108
109 return this.authHttp.delete(url)
110 .pipe(
9270ccf6
RK
111 tap(() => {
112 this.myAccountSubscriptionCache[nameWithHost] = false
113
114 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
115 }),
22a16e36
C
116 catchError(err => this.restExtractor.handleError(err))
117 )
118 }
119
120 addSubscription (nameWithHost: string) {
121 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
122
123 const body = { uri: nameWithHost }
124 return this.authHttp.post(url, body)
125 .pipe(
9270ccf6
RK
126 tap(() => {
127 this.myAccountSubscriptionCache[nameWithHost] = true
128
129 this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache)
130 }),
22a16e36
C
131 catchError(err => this.restExtractor.handleError(err))
132 )
133 }
134
4f5d0459
RK
135 listSubscriptions (parameters: {
136 pagination: ComponentPaginationLight
137 search: string
138 }): Observable<ResultList<VideoChannel>> {
139 const { pagination, search } = parameters
22a16e36
C
140 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL
141
4beda9e1 142 const restPagination = this.restService.componentToRestPagination(pagination)
aa55a4da
C
143
144 let params = new HttpParams()
4f5d0459
RK
145 params = this.restService.addRestGetParams(params, restPagination)
146 if (search) params = params.append('search', search)
aa55a4da
C
147
148 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
22a16e36
C
149 .pipe(
150 map(res => VideoChannelService.extractVideoChannels(res)),
151 catchError(err => this.restExtractor.handleError(err))
152 )
153 }
154
9270ccf6
RK
155 /**
156 * SubscriptionExist part
157 */
158
159 listenToMyAccountSubscriptionCacheSubject () {
160 return this.myAccountSubscriptionCacheSubject.asObservable()
161 }
162
163 listenToSubscriptionCacheChange (nameWithHost: string) {
164 if (nameWithHost in this.myAccountSubscriptionCacheObservable) {
9df52d66 165 return this.myAccountSubscriptionCacheObservable[nameWithHost]
9270ccf6
RK
166 }
167
168 const obs = this.existsObservable
169 .pipe(
9df52d66
C
170 filter(existsResult => existsResult[nameWithHost] !== undefined),
171 map(existsResult => existsResult[nameWithHost])
9270ccf6
RK
172 )
173
9df52d66 174 this.myAccountSubscriptionCacheObservable[nameWithHost] = obs
9270ccf6
RK
175 return obs
176 }
177
f0a39880 178 doesSubscriptionExist (nameWithHost: string) {
9270ccf6
RK
179 logger('Running subscription check for %d.', nameWithHost)
180
181 if (nameWithHost in this.myAccountSubscriptionCache) {
182 logger('Found cache for %d.', nameWithHost)
183
9df52d66 184 return of(this.myAccountSubscriptionCache[nameWithHost])
9270ccf6
RK
185 }
186
f37dc0dd 187 this.existsSubject.next(nameWithHost)
22a16e36 188
9270ccf6
RK
189 logger('Fetching from network for %d.', nameWithHost)
190 return this.existsObservable.pipe(
9df52d66
C
191 filter(existsResult => existsResult[nameWithHost] !== undefined),
192 map(existsResult => existsResult[nameWithHost]),
193 tap(result => this.myAccountSubscriptionCache[nameWithHost] = result)
9270ccf6 194 )
f37dc0dd 195 }
22a16e36 196
f0a39880 197 private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> {
f37dc0dd
C
198 const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/exist'
199 let params = new HttpParams()
200
201 params = this.restService.addObjectParams(params, { uris })
202
203 return this.authHttp.get<SubscriptionExistResult>(url, { params })
9270ccf6
RK
204 .pipe(
205 tap(res => {
206 this.myAccountSubscriptionCache = {
207 ...this.myAccountSubscriptionCache,
208 ...res
209 }
210 }),
211 catchError(err => this.restExtractor.handleError(err))
212 )
22a16e36
C
213 }
214}