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 { AuthUser, ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core'
-import { enterZone, leaveZone, objectToFormData } from '@app/helpers'
+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 { 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,
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 }
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(
- 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: (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
)
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)
): 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()
return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data)
.pipe(
- map(this.restExtractor.extractDataBool),
tap(() => {
if (!this.myAccountPlaylistCache) return
removeVideoPlaylist (videoPlaylist: VideoPlaylist) {
return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id)
.pipe(
- map(this.restExtractor.extractDataBool),
tap(() => {
if (!this.myAccountPlaylistCache) return
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]
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
}
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<ResultList<VideoPlaylistElement>> {
- const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos'
- const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
+ }): Observable<ResultList<VideoPlaylistElement>> {
+ 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)
}
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
}
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)
}
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)))
}
}