]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
db400f44 1import { Observable, ReplaySubject } from 'rxjs'
67ed6552 2import { catchError, map, tap } from 'rxjs/operators'
c8487f3f 3import { HttpClient, HttpParams } from '@angular/common/http'
67ed6552
C
4import { Injectable } from '@angular/core'
5import { ComponentPaginationLight, RestExtractor, RestService } from '@app/core'
a3b472a1
C
6import {
7 ActorImage,
8 ResultList,
9 VideoChannel as VideoChannelServer,
10 VideoChannelCreate,
11 VideoChannelUpdate,
12 VideosImportInChannelCreate
13} from '@shared/models'
67ed6552
C
14import { environment } from '../../../../environments/environment'
15import { Account } from '../account'
d3e91a5f 16import { AccountService } from '../account/account.service'
d3e91a5f
C
17import { VideoChannel } from './video-channel.model'
18
19@Injectable()
20export class VideoChannelService {
170726f5
C
21 static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
22
23 videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
24
9df52d66
C
25 constructor (
26 private authHttp: HttpClient,
27 private restService: RestService,
28 private restExtractor: RestExtractor
29 ) { }
30
22a16e36
C
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
8a19bee1
C
41 getVideoChannel (videoChannelName: string) {
42 return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
db400f44
C
43 .pipe(
44 map(videoChannelHash => new VideoChannel(videoChannelHash)),
45 tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
e4f0e92e 46 catchError(err => this.restExtractor.handleError(err))
db400f44 47 )
170726f5
C
48 }
49
dc2b2938
C
50 listAccountVideoChannels (options: {
51 account: Account
52 componentPagination?: ComponentPaginationLight
53 withStats?: boolean
54 sort?: string
bc99dfe5 55 search?: string
dc2b2938
C
56 }): Observable<ResultList<VideoChannel>> {
57 const { account, componentPagination, withStats = false, sort, search } = options
58
c8487f3f 59 const pagination = componentPagination
4beda9e1 60 ? this.restService.componentToRestPagination(componentPagination)
c8487f3f
C
61 : { start: 0, count: 20 }
62
63 let params = new HttpParams()
dc2b2938 64 params = this.restService.addRestGetParams(params, pagination, sort)
747c5628 65 params = params.set('withStats', withStats + '')
c8487f3f 66
dc2b2938 67 if (search) params = params.set('search', search)
bc99dfe5 68
c8487f3f
C
69 const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
70 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
db400f44 71 .pipe(
22a16e36 72 map(res => VideoChannelService.extractVideoChannels(res)),
e4f0e92e 73 catchError(err => this.restExtractor.handleError(err))
db400f44 74 )
d3e91a5f
C
75 }
76
08c1efbe
C
77 createVideoChannel (videoChannel: VideoChannelCreate) {
78 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
e8bffe96 79 .pipe(catchError(err => this.restExtractor.handleError(err)))
08c1efbe
C
80 }
81
22a16e36
C
82 updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
83 return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
e8bffe96 84 .pipe(catchError(err => this.restExtractor.handleError(err)))
08c1efbe
C
85 }
86
cdeddff1
C
87 changeVideoChannelImage (videoChannelName: string, avatarForm: FormData, type: 'avatar' | 'banner') {
88 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type + '/pick'
52d9f792 89
d0800f76 90 return this.authHttp.post<{ avatars?: ActorImage[], banners?: ActorImage[] }>(url, avatarForm)
e4f0e92e 91 .pipe(catchError(err => this.restExtractor.handleError(err)))
52d9f792
C
92 }
93
cdeddff1
C
94 deleteVideoChannelImage (videoChannelName: string, type: 'avatar' | 'banner') {
95 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type
1ea7da81
RK
96
97 return this.authHttp.delete(url)
e8bffe96 98 .pipe(catchError(err => this.restExtractor.handleError(err)))
1ea7da81
RK
99 }
100
08c1efbe 101 removeVideoChannel (videoChannel: VideoChannel) {
4035d2b6 102 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
e8bffe96 103 .pipe(catchError(err => this.restExtractor.handleError(err)))
08c1efbe 104 }
2a491182 105
a3b472a1 106 importVideos (videoChannelName: string, externalChannelUrl: string, syncId?: number) {
2a491182 107 const path = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/import-videos'
a3b472a1
C
108
109 const body: VideosImportInChannelCreate = {
110 externalChannelUrl,
111 videoChannelSyncId: syncId
112 }
113
114 return this.authHttp.post(path, body)
2a491182
F
115 .pipe(catchError(err => this.restExtractor.handleError(err)))
116 }
d3e91a5f 117}