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