1 import { Observable, ReplaySubject } from 'rxjs'
2 import { catchError, map, tap } from 'rxjs/operators'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { ComponentPaginationLight, RestExtractor, RestService } from '@app/core'
9 VideoChannel as VideoChannelServer,
12 VideosImportInChannelCreate
13 } from '@shared/models'
14 import { environment } from '../../../../environments/environment'
15 import { Account } from '../account'
16 import { AccountService } from '../account/account.service'
17 import { VideoChannel } from './video-channel.model'
20 export class VideoChannelService {
21 static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
23 videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
26 private authHttp: HttpClient,
27 private restService: RestService,
28 private restExtractor: RestExtractor
31 static extractVideoChannels (result: ResultList<VideoChannelServer>) {
32 const videoChannels: VideoChannel[] = []
34 for (const videoChannelJSON of result.data) {
35 videoChannels.push(new VideoChannel(videoChannelJSON))
38 return { data: videoChannels, total: result.total }
41 getVideoChannel (videoChannelName: string) {
42 return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
44 map(videoChannelHash => new VideoChannel(videoChannelHash)),
45 tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
46 catchError(err => this.restExtractor.handleError(err))
50 listAccountVideoChannels (options: {
52 componentPagination?: ComponentPaginationLight
56 }): Observable<ResultList<VideoChannel>> {
57 const { account, componentPagination, withStats = false, sort, search } = options
59 const pagination = componentPagination
60 ? this.restService.componentToRestPagination(componentPagination)
61 : { start: 0, count: 20 }
63 let params = new HttpParams()
64 params = this.restService.addRestGetParams(params, pagination, sort)
65 params = params.set('withStats', withStats + '')
67 if (search) params = params.set('search', search)
69 const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
70 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
72 map(res => VideoChannelService.extractVideoChannels(res)),
73 catchError(err => this.restExtractor.handleError(err))
77 createVideoChannel (videoChannel: VideoChannelCreate) {
78 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
79 .pipe(catchError(err => this.restExtractor.handleError(err)))
82 updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
83 return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
84 .pipe(catchError(err => this.restExtractor.handleError(err)))
87 changeVideoChannelImage (videoChannelName: string, avatarForm: FormData, type: 'avatar' | 'banner') {
88 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type + '/pick'
90 return this.authHttp.post<{ avatars?: ActorImage[], banners?: ActorImage[] }>(url, avatarForm)
91 .pipe(catchError(err => this.restExtractor.handleError(err)))
94 deleteVideoChannelImage (videoChannelName: string, type: 'avatar' | 'banner') {
95 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type
97 return this.authHttp.delete(url)
98 .pipe(catchError(err => this.restExtractor.handleError(err)))
101 removeVideoChannel (videoChannel: VideoChannel) {
102 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
103 .pipe(catchError(err => this.restExtractor.handleError(err)))
106 importVideos (videoChannelName: string, externalChannelUrl: string, syncId?: number) {
107 const path = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/import-videos'
109 const body: VideosImportInChannelCreate = {
111 videoChannelSyncId: syncId
114 return this.authHttp.post(path, body)
115 .pipe(catchError(err => this.restExtractor.handleError(err)))