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