]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video-channel/video-channel.service.ts
Add ability to list imports of a channel sync
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video-channel / video-channel.service.ts
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'
6 import {
7 ActorImage,
8 ResultList,
9 VideoChannel as VideoChannelServer,
10 VideoChannelCreate,
11 VideoChannelUpdate,
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'
18
19 @Injectable()
20 export class VideoChannelService {
21 static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
22
23 videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
24
25 constructor (
26 private authHttp: HttpClient,
27 private restService: RestService,
28 private restExtractor: RestExtractor
29 ) { }
30
31 static extractVideoChannels (result: ResultList<VideoChannelServer>) {
32 const videoChannels: VideoChannel[] = []
33
34 for (const videoChannelJSON of result.data) {
35 videoChannels.push(new VideoChannel(videoChannelJSON))
36 }
37
38 return { data: videoChannels, total: result.total }
39 }
40
41 getVideoChannel (videoChannelName: string) {
42 return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
43 .pipe(
44 map(videoChannelHash => new VideoChannel(videoChannelHash)),
45 tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
46 catchError(err => this.restExtractor.handleError(err))
47 )
48 }
49
50 listAccountVideoChannels (options: {
51 account: Account
52 componentPagination?: ComponentPaginationLight
53 withStats?: boolean
54 sort?: string
55 search?: string
56 }): Observable<ResultList<VideoChannel>> {
57 const { account, componentPagination, withStats = false, sort, search } = options
58
59 const pagination = componentPagination
60 ? this.restService.componentToRestPagination(componentPagination)
61 : { start: 0, count: 20 }
62
63 let params = new HttpParams()
64 params = this.restService.addRestGetParams(params, pagination, sort)
65 params = params.set('withStats', withStats + '')
66
67 if (search) params = params.set('search', search)
68
69 const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
70 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
71 .pipe(
72 map(res => VideoChannelService.extractVideoChannels(res)),
73 catchError(err => this.restExtractor.handleError(err))
74 )
75 }
76
77 createVideoChannel (videoChannel: VideoChannelCreate) {
78 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
79 .pipe(catchError(err => this.restExtractor.handleError(err)))
80 }
81
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)))
85 }
86
87 changeVideoChannelImage (videoChannelName: string, avatarForm: FormData, type: 'avatar' | 'banner') {
88 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type + '/pick'
89
90 return this.authHttp.post<{ avatars?: ActorImage[], banners?: ActorImage[] }>(url, avatarForm)
91 .pipe(catchError(err => this.restExtractor.handleError(err)))
92 }
93
94 deleteVideoChannelImage (videoChannelName: string, type: 'avatar' | 'banner') {
95 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type
96
97 return this.authHttp.delete(url)
98 .pipe(catchError(err => this.restExtractor.handleError(err)))
99 }
100
101 removeVideoChannel (videoChannel: VideoChannel) {
102 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
103 .pipe(catchError(err => this.restExtractor.handleError(err)))
104 }
105
106 importVideos (videoChannelName: string, externalChannelUrl: string, syncId?: number) {
107 const path = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/import-videos'
108
109 const body: VideosImportInChannelCreate = {
110 externalChannelUrl,
111 videoChannelSyncId: syncId
112 }
113
114 return this.authHttp.post(path, body)
115 .pipe(catchError(err => this.restExtractor.handleError(err)))
116 }
117 }