]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video-channel/video-channel.service.ts
3f9ef74fa8f0d5f379c65b98809ac3b1fed46374
[github/Chocobozzz/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 { ActorImage, 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 search?: string
48 ): Observable<ResultList<VideoChannel>> {
49 const pagination = componentPagination
50 ? this.restService.componentPaginationToRestPagination(componentPagination)
51 : { start: 0, count: 20 }
52
53 let params = new HttpParams()
54 params = this.restService.addRestGetParams(params, pagination)
55 params = params.set('withStats', withStats + '')
56
57 if (search) {
58 params = params.set('search', search)
59 }
60
61 const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
62 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
63 .pipe(
64 map(res => VideoChannelService.extractVideoChannels(res)),
65 catchError(err => this.restExtractor.handleError(err))
66 )
67 }
68
69 createVideoChannel (videoChannel: VideoChannelCreate) {
70 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
71 .pipe(
72 map(this.restExtractor.extractDataBool),
73 catchError(err => this.restExtractor.handleError(err))
74 )
75 }
76
77 updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
78 return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
79 .pipe(
80 map(this.restExtractor.extractDataBool),
81 catchError(err => this.restExtractor.handleError(err))
82 )
83 }
84
85 changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) {
86 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick'
87
88 return this.authHttp.post<{ avatar: ActorImage }>(url, avatarForm)
89 .pipe(catchError(err => this.restExtractor.handleError(err)))
90 }
91
92 deleteVideoChannelAvatar (videoChannelName: string) {
93 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar'
94
95 return this.authHttp.delete(url)
96 .pipe(
97 map(this.restExtractor.extractDataBool),
98 catchError(err => this.restExtractor.handleError(err))
99 )
100 }
101
102 removeVideoChannel (videoChannel: VideoChannel) {
103 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
104 .pipe(
105 map(this.restExtractor.extractDataBool),
106 catchError(err => this.restExtractor.handleError(err))
107 )
108 }
109 }