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 { 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 { 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
this.videoExistsInPlaylistObservable = merge(
buildBulkObservable({
time: 500,
- 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)),
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 (options: {
}
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)
}