]>
Commit | Line | Data |
---|---|---|
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 | constructor ( | |
19 | private authHttp: HttpClient, | |
20 | private restService: RestService, | |
21 | private restExtractor: RestExtractor | |
22 | ) { } | |
23 | ||
24 | static extractVideoChannels (result: ResultList<VideoChannelServer>) { | |
25 | const videoChannels: VideoChannel[] = [] | |
26 | ||
27 | for (const videoChannelJSON of result.data) { | |
28 | videoChannels.push(new VideoChannel(videoChannelJSON)) | |
29 | } | |
30 | ||
31 | return { data: videoChannels, total: result.total } | |
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 (options: { | |
44 | account: Account | |
45 | componentPagination?: ComponentPaginationLight | |
46 | withStats?: boolean | |
47 | sort?: string | |
48 | search?: string | |
49 | }): Observable<ResultList<VideoChannel>> { | |
50 | const { account, componentPagination, withStats = false, sort, search } = options | |
51 | ||
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, sort) | |
58 | params = params.set('withStats', withStats + '') | |
59 | ||
60 | if (search) params = params.set('search', search) | |
61 | ||
62 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels' | |
63 | return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params }) | |
64 | .pipe( | |
65 | map(res => VideoChannelService.extractVideoChannels(res)), | |
66 | catchError(err => this.restExtractor.handleError(err)) | |
67 | ) | |
68 | } | |
69 | ||
70 | createVideoChannel (videoChannel: VideoChannelCreate) { | |
71 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
72 | .pipe( | |
73 | map(this.restExtractor.extractDataBool), | |
74 | catchError(err => this.restExtractor.handleError(err)) | |
75 | ) | |
76 | } | |
77 | ||
78 | updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) { | |
79 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel) | |
80 | .pipe( | |
81 | map(this.restExtractor.extractDataBool), | |
82 | catchError(err => this.restExtractor.handleError(err)) | |
83 | ) | |
84 | } | |
85 | ||
86 | changeVideoChannelImage (videoChannelName: string, avatarForm: FormData, type: 'avatar' | 'banner') { | |
87 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type + '/pick' | |
88 | ||
89 | return this.authHttp.post<{ avatar?: ActorImage, banner?: ActorImage }>(url, avatarForm) | |
90 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
91 | } | |
92 | ||
93 | deleteVideoChannelImage (videoChannelName: string, type: 'avatar' | 'banner') { | |
94 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type | |
95 | ||
96 | return this.authHttp.delete(url) | |
97 | .pipe( | |
98 | map(this.restExtractor.extractDataBool), | |
99 | catchError(err => this.restExtractor.handleError(err)) | |
100 | ) | |
101 | } | |
102 | ||
103 | removeVideoChannel (videoChannel: VideoChannel) { | |
104 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost) | |
105 | .pipe( | |
106 | map(this.restExtractor.extractDataBool), | |
107 | catchError(err => this.restExtractor.handleError(err)) | |
108 | ) | |
109 | } | |
110 | } |