allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / client / src / app / shared / shared-main / video-channel / video-channel.service.ts
1 import { Observable, ReplaySubject } from 'rxjs'
2 import { catchError, map, tap } from 'rxjs/operators'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { ComponentPaginationLight, RestExtractor, RestService } from '@app/core'
6 import { Avatar, ResultList, VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '@shared/models'
7 import { environment } from '../../../../environments/environment'
8 import { Account } from '../account'
9 import { AccountService } from '../account/account.service'
10 import { VideoChannel } from './video-channel.model'
11
12 @Injectable()
13 export class VideoChannelService {
14   static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
15
16   videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
17
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
28   constructor (
29     private authHttp: HttpClient,
30     private restService: RestService,
31     private restExtractor: RestExtractor
32   ) { }
33
34   getVideoChannel (videoChannelName: string) {
35     return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
36                .pipe(
37                  map(videoChannelHash => new VideoChannel(videoChannelHash)),
38                  tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
39                  catchError(err => this.restExtractor.handleError(err))
40                )
41   }
42
43   listAccountVideoChannels (
44     account: Account,
45     componentPagination?: ComponentPaginationLight,
46     withStats = false
47   ): Observable<ResultList<VideoChannel>> {
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)
54     params = params.set('withStats', withStats + '')
55
56     const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
57     return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
58                .pipe(
59                  map(res => VideoChannelService.extractVideoChannels(res)),
60                  catchError(err => this.restExtractor.handleError(err))
61                )
62   }
63
64   createVideoChannel (videoChannel: VideoChannelCreate) {
65     return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
66                .pipe(
67                  map(this.restExtractor.extractDataBool),
68                  catchError(err => this.restExtractor.handleError(err))
69                )
70   }
71
72   updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
73     return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
74                .pipe(
75                  map(this.restExtractor.extractDataBool),
76                  catchError(err => this.restExtractor.handleError(err))
77                )
78   }
79
80   changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) {
81     const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick'
82
83     return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
84                .pipe(catchError(err => this.restExtractor.handleError(err)))
85   }
86
87   removeVideoChannel (videoChannel: VideoChannel) {
88     return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
89                .pipe(
90                  map(this.restExtractor.extractDataBool),
91                  catchError(err => this.restExtractor.handleError(err))
92                )
93   }
94 }