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