123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- 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, 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 {
- CachedVideoExistInPlaylist,
- CachedVideosExistInPlaylists,
- ResultList,
- VideoExistInPlaylist,
- VideoPlaylist as VideoPlaylistServerModel,
- VideoPlaylistCreate,
- VideoPlaylistElement as ServerVideoPlaylistElement,
- VideoPlaylistElementCreate,
- VideoPlaylistElementUpdate,
- VideoPlaylistReorder,
- VideoPlaylistUpdate,
- VideosExistInPlaylists
- } from '@shared/models'
- import { environment } from '../../../environments/environment'
- import { VideoPlaylistElement } from './video-playlist-element.model'
- import { VideoPlaylist } from './video-playlist.model'
- const debugLogger = debug('peertube:playlists:VideoPlaylistService')
- export type CachedPlaylist = VideoPlaylist | { id: number, displayName: string }
- @Injectable()
- export class VideoPlaylistService {
- static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
- static MY_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/users/me/video-playlists/'
- // Use a replay subject because we "next" a value before subscribing
- private videoExistsInPlaylistNotifier = new ReplaySubject<number>(1)
- private videoExistsInPlaylistCacheSubject = new Subject<CachedVideosExistInPlaylists>()
- private readonly videoExistsInPlaylistObservable: Observable<CachedVideosExistInPlaylists>
- private videoExistsObservableCache: { [ id: number ]: Observable<CachedVideoExistInPlaylist[]> } = {}
- private videoExistsCache: { [ id: number ]: CachedVideoExistInPlaylist[] } = {}
- private myAccountPlaylistCache: ResultList<CachedPlaylist> = undefined
- private myAccountPlaylistCacheRunning: Observable<ResultList<CachedPlaylist>>
- private myAccountPlaylistCacheSubject = new Subject<ResultList<CachedPlaylist>>()
- constructor (
- private authHttp: HttpClient,
- private auth: AuthService,
- private serverService: ServerService,
- private restExtractor: RestExtractor,
- private restService: RestService
- ) {
- this.videoExistsInPlaylistObservable = merge(
- 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.componentToRestPagination(componentPagination)
- let params = new HttpParams()
- params = this.restService.addRestGetParams(params, pagination)
- return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params })
- .pipe(
- switchMap(res => this.extractPlaylists(res)),
- catchError(err => this.restExtractor.handleError(err))
- )
- }
- listMyPlaylistWithCache (user: AuthUser, search?: string) {
- if (!search) {
- if (this.myAccountPlaylistCacheRunning) return this.myAccountPlaylistCacheRunning
- if (this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache)
- }
- const obs = this.listAccountPlaylists(user.account, undefined, '-updatedAt', search)
- .pipe(
- tap(result => {
- if (!search) {
- this.myAccountPlaylistCacheRunning = undefined
- this.myAccountPlaylistCache = result
- }
- }),
- share()
- )
- if (!search) this.myAccountPlaylistCacheRunning = obs
- return obs
- }
- listAccountPlaylists (
- account: Account,
- componentPagination: ComponentPaginationLight,
- sort: string,
- search?: string
- ): Observable<ResultList<VideoPlaylist>> {
- const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists'
- const pagination = componentPagination
- ? this.restService.componentToRestPagination(componentPagination)
- : undefined
- let params = new HttpParams()
- params = this.restService.addRestGetParams(params, pagination, sort)
- if (search) params = this.restService.addObjectParams(params, { search })
- return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params })
- .pipe(
- switchMap(res => this.extractPlaylists(res)),
- catchError(err => this.restExtractor.handleError(err))
- )
- }
- getVideoPlaylist (id: string | number) {
- const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + id
- return this.authHttp.get<VideoPlaylist>(url)
- .pipe(
- switchMap(res => this.extractPlaylist(res)),
- catchError(err => this.restExtractor.handleError(err))
- )
- }
- createVideoPlaylist (body: VideoPlaylistCreate) {
- const data = objectToFormData(body)
- return this.authHttp.post<{ videoPlaylist: { id: number } }>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data)
- .pipe(
- tap(res => {
- if (!this.myAccountPlaylistCache) return
- this.myAccountPlaylistCache.total++
- this.myAccountPlaylistCache.data.push({
- id: res.videoPlaylist.id,
- displayName: body.displayName
- })
- this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache)
- }),
- catchError(err => this.restExtractor.handleError(err))
- )
- }
- updateVideoPlaylist (videoPlaylist: VideoPlaylist, body: VideoPlaylistUpdate) {
- const data = objectToFormData(body)
- return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data)
- .pipe(
- tap(() => {
- if (!this.myAccountPlaylistCache) return
- const playlist = this.myAccountPlaylistCache.data.find(p => p.id === videoPlaylist.id)
- playlist.displayName = body.displayName
- this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache)
- }),
- catchError(err => this.restExtractor.handleError(err))
- )
- }
- removeVideoPlaylist (videoPlaylist: VideoPlaylist) {
- return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id)
- .pipe(
- tap(() => {
- if (!this.myAccountPlaylistCache) return
- this.myAccountPlaylistCache.total--
- this.myAccountPlaylistCache.data = this.myAccountPlaylistCache.data
- .filter(p => p.id !== videoPlaylist.id)
- this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache)
- }),
- catchError(err => this.restExtractor.handleError(err))
- )
- }
- 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(
- tap(() => {
- const existsResult = this.videoExistsCache[videoId]
- if (existsResult) {
- 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(
- tap(() => {
- if (!videoId) return
- if (this.videoExistsCache[videoId]) {
- 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(catchError(err => this.restExtractor.handleError(err)))
- }
- getPlaylistVideos (options: {
- videoPlaylistId: number | string
- componentPagination: ComponentPaginationLight
- }): 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)
- 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) {
- debugLogger('Running playlist check.')
- if (this.videoExistsCache[videoId]) {
- debugLogger('Found cache for %d.', videoId)
- return this.videoExistsInPlaylistCacheSubject.next({ [videoId]: this.videoExistsCache[videoId] })
- }
- debugLogger('Fetching from network for %d.', videoId)
- return this.videoExistsInPlaylistNotifier.next(videoId)
- }
- extractPlaylists (result: ResultList<VideoPlaylistServerModel>) {
- return this.serverService.getServerLocale()
- .pipe(
- map(translations => {
- const playlistsJSON = result.data
- const total = result.total
- const playlists: VideoPlaylist[] = []
- for (const playlistJSON of playlistsJSON) {
- playlists.push(new VideoPlaylist(playlistJSON, translations))
- }
- return { data: playlists, total }
- })
- )
- }
- extractPlaylist (playlist: VideoPlaylistServerModel) {
- return this.serverService.getServerLocale()
- .pipe(map(translations => new VideoPlaylist(playlist, translations)))
- }
- extractVideoPlaylistElements (result: ResultList<ServerVideoPlaylistElement>) {
- return this.serverService.getServerLocale()
- .pipe(
- map(translations => {
- const elementsJson = result.data
- const total = result.total
- const elements: VideoPlaylistElement[] = []
- for (const elementJson of elementsJson) {
- elements.push(new VideoPlaylistElement(elementJson, translations))
- }
- return { total, data: elements }
- })
- )
- }
- doVideosExistInPlaylist (videoIds: number[]): Observable<VideosExistInPlaylists> {
- const url = VideoPlaylistService.MY_VIDEO_PLAYLIST_URL + 'videos-exist'
- let params = new HttpParams()
- params = this.restService.addObjectParams(params, { videoIds })
- return this.authHttp.get<VideoExistInPlaylist>(url, { params, context: new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) })
- .pipe(catchError(err => this.restExtractor.handleError(err)))
- }
- }
|