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