0e036bda766b4da49066339d9c43a5ff49039055
[oweals/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, HttpParams } 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 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
13 import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model'
14 import { RestService } from '@app/shared/rest'
15
16 @Injectable()
17 export class VideoChannelService {
18   static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
19
20   videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
21
22   static extractVideoChannels (result: ResultList<VideoChannelServer>) {
23     const videoChannels: VideoChannel[] = []
24
25     for (const videoChannelJSON of result.data) {
26       videoChannels.push(new VideoChannel(videoChannelJSON))
27     }
28
29     return { data: videoChannels, total: result.total }
30   }
31
32   constructor (
33     private authHttp: HttpClient,
34     private restService: RestService,
35     private restExtractor: RestExtractor
36   ) { }
37
38   getVideoChannel (videoChannelName: string) {
39     return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
40                .pipe(
41                  map(videoChannelHash => new VideoChannel(videoChannelHash)),
42                  tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
43                  catchError(err => this.restExtractor.handleError(err))
44                )
45   }
46
47   listAccountVideoChannels (
48     account: Account,
49     componentPagination?: ComponentPaginationLight,
50     withStats = false
51   ): Observable<ResultList<VideoChannel>> {
52     const pagination = componentPagination
53       ? this.restService.componentPaginationToRestPagination(componentPagination)
54       : { start: 0, count: 20 }
55
56     let params = new HttpParams()
57     params = this.restService.addRestGetParams(params, pagination)
58     params = params.set('withStats', withStats + '')
59
60     const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
61     return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
62                .pipe(
63                  map(res => VideoChannelService.extractVideoChannels(res)),
64                  catchError(err => this.restExtractor.handleError(err))
65                )
66   }
67
68   createVideoChannel (videoChannel: VideoChannelCreate) {
69     return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
70                .pipe(
71                  map(this.restExtractor.extractDataBool),
72                  catchError(err => this.restExtractor.handleError(err))
73                )
74   }
75
76   updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
77     return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
78                .pipe(
79                  map(this.restExtractor.extractDataBool),
80                  catchError(err => this.restExtractor.handleError(err))
81                )
82   }
83
84   changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) {
85     const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick'
86
87     return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
88                .pipe(catchError(err => this.restExtractor.handleError(err)))
89   }
90
91   removeVideoChannel (videoChannel: VideoChannel) {
92     return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
93                .pipe(
94                  map(this.restExtractor.extractDataBool),
95                  catchError(err => this.restExtractor.handleError(err))
96                )
97   }
98 }