]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/video-channel/video-channel.service.ts
Display user quota progress bars above upload form (#2981)
[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'
6import { Avatar, ResultList, VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '@shared/models'
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
22a16e36
C
18 static extractVideoChannels (result: ResultList<VideoChannelServer>) {
19 const videoChannels: VideoChannel[] = []
20
21 for (const videoChannelJSON of result.data) {
22 videoChannels.push(new VideoChannel(videoChannelJSON))
23 }
24
25 return { data: videoChannels, total: result.total }
26 }
27
f6eebcb3
C
28 constructor (
29 private authHttp: HttpClient,
c8487f3f 30 private restService: RestService,
f6eebcb3
C
31 private restExtractor: RestExtractor
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
747c5628
RK
43 listAccountVideoChannels (
44 account: Account,
45 componentPagination?: ComponentPaginationLight,
46 withStats = false
47 ): Observable<ResultList<VideoChannel>> {
c8487f3f
C
48 const pagination = componentPagination
49 ? this.restService.componentPaginationToRestPagination(componentPagination)
50 : { start: 0, count: 20 }
51
52 let params = new HttpParams()
53 params = this.restService.addRestGetParams(params, pagination)
747c5628 54 params = params.set('withStats', withStats + '')
c8487f3f
C
55
56 const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
57 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
db400f44 58 .pipe(
22a16e36 59 map(res => VideoChannelService.extractVideoChannels(res)),
e4f0e92e 60 catchError(err => this.restExtractor.handleError(err))
db400f44 61 )
d3e91a5f
C
62 }
63
08c1efbe
C
64 createVideoChannel (videoChannel: VideoChannelCreate) {
65 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
db400f44
C
66 .pipe(
67 map(this.restExtractor.extractDataBool),
68 catchError(err => this.restExtractor.handleError(err))
69 )
08c1efbe
C
70 }
71
22a16e36
C
72 updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
73 return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
db400f44
C
74 .pipe(
75 map(this.restExtractor.extractDataBool),
76 catchError(err => this.restExtractor.handleError(err))
77 )
08c1efbe
C
78 }
79
22a16e36
C
80 changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) {
81 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick'
52d9f792
C
82
83 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
e4f0e92e 84 .pipe(catchError(err => this.restExtractor.handleError(err)))
52d9f792
C
85 }
86
08c1efbe 87 removeVideoChannel (videoChannel: VideoChannel) {
4035d2b6 88 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
db400f44
C
89 .pipe(
90 map(this.restExtractor.extractDataBool),
91 catchError(err => this.restExtractor.handleError(err))
92 )
08c1efbe 93 }
d3e91a5f 94}