]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-channel/video-channel.service.ts
Add ability for uploaders to schedule video update
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-channel / video-channel.service.ts
1 import { catchError, map, tap } from 'rxjs/operators'
2 import { Injectable } from '@angular/core'
3 import { Observable, ReplaySubject } from 'rxjs'
4 import { RestExtractor } from '../rest/rest-extractor.service'
5 import { HttpClient } from '@angular/common/http'
6 import { VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '../../../../../shared/models/videos'
7 import { AccountService } from '../account/account.service'
8 import { ResultList } from '../../../../../shared'
9 import { VideoChannel } from './video-channel.model'
10 import { environment } from '../../../environments/environment'
11 import { Account } from '@app/shared/account/account.model'
12
13 @Injectable()
14 export class VideoChannelService {
15 static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
16
17 videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
18
19 constructor (
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor
22 ) {}
23
24 getVideoChannel (videoChannelUUID: string) {
25 return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID)
26 .pipe(
27 map(videoChannelHash => new VideoChannel(videoChannelHash)),
28 tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
29 catchError(res => this.restExtractor.handleError(res))
30 )
31 }
32
33 listAccountVideoChannels (account: Account): Observable<ResultList<VideoChannel>> {
34 return this.authHttp.get<ResultList<VideoChannelServer>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels')
35 .pipe(
36 map(res => this.extractVideoChannels(res)),
37 catchError((res) => this.restExtractor.handleError(res))
38 )
39 }
40
41 createVideoChannel (videoChannel: VideoChannelCreate) {
42 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
43 .pipe(
44 map(this.restExtractor.extractDataBool),
45 catchError(err => this.restExtractor.handleError(err))
46 )
47 }
48
49 updateVideoChannel (videoChannelUUID: string, videoChannel: VideoChannelUpdate) {
50 return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID, videoChannel)
51 .pipe(
52 map(this.restExtractor.extractDataBool),
53 catchError(err => this.restExtractor.handleError(err))
54 )
55 }
56
57 removeVideoChannel (videoChannel: VideoChannel) {
58 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid)
59 .pipe(
60 map(this.restExtractor.extractDataBool),
61 catchError(err => this.restExtractor.handleError(err))
62 )
63 }
64
65 private extractVideoChannels (result: ResultList<VideoChannelServer>) {
66 const videoChannels: VideoChannel[] = []
67
68 for (const videoChannelJSON of result.data) {
69 videoChannels.push(new VideoChannel(videoChannelJSON))
70 }
71
72 return { data: videoChannels, total: result.total }
73 }
74 }