X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Fshared-video-playlist%2Fvideo-playlist.service.ts;h=db9f78a7ab3e267b1c36f8e9cde778524788346e;hb=89e3de8dc6dbe3c78aefb5f43e0feffd97b1af02;hp=cc3d04b9e2f30254ddf52ae7a1026e1fc6f896be;hpb=67ed6552b831df66713bac9e672738796128d33f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/shared-video-playlist/video-playlist.service.ts b/client/src/app/shared/shared-video-playlist/video-playlist.service.ts index cc3d04b9e..db9f78a7a 100644 --- a/client/src/app/shared/shared-video-playlist/video-playlist.service.ts +++ b/client/src/app/shared/shared-video-playlist/video-playlist.service.ts @@ -1,12 +1,12 @@ import * as debug from 'debug' -import { uniq } from 'lodash-es' -import { asyncScheduler, merge, Observable, of, ReplaySubject, Subject } from 'rxjs' -import { bufferTime, catchError, filter, map, observeOn, share, switchMap, tap } from 'rxjs/operators' -import { HttpClient, HttpParams } from '@angular/common/http' -import { Injectable, NgZone } from '@angular/core' +import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs' +import { catchError, filter, map, share, switchMap, tap } from 'rxjs/operators' +import { HttpClient, HttpContext, HttpParams } from '@angular/common/http' +import { Injectable } from '@angular/core' import { AuthUser, ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core' -import { enterZone, leaveZone, objectToFormData } from '@app/helpers' +import { buildBulkObservable, objectToFormData } from '@app/helpers' import { Account, AccountService, VideoChannel, VideoChannelService } from '@app/shared/shared-main' +import { NGX_LOADING_BAR_IGNORED } from '@ngx-loading-bar/http-client' import { ResultList, VideoExistInPlaylist, @@ -48,19 +48,14 @@ export class VideoPlaylistService { private authHttp: HttpClient, private serverService: ServerService, private restExtractor: RestExtractor, - private restService: RestService, - private ngZone: NgZone + private restService: RestService ) { this.videoExistsInPlaylistObservable = merge( - this.videoExistsInPlaylistNotifier.pipe( - // We leave Angular zone so Protractor does not get stuck - bufferTime(500, leaveZone(this.ngZone, asyncScheduler)), - filter(videoIds => videoIds.length !== 0), - map(videoIds => uniq(videoIds)), - observeOn(enterZone(this.ngZone, asyncScheduler)), - switchMap(videoIds => this.doVideosExistInPlaylist(videoIds)), - share() - ), + buildBulkObservable({ + time: 500, + bulkGet: this.doVideosExistInPlaylist.bind(this), + notifierObservable: this.videoExistsInPlaylistNotifier + }).pipe(map(({ response }) => response)), this.videoExistsInPlaylistCacheSubject ) @@ -68,7 +63,7 @@ export class VideoPlaylistService { listChannelPlaylists (videoChannel: VideoChannel, componentPagination: ComponentPaginationLight): Observable> { const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists' - const pagination = this.restService.componentPaginationToRestPagination(componentPagination) + const pagination = this.restService.componentToRestPagination(componentPagination) let params = new HttpParams() params = this.restService.addRestGetParams(params, pagination) @@ -109,7 +104,7 @@ export class VideoPlaylistService { ): Observable> { const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists' const pagination = componentPagination - ? this.restService.componentPaginationToRestPagination(componentPagination) + ? this.restService.componentToRestPagination(componentPagination) : undefined let params = new HttpParams() @@ -159,7 +154,6 @@ export class VideoPlaylistService { return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data) .pipe( - map(this.restExtractor.extractDataBool), tap(() => { if (!this.myAccountPlaylistCache) return @@ -175,7 +169,6 @@ export class VideoPlaylistService { removeVideoPlaylist (videoPlaylist: VideoPlaylist) { return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id) .pipe( - map(this.restExtractor.extractDataBool), tap(() => { if (!this.myAccountPlaylistCache) return @@ -212,13 +205,15 @@ export class VideoPlaylistService { 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 + if (existsResult) { + const elem = existsResult.find(e => e.playlistElementId === playlistElementId) + + elem.startTimestamp = body.startTimestamp + elem.stopTimestamp = body.stopTimestamp + } this.runPlaylistCheck(videoId) }), @@ -229,11 +224,14 @@ export class VideoPlaylistService { 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) + if (this.videoExistsCache[videoId]) { + this.videoExistsCache[videoId] = this.videoExistsCache[videoId] + .filter(e => e.playlistElementId !== playlistElementId) + } + this.runPlaylistCheck(videoId) }), catchError(err => this.restExtractor.handleError(err)) @@ -247,18 +245,15 @@ export class VideoPlaylistService { } return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/reorder', body) - .pipe( - map(this.restExtractor.extractDataBool), - catchError(err => this.restExtractor.handleError(err)) - ) + .pipe(catchError(err => this.restExtractor.handleError(err))) } - getPlaylistVideos ( - videoPlaylistId: number | string, + getPlaylistVideos (options: { + videoPlaylistId: number | string componentPagination: ComponentPaginationLight - ): Observable> { - const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos' - const pagination = this.restService.componentPaginationToRestPagination(componentPagination) + }): Observable> { + const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + options.videoPlaylistId + '/videos' + const pagination = this.restService.componentToRestPagination(options.componentPagination) let params = new HttpParams() params = this.restService.addRestGetParams(params, pagination) @@ -276,18 +271,18 @@ export class VideoPlaylistService { } listenToVideoPlaylistChange (videoId: number) { - if (this.videoExistsObservableCache[ videoId ]) { - return this.videoExistsObservableCache[ videoId ] + if (this.videoExistsObservableCache[videoId]) { + return this.videoExistsObservableCache[videoId] } const obs = this.videoExistsInPlaylistObservable .pipe( - map(existsResult => existsResult[ videoId ]), + map(existsResult => existsResult[videoId]), filter(r => !!r), - tap(result => this.videoExistsCache[ videoId ] = result) + tap(result => this.videoExistsCache[videoId] = result) ) - this.videoExistsObservableCache[ videoId ] = obs + this.videoExistsObservableCache[videoId] = obs return obs } @@ -349,7 +344,7 @@ export class VideoPlaylistService { let params = new HttpParams() params = this.restService.addObjectParams(params, { videoIds }) - return this.authHttp.get(url, { params, headers: { ignoreLoadingBar: '' } }) + return this.authHttp.get(url, { params, context: new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) }) .pipe(catchError(err => this.restExtractor.handleError(err))) } }