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