+ addVideoInPlaylist (playlistId: number, body: VideoPlaylistElementCreate) {
+ const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos'
+
+ return this.authHttp.post<{ videoPlaylistElement: { id: number } }>(url, body)
+ .pipe(
+ tap(res => {
+ const existsResult = this.videoExistsCache[body.videoId]
+ existsResult.push({
+ playlistId,
+ playlistElementId: res.videoPlaylistElement.id,
+ startTimestamp: body.startTimestamp,
+ stopTimestamp: body.stopTimestamp
+ })
+
+ this.runPlaylistCheck(body.videoId)
+ }),
+ catchError(err => this.restExtractor.handleError(err))
+ )
+ }
+
+ updateVideoOfPlaylist (playlistId: number, playlistElementId: number, body: VideoPlaylistElementUpdate, videoId: number) {
+ return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId, body)
+ .pipe(
+ map(this.restExtractor.extractDataBool),
+ tap(() => {
+ const existsResult = this.videoExistsCache[videoId]
+ const elem = existsResult.find(e => e.playlistElementId === playlistElementId)
+
+ elem.startTimestamp = body.startTimestamp
+ elem.stopTimestamp = body.stopTimestamp
+
+ this.runPlaylistCheck(videoId)
+ }),
+ catchError(err => this.restExtractor.handleError(err))
+ )
+ }
+
+ removeVideoFromPlaylist (playlistId: number, playlistElementId: number, videoId?: number) {
+ return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId)
+ .pipe(
+ map(this.restExtractor.extractDataBool),
+ tap(() => {
+ if (!videoId) return
+
+ this.videoExistsCache[videoId] = this.videoExistsCache[videoId].filter(e => e.playlistElementId !== playlistElementId)
+ this.runPlaylistCheck(videoId)
+ }),
+ catchError(err => this.restExtractor.handleError(err))
+ )
+ }
+
+ reorderPlaylist (playlistId: number, oldPosition: number, newPosition: number) {
+ const body: VideoPlaylistReorder = {
+ startPosition: oldPosition,
+ insertAfterPosition: newPosition
+ }
+
+ return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/reorder', body)
+ .pipe(
+ map(this.restExtractor.extractDataBool),
+ catchError(err => this.restExtractor.handleError(err))
+ )
+ }
+
+ getPlaylistVideos (
+ videoPlaylistId: number | string,
+ componentPagination: ComponentPagination
+ ): Observable<ResultList<VideoPlaylistElement>> {
+ const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos'
+ const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
+
+ let params = new HttpParams()
+ params = this.restService.addRestGetParams(params, pagination)
+
+ return this.authHttp
+ .get<ResultList<ServerVideoPlaylistElement>>(path, { params })
+ .pipe(
+ switchMap(res => this.extractVideoPlaylistElements(res)),
+ catchError(err => this.restExtractor.handleError(err))
+ )
+ }
+
+ listenToMyAccountPlaylistsChange () {
+ return this.myAccountPlaylistCacheSubject.asObservable()
+ }
+
+ listenToVideoPlaylistChange (videoId: number) {
+ if (this.videoExistsObservableCache[ videoId ]) {
+ return this.videoExistsObservableCache[ videoId ]
+ }
+
+ const obs = this.videoExistsInPlaylistObservable
+ .pipe(
+ map(existsResult => existsResult[ videoId ]),
+ filter(r => !!r),
+ tap(result => this.videoExistsCache[ videoId ] = result)
+ )
+
+ this.videoExistsObservableCache[ videoId ] = obs
+ return obs
+ }
+
+ runPlaylistCheck (videoId: number) {
+ logger('Running playlist check.')
+
+ if (this.videoExistsCache[videoId]) {
+ logger('Found cache for %d.', videoId)
+
+ return this.videoExistsInPlaylistCacheSubject.next({ [videoId]: this.videoExistsCache[videoId] })
+ }
+
+ logger('Fetching from network for %d.', videoId)
+ return this.videoExistsInPlaylistNotifier.next(videoId)
+ }
+