]>
Commit | Line | Data |
---|---|---|
1 | import * as debug from 'debug' | |
2 | import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs' | |
3 | import { catchError, filter, map, share, switchMap, tap } from 'rxjs/operators' | |
4 | import { HttpClient, HttpContext, HttpParams } from '@angular/common/http' | |
5 | import { Injectable } from '@angular/core' | |
6 | import { AuthService, AuthUser, ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core' | |
7 | import { buildBulkObservable, objectToFormData } from '@app/helpers' | |
8 | import { Account, AccountService, VideoChannel, VideoChannelService } from '@app/shared/shared-main' | |
9 | import { NGX_LOADING_BAR_IGNORED } from '@ngx-loading-bar/http-client' | |
10 | import { | |
11 | CachedVideoExistInPlaylist, | |
12 | CachedVideosExistInPlaylists, | |
13 | ResultList, | |
14 | VideoExistInPlaylist, | |
15 | VideoPlaylist as VideoPlaylistServerModel, | |
16 | VideoPlaylistCreate, | |
17 | VideoPlaylistElement as ServerVideoPlaylistElement, | |
18 | VideoPlaylistElementCreate, | |
19 | VideoPlaylistElementUpdate, | |
20 | VideoPlaylistReorder, | |
21 | VideoPlaylistUpdate, | |
22 | VideosExistInPlaylists | |
23 | } from '@shared/models' | |
24 | import { environment } from '../../../environments/environment' | |
25 | import { VideoPlaylistElement } from './video-playlist-element.model' | |
26 | import { VideoPlaylist } from './video-playlist.model' | |
27 | ||
28 | const debugLogger = debug('peertube:playlists:VideoPlaylistService') | |
29 | ||
30 | export type CachedPlaylist = VideoPlaylist | { id: number, displayName: string } | |
31 | ||
32 | @Injectable() | |
33 | export class VideoPlaylistService { | |
34 | static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' | |
35 | static MY_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/users/me/video-playlists/' | |
36 | ||
37 | // Use a replay subject because we "next" a value before subscribing | |
38 | private videoExistsInPlaylistNotifier = new ReplaySubject<number>(1) | |
39 | private videoExistsInPlaylistCacheSubject = new Subject<CachedVideosExistInPlaylists>() | |
40 | private readonly videoExistsInPlaylistObservable: Observable<CachedVideosExistInPlaylists> | |
41 | ||
42 | private videoExistsObservableCache: { [ id: number ]: Observable<CachedVideoExistInPlaylist[]> } = {} | |
43 | private videoExistsCache: { [ id: number ]: CachedVideoExistInPlaylist[] } = {} | |
44 | ||
45 | private myAccountPlaylistCache: ResultList<CachedPlaylist> = undefined | |
46 | private myAccountPlaylistCacheRunning: Observable<ResultList<CachedPlaylist>> | |
47 | private myAccountPlaylistCacheSubject = new Subject<ResultList<CachedPlaylist>>() | |
48 | ||
49 | constructor ( | |
50 | private authHttp: HttpClient, | |
51 | private auth: AuthService, | |
52 | private serverService: ServerService, | |
53 | private restExtractor: RestExtractor, | |
54 | private restService: RestService | |
55 | ) { | |
56 | this.videoExistsInPlaylistObservable = merge( | |
57 | buildBulkObservable({ | |
58 | time: 500, | |
59 | bulkGet: (videoIds: number[]) => { | |
60 | // We added a delay to the request, so ensure the user is still logged in | |
61 | if (this.auth.isLoggedIn()) { | |
62 | return this.doVideosExistInPlaylist(videoIds) | |
63 | } | |
64 | ||
65 | return of({}) | |
66 | }, | |
67 | notifierObservable: this.videoExistsInPlaylistNotifier | |
68 | }).pipe(map(({ response }) => response)), | |
69 | ||
70 | this.videoExistsInPlaylistCacheSubject | |
71 | ) | |
72 | } | |
73 | ||
74 | listChannelPlaylists (videoChannel: VideoChannel, componentPagination: ComponentPaginationLight): Observable<ResultList<VideoPlaylist>> { | |
75 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists' | |
76 | const pagination = this.restService.componentToRestPagination(componentPagination) | |
77 | ||
78 | let params = new HttpParams() | |
79 | params = this.restService.addRestGetParams(params, pagination) | |
80 | ||
81 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | |
82 | .pipe( | |
83 | switchMap(res => this.extractPlaylists(res)), | |
84 | catchError(err => this.restExtractor.handleError(err)) | |
85 | ) | |
86 | } | |
87 | ||
88 | listMyPlaylistWithCache (user: AuthUser, search?: string) { | |
89 | if (!search) { | |
90 | if (this.myAccountPlaylistCacheRunning) return this.myAccountPlaylistCacheRunning | |
91 | if (this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache) | |
92 | } | |
93 | ||
94 | const obs = this.listAccountPlaylists(user.account, undefined, '-updatedAt', search) | |
95 | .pipe( | |
96 | tap(result => { | |
97 | if (!search) { | |
98 | this.myAccountPlaylistCacheRunning = undefined | |
99 | this.myAccountPlaylistCache = result | |
100 | } | |
101 | }), | |
102 | share() | |
103 | ) | |
104 | ||
105 | if (!search) this.myAccountPlaylistCacheRunning = obs | |
106 | return obs | |
107 | } | |
108 | ||
109 | listAccountPlaylists ( | |
110 | account: Account, | |
111 | componentPagination: ComponentPaginationLight, | |
112 | sort: string, | |
113 | search?: string | |
114 | ): Observable<ResultList<VideoPlaylist>> { | |
115 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists' | |
116 | const pagination = componentPagination | |
117 | ? this.restService.componentToRestPagination(componentPagination) | |
118 | : undefined | |
119 | ||
120 | let params = new HttpParams() | |
121 | params = this.restService.addRestGetParams(params, pagination, sort) | |
122 | if (search) params = this.restService.addObjectParams(params, { search }) | |
123 | ||
124 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | |
125 | .pipe( | |
126 | switchMap(res => this.extractPlaylists(res)), | |
127 | catchError(err => this.restExtractor.handleError(err)) | |
128 | ) | |
129 | } | |
130 | ||
131 | getVideoPlaylist (id: string | number) { | |
132 | const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + id | |
133 | ||
134 | return this.authHttp.get<VideoPlaylist>(url) | |
135 | .pipe( | |
136 | switchMap(res => this.extractPlaylist(res)), | |
137 | catchError(err => this.restExtractor.handleError(err)) | |
138 | ) | |
139 | } | |
140 | ||
141 | createVideoPlaylist (body: VideoPlaylistCreate) { | |
142 | const data = objectToFormData(body) | |
143 | ||
144 | return this.authHttp.post<{ videoPlaylist: { id: number } }>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data) | |
145 | .pipe( | |
146 | tap(res => { | |
147 | if (!this.myAccountPlaylistCache) return | |
148 | ||
149 | this.myAccountPlaylistCache.total++ | |
150 | ||
151 | this.myAccountPlaylistCache.data.push({ | |
152 | id: res.videoPlaylist.id, | |
153 | displayName: body.displayName | |
154 | }) | |
155 | ||
156 | this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache) | |
157 | }), | |
158 | catchError(err => this.restExtractor.handleError(err)) | |
159 | ) | |
160 | } | |
161 | ||
162 | updateVideoPlaylist (videoPlaylist: VideoPlaylist, body: VideoPlaylistUpdate) { | |
163 | const data = objectToFormData(body) | |
164 | ||
165 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data) | |
166 | .pipe( | |
167 | tap(() => { | |
168 | if (!this.myAccountPlaylistCache) return | |
169 | ||
170 | const playlist = this.myAccountPlaylistCache.data.find(p => p.id === videoPlaylist.id) | |
171 | playlist.displayName = body.displayName | |
172 | ||
173 | this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache) | |
174 | }), | |
175 | catchError(err => this.restExtractor.handleError(err)) | |
176 | ) | |
177 | } | |
178 | ||
179 | removeVideoPlaylist (videoPlaylist: VideoPlaylist) { | |
180 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id) | |
181 | .pipe( | |
182 | tap(() => { | |
183 | if (!this.myAccountPlaylistCache) return | |
184 | ||
185 | this.myAccountPlaylistCache.total-- | |
186 | this.myAccountPlaylistCache.data = this.myAccountPlaylistCache.data | |
187 | .filter(p => p.id !== videoPlaylist.id) | |
188 | ||
189 | this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache) | |
190 | }), | |
191 | catchError(err => this.restExtractor.handleError(err)) | |
192 | ) | |
193 | } | |
194 | ||
195 | addVideoInPlaylist (playlistId: number, body: VideoPlaylistElementCreate) { | |
196 | const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos' | |
197 | ||
198 | return this.authHttp.post<{ videoPlaylistElement: { id: number } }>(url, body) | |
199 | .pipe( | |
200 | tap(res => { | |
201 | const existsResult = this.videoExistsCache[body.videoId] | |
202 | existsResult.push({ | |
203 | playlistId, | |
204 | playlistElementId: res.videoPlaylistElement.id, | |
205 | startTimestamp: body.startTimestamp, | |
206 | stopTimestamp: body.stopTimestamp | |
207 | }) | |
208 | ||
209 | this.runVideoExistsInPlaylistCheck(body.videoId) | |
210 | ||
211 | if (this.myAccountPlaylistCache) { | |
212 | const playlist = this.myAccountPlaylistCache.data.find(p => p.id === playlistId) | |
213 | if (!playlist) return | |
214 | ||
215 | const otherPlaylists = this.myAccountPlaylistCache.data.filter(p => p !== playlist) | |
216 | this.myAccountPlaylistCache.data = [ playlist, ...otherPlaylists ] | |
217 | } | |
218 | }), | |
219 | catchError(err => this.restExtractor.handleError(err)) | |
220 | ) | |
221 | } | |
222 | ||
223 | updateVideoOfPlaylist (playlistId: number, playlistElementId: number, body: VideoPlaylistElementUpdate, videoId: number) { | |
224 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId, body) | |
225 | .pipe( | |
226 | tap(() => { | |
227 | const existsResult = this.videoExistsCache[videoId] | |
228 | ||
229 | if (existsResult) { | |
230 | const elem = existsResult.find(e => e.playlistElementId === playlistElementId) | |
231 | ||
232 | elem.startTimestamp = body.startTimestamp | |
233 | elem.stopTimestamp = body.stopTimestamp | |
234 | } | |
235 | ||
236 | this.runVideoExistsInPlaylistCheck(videoId) | |
237 | }), | |
238 | catchError(err => this.restExtractor.handleError(err)) | |
239 | ) | |
240 | } | |
241 | ||
242 | removeVideoFromPlaylist (playlistId: number, playlistElementId: number, videoId?: number) { | |
243 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId) | |
244 | .pipe( | |
245 | tap(() => { | |
246 | if (!videoId) return | |
247 | ||
248 | if (this.videoExistsCache[videoId]) { | |
249 | this.videoExistsCache[videoId] = this.videoExistsCache[videoId] | |
250 | .filter(e => e.playlistElementId !== playlistElementId) | |
251 | } | |
252 | ||
253 | this.runVideoExistsInPlaylistCheck(videoId) | |
254 | }), | |
255 | catchError(err => this.restExtractor.handleError(err)) | |
256 | ) | |
257 | } | |
258 | ||
259 | reorderPlaylist (playlistId: number, oldPosition: number, newPosition: number) { | |
260 | const body: VideoPlaylistReorder = { | |
261 | startPosition: oldPosition, | |
262 | insertAfterPosition: newPosition | |
263 | } | |
264 | ||
265 | return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/reorder', body) | |
266 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
267 | } | |
268 | ||
269 | getPlaylistVideos (options: { | |
270 | videoPlaylistId: number | string | |
271 | componentPagination: ComponentPaginationLight | |
272 | }): Observable<ResultList<VideoPlaylistElement>> { | |
273 | const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + options.videoPlaylistId + '/videos' | |
274 | const pagination = this.restService.componentToRestPagination(options.componentPagination) | |
275 | ||
276 | let params = new HttpParams() | |
277 | params = this.restService.addRestGetParams(params, pagination) | |
278 | ||
279 | return this.authHttp | |
280 | .get<ResultList<ServerVideoPlaylistElement>>(path, { params }) | |
281 | .pipe( | |
282 | switchMap(res => this.extractVideoPlaylistElements(res)), | |
283 | catchError(err => this.restExtractor.handleError(err)) | |
284 | ) | |
285 | } | |
286 | ||
287 | listenToMyAccountPlaylistsChange () { | |
288 | return this.myAccountPlaylistCacheSubject.asObservable() | |
289 | } | |
290 | ||
291 | listenToVideoPlaylistChange (videoId: number) { | |
292 | if (this.videoExistsObservableCache[videoId]) { | |
293 | return this.videoExistsObservableCache[videoId] | |
294 | } | |
295 | ||
296 | const obs = this.videoExistsInPlaylistObservable | |
297 | .pipe( | |
298 | map(existsResult => existsResult[videoId]), | |
299 | filter(r => !!r), | |
300 | tap(result => this.videoExistsCache[videoId] = result) | |
301 | ) | |
302 | ||
303 | this.videoExistsObservableCache[videoId] = obs | |
304 | return obs | |
305 | } | |
306 | ||
307 | runVideoExistsInPlaylistCheck (videoId: number) { | |
308 | debugLogger('Running playlist check.') | |
309 | ||
310 | if (this.videoExistsCache[videoId]) { | |
311 | debugLogger('Found cache for %d.', videoId) | |
312 | ||
313 | return this.videoExistsInPlaylistCacheSubject.next({ [videoId]: this.videoExistsCache[videoId] }) | |
314 | } | |
315 | ||
316 | debugLogger('Fetching from network for %d.', videoId) | |
317 | return this.videoExistsInPlaylistNotifier.next(videoId) | |
318 | } | |
319 | ||
320 | extractPlaylists (result: ResultList<VideoPlaylistServerModel>) { | |
321 | return this.serverService.getServerLocale() | |
322 | .pipe( | |
323 | map(translations => { | |
324 | const playlistsJSON = result.data | |
325 | const total = result.total | |
326 | const playlists: VideoPlaylist[] = [] | |
327 | ||
328 | for (const playlistJSON of playlistsJSON) { | |
329 | playlists.push(new VideoPlaylist(playlistJSON, translations)) | |
330 | } | |
331 | ||
332 | return { data: playlists, total } | |
333 | }) | |
334 | ) | |
335 | } | |
336 | ||
337 | extractPlaylist (playlist: VideoPlaylistServerModel) { | |
338 | return this.serverService.getServerLocale() | |
339 | .pipe(map(translations => new VideoPlaylist(playlist, translations))) | |
340 | } | |
341 | ||
342 | extractVideoPlaylistElements (result: ResultList<ServerVideoPlaylistElement>) { | |
343 | return this.serverService.getServerLocale() | |
344 | .pipe( | |
345 | map(translations => { | |
346 | const elementsJson = result.data | |
347 | const total = result.total | |
348 | const elements: VideoPlaylistElement[] = [] | |
349 | ||
350 | for (const elementJson of elementsJson) { | |
351 | elements.push(new VideoPlaylistElement(elementJson, translations)) | |
352 | } | |
353 | ||
354 | return { total, data: elements } | |
355 | }) | |
356 | ) | |
357 | } | |
358 | ||
359 | doVideosExistInPlaylist (videoIds: number[]): Observable<VideosExistInPlaylists> { | |
360 | const url = VideoPlaylistService.MY_VIDEO_PLAYLIST_URL + 'videos-exist' | |
361 | ||
362 | let params = new HttpParams() | |
363 | params = this.restService.addObjectParams(params, { videoIds }) | |
364 | ||
365 | return this.authHttp.get<VideoExistInPlaylist>(url, { params, context: new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) }) | |
366 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
367 | } | |
368 | } |