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