]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-video-playlist/video-playlist.service.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-playlist / video-playlist.service.ts
index 76835b9fcaf7a62250d9bf6fb699aec9d4a2870f..d71f8f72ee96876f8e3e0b22c79a841c0052cb93 100644 (file)
@@ -1,11 +1,12 @@
 import * as debug from 'debug'
 import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
 import { catchError, filter, map, share, switchMap, tap } from 'rxjs/operators'
-import { HttpClient, HttpParams } from '@angular/common/http'
-import { Injectable, NgZone } from '@angular/core'
-import { AuthUser, ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core'
+import { HttpClient, HttpContext, HttpParams } from '@angular/common/http'
+import { Injectable } from '@angular/core'
+import { AuthService, AuthUser, ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core'
 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,
@@ -22,7 +23,7 @@ import { environment } from '../../../environments/environment'
 import { VideoPlaylistElement } from './video-playlist-element.model'
 import { VideoPlaylist } from './video-playlist.model'
 
-const logger = debug('peertube:playlists:VideoPlaylistService')
+const debugLogger = debug('peertube:playlists:VideoPlaylistService')
 
 export type CachedPlaylist = VideoPlaylist | { id: number, displayName: string }
 
@@ -45,18 +46,24 @@ export class VideoPlaylistService {
 
   constructor (
     private authHttp: HttpClient,
+    private auth: AuthService,
     private serverService: ServerService,
     private restExtractor: RestExtractor,
-    private restService: RestService,
-    private ngZone: NgZone
+    private restService: RestService
   ) {
     this.videoExistsInPlaylistObservable = merge(
       buildBulkObservable({
         time: 500,
-        ngZone: this.ngZone,
-        bulkGet: this.doVideosExistInPlaylist.bind(this),
+        bulkGet: (videoIds: number[]) => {
+          // We added a delay to the request, so ensure the user is still logged in
+          if (this.auth.isLoggedIn()) {
+            return this.doVideosExistInPlaylist(videoIds)
+          }
+
+          return of({})
+        },
         notifierObservable: this.videoExistsInPlaylistNotifier
-      }),
+      }).pipe(map(({ response }) => response)),
 
       this.videoExistsInPlaylistCacheSubject
     )
@@ -64,7 +71,7 @@ export class VideoPlaylistService {
 
   listChannelPlaylists (videoChannel: VideoChannel, componentPagination: ComponentPaginationLight): Observable<ResultList<VideoPlaylist>> {
     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)
@@ -105,7 +112,7 @@ export class VideoPlaylistService {
   ): Observable<ResultList<VideoPlaylist>> {
     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()
@@ -155,7 +162,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
 
@@ -171,7 +177,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
 
@@ -208,7 +213,6 @@ 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]
 
@@ -228,7 +232,6 @@ 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
 
@@ -250,10 +253,7 @@ 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 (options: {
@@ -261,7 +261,7 @@ export class VideoPlaylistService {
     componentPagination: ComponentPaginationLight
   }): Observable<ResultList<VideoPlaylistElement>> {
     const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + options.videoPlaylistId + '/videos'
-    const pagination = this.restService.componentPaginationToRestPagination(options.componentPagination)
+    const pagination = this.restService.componentToRestPagination(options.componentPagination)
 
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination)
@@ -295,15 +295,15 @@ export class VideoPlaylistService {
   }
 
   runPlaylistCheck (videoId: number) {
-    logger('Running playlist check.')
+    debugLogger('Running playlist check.')
 
     if (this.videoExistsCache[videoId]) {
-      logger('Found cache for %d.', videoId)
+      debugLogger('Found cache for %d.', videoId)
 
       return this.videoExistsInPlaylistCacheSubject.next({ [videoId]: this.videoExistsCache[videoId] })
     }
 
-    logger('Fetching from network for %d.', videoId)
+    debugLogger('Fetching from network for %d.', videoId)
     return this.videoExistsInPlaylistNotifier.next(videoId)
   }
 
@@ -352,7 +352,7 @@ export class VideoPlaylistService {
     let params = new HttpParams()
     params = this.restService.addObjectParams(params, { videoIds })
 
-    return this.authHttp.get<VideoExistInPlaylist>(url, { params, headers: { ignoreLoadingBar: '' } })
+    return this.authHttp.get<VideoExistInPlaylist>(url, { params, context: new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) })
                .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 }