]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { Observable, ReplaySubject } from 'rxjs' |
67ed6552 | 2 | import { catchError, map, tap } from 'rxjs/operators' |
c8487f3f | 3 | import { HttpClient, HttpParams } from '@angular/common/http' |
67ed6552 C |
4 | import { Injectable } from '@angular/core' |
5 | import { ComponentPaginationLight, RestExtractor, RestService } from '@app/core' | |
f4796856 | 6 | import { ActorImage, ResultList, VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '@shared/models' |
67ed6552 C |
7 | import { environment } from '../../../../environments/environment' |
8 | import { Account } from '../account' | |
d3e91a5f | 9 | import { AccountService } from '../account/account.service' |
d3e91a5f C |
10 | import { VideoChannel } from './video-channel.model' |
11 | ||
12 | @Injectable() | |
13 | export class VideoChannelService { | |
170726f5 C |
14 | static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/' |
15 | ||
16 | videoChannelLoaded = new ReplaySubject<VideoChannel>(1) | |
17 | ||
22a16e36 C |
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 | ||
f6eebcb3 C |
28 | constructor ( |
29 | private authHttp: HttpClient, | |
c8487f3f | 30 | private restService: RestService, |
f6eebcb3 C |
31 | private restExtractor: RestExtractor |
32 | ) { } | |
33 | ||
8a19bee1 C |
34 | getVideoChannel (videoChannelName: string) { |
35 | return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName) | |
db400f44 C |
36 | .pipe( |
37 | map(videoChannelHash => new VideoChannel(videoChannelHash)), | |
38 | tap(videoChannel => this.videoChannelLoaded.next(videoChannel)), | |
e4f0e92e | 39 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 40 | ) |
170726f5 C |
41 | } |
42 | ||
747c5628 RK |
43 | listAccountVideoChannels ( |
44 | account: Account, | |
45 | componentPagination?: ComponentPaginationLight, | |
bc99dfe5 RK |
46 | withStats = false, |
47 | search?: string | |
747c5628 | 48 | ): Observable<ResultList<VideoChannel>> { |
c8487f3f C |
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) | |
747c5628 | 55 | params = params.set('withStats', withStats + '') |
c8487f3f | 56 | |
bc99dfe5 RK |
57 | if (search) { |
58 | params = params.set('search', search) | |
59 | } | |
60 | ||
c8487f3f C |
61 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels' |
62 | return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params }) | |
db400f44 | 63 | .pipe( |
22a16e36 | 64 | map(res => VideoChannelService.extractVideoChannels(res)), |
e4f0e92e | 65 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 66 | ) |
d3e91a5f C |
67 | } |
68 | ||
08c1efbe C |
69 | createVideoChannel (videoChannel: VideoChannelCreate) { |
70 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
db400f44 C |
71 | .pipe( |
72 | map(this.restExtractor.extractDataBool), | |
73 | catchError(err => this.restExtractor.handleError(err)) | |
74 | ) | |
08c1efbe C |
75 | } |
76 | ||
22a16e36 C |
77 | updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) { |
78 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel) | |
db400f44 C |
79 | .pipe( |
80 | map(this.restExtractor.extractDataBool), | |
81 | catchError(err => this.restExtractor.handleError(err)) | |
82 | ) | |
08c1efbe C |
83 | } |
84 | ||
cdeddff1 C |
85 | changeVideoChannelImage (videoChannelName: string, avatarForm: FormData, type: 'avatar' | 'banner') { |
86 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type + '/pick' | |
52d9f792 | 87 | |
cdeddff1 | 88 | return this.authHttp.post<{ avatar?: ActorImage, banner?: ActorImage }>(url, avatarForm) |
e4f0e92e | 89 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
52d9f792 C |
90 | } |
91 | ||
cdeddff1 C |
92 | deleteVideoChannelImage (videoChannelName: string, type: 'avatar' | 'banner') { |
93 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type | |
1ea7da81 RK |
94 | |
95 | return this.authHttp.delete(url) | |
96 | .pipe( | |
97 | map(this.restExtractor.extractDataBool), | |
98 | catchError(err => this.restExtractor.handleError(err)) | |
99 | ) | |
100 | } | |
101 | ||
08c1efbe | 102 | removeVideoChannel (videoChannel: VideoChannel) { |
4035d2b6 | 103 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost) |
db400f44 C |
104 | .pipe( |
105 | map(this.restExtractor.extractDataBool), | |
106 | catchError(err => this.restExtractor.handleError(err)) | |
107 | ) | |
08c1efbe | 108 | } |
d3e91a5f | 109 | } |