]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-channel/video-channel.service.ts
Implement video channel views
[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 } 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
15 @Injectable()
16 export class VideoChannelService {
17 static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
18
19 videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
20
21 constructor (
22 private authHttp: HttpClient,
23 private restExtractor: RestExtractor,
24 private restService: RestService
25 ) {}
26
27 getVideoChannel (videoChannelUUID: string) {
28 return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID)
29 .map(videoChannelHash => new VideoChannel(videoChannelHash))
30 .do(videoChannel => this.videoChannelLoaded.next(videoChannel))
31 .catch((res) => this.restExtractor.handleError(res))
32 }
33
34 listAccountVideoChannels (accountId: number): Observable<ResultList<VideoChannel>> {
35 return this.authHttp.get<ResultList<VideoChannelServer>>(AccountService.BASE_ACCOUNT_URL + accountId + '/video-channels')
36 .map(res => this.extractVideoChannels(res))
37 .catch((res) => this.restExtractor.handleError(res))
38 }
39
40 private extractVideoChannels (result: ResultList<VideoChannelServer>) {
41 const videoChannels: VideoChannel[] = []
42
43 for (const videoChannelJSON of result.data) {
44 videoChannels.push(new VideoChannel(videoChannelJSON))
45 }
46
47 return { data: videoChannels, total: result.total }
48 }
49 }