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 { ComponentPagination } from '@app/shared/rest/component-pagination.model'
14 import { RestService } from '@app/shared/rest'
17 export class VideoChannelService {
18 static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/'
20 videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
22 static extractVideoChannels (result: ResultList<VideoChannelServer>) {
23 const videoChannels: VideoChannel[] = []
25 for (const videoChannelJSON of result.data) {
26 videoChannels.push(new VideoChannel(videoChannelJSON))
29 return { data: videoChannels, total: result.total }
33 private authHttp: HttpClient,
34 private restService: RestService,
35 private restExtractor: RestExtractor
38 getVideoChannel (videoChannelName: string) {
39 return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
41 map(videoChannelHash => new VideoChannel(videoChannelHash)),
42 tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
43 catchError(err => this.restExtractor.handleError(err))
47 listAccountVideoChannels (account: Account, componentPagination?: ComponentPagination): Observable<ResultList<VideoChannel>> {
48 const pagination = componentPagination
49 ? this.restService.componentPaginationToRestPagination(componentPagination)
50 : { start: 0, count: 20 }
52 let params = new HttpParams()
53 params = this.restService.addRestGetParams(params, pagination)
55 const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
56 return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
58 map(res => VideoChannelService.extractVideoChannels(res)),
59 catchError(err => this.restExtractor.handleError(err))
63 createVideoChannel (videoChannel: VideoChannelCreate) {
64 return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel)
66 map(this.restExtractor.extractDataBool),
67 catchError(err => this.restExtractor.handleError(err))
71 updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
72 return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
74 map(this.restExtractor.extractDataBool),
75 catchError(err => this.restExtractor.handleError(err))
79 changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) {
80 const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick'
82 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
83 .pipe(catchError(err => this.restExtractor.handleError(err)))
86 removeVideoChannel (videoChannel: VideoChannel) {
87 return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
89 map(this.restExtractor.extractDataBool),
90 catchError(err => this.restExtractor.handleError(err))