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