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, ServerService } 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,
29 private serverService: ServerService
32 static extractVideoChannels (result: ResultList<VideoChannelServer>) {
33 const videoChannels: VideoChannel[] = []
35 for (const videoChannelJSON of result.data) {
36 videoChannels.push(new VideoChannel(videoChannelJSON))
39 return { data: videoChannels, total: result.total }
42 getVideoChannel (videoChannelName: string) {
43 return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
45 map(videoChannelHash => new VideoChannel(videoChannelHash)),
46 tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
47 catchError(err => this.restExtractor.handleError(err))
51 listAccountVideoChannels (options: {
53 componentPagination?: ComponentPaginationLight
57 }): Observable<ResultList<VideoChannel>> {
58 const { account, componentPagination, withStats = false, sort, search } = options
60 const defaultCount = this.serverService.getHTMLConfig().videoChannels.maxPerUser
62 const pagination = componentPagination
63 ? this.restService.componentToRestPagination(componentPagination)
64 : { start: 0, count: defaultCount }
66 let params = new HttpParams()
67 params = this.restService.addRestGetParams(params, pagination, sort)
68 params = params.set('withStats', withStats + '')
70 if (search) params = params.set('search', search)
72 const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
73 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
75 map(res => VideoChannelService.extractVideoChannels(res)),
76 catchError(err => this.restExtractor.handleError(err))
80 createVideoChannel (videoChannel: VideoChannelCreate) {
81 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
82 .pipe(catchError(err => this.restExtractor.handleError(err)))
85 updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
86 return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
87 .pipe(catchError(err => this.restExtractor.handleError(err)))
90 changeVideoChannelImage (videoChannelName: string, avatarForm: FormData, type: 'avatar' | 'banner') {
91 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type + '/pick'
93 return this.authHttp.post<{ avatars?: ActorImage[], banners?: ActorImage[] }>(url, avatarForm)
94 .pipe(catchError(err => this.restExtractor.handleError(err)))
97 deleteVideoChannelImage (videoChannelName: string, type: 'avatar' | 'banner') {
98 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type
100 return this.authHttp.delete(url)
101 .pipe(catchError(err => this.restExtractor.handleError(err)))
104 removeVideoChannel (videoChannel: VideoChannel) {
105 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
106 .pipe(catchError(err => this.restExtractor.handleError(err)))
109 importVideos (videoChannelName: string, externalChannelUrl: string, syncId?: number) {
110 const path = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/import-videos'
112 const body: VideosImportInChannelCreate = {
114 videoChannelSyncId: syncId
117 return this.authHttp.post(path, body)
118 .pipe(catchError(err => this.restExtractor.handleError(err)))