]>
Commit | Line | Data |
---|---|---|
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 } 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 | ||
14 | @Injectable() | |
15 | export class VideoChannelService { | |
16 | static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/' | |
17 | ||
18 | videoChannelLoaded = new ReplaySubject<VideoChannel>(1) | |
19 | ||
20 | static extractVideoChannels (result: ResultList<VideoChannelServer>) { | |
21 | const videoChannels: VideoChannel[] = [] | |
22 | ||
23 | for (const videoChannelJSON of result.data) { | |
24 | videoChannels.push(new VideoChannel(videoChannelJSON)) | |
25 | } | |
26 | ||
27 | return { data: videoChannels, total: result.total } | |
28 | } | |
29 | ||
30 | constructor ( | |
31 | private authHttp: HttpClient, | |
32 | private restExtractor: RestExtractor | |
33 | ) { } | |
34 | ||
35 | getVideoChannel (videoChannelName: string) { | |
36 | return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName) | |
37 | .pipe( | |
38 | map(videoChannelHash => new VideoChannel(videoChannelHash)), | |
39 | tap(videoChannel => this.videoChannelLoaded.next(videoChannel)), | |
40 | catchError(err => this.restExtractor.handleError(err)) | |
41 | ) | |
42 | } | |
43 | ||
44 | listAccountVideoChannels (account: Account): Observable<ResultList<VideoChannel>> { | |
45 | return this.authHttp.get<ResultList<VideoChannelServer>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels') | |
46 | .pipe( | |
47 | map(res => VideoChannelService.extractVideoChannels(res)), | |
48 | catchError(err => this.restExtractor.handleError(err)) | |
49 | ) | |
50 | } | |
51 | ||
52 | createVideoChannel (videoChannel: VideoChannelCreate) { | |
53 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
54 | .pipe( | |
55 | map(this.restExtractor.extractDataBool), | |
56 | catchError(err => this.restExtractor.handleError(err)) | |
57 | ) | |
58 | } | |
59 | ||
60 | updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) { | |
61 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel) | |
62 | .pipe( | |
63 | map(this.restExtractor.extractDataBool), | |
64 | catchError(err => this.restExtractor.handleError(err)) | |
65 | ) | |
66 | } | |
67 | ||
68 | changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) { | |
69 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick' | |
70 | ||
71 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) | |
72 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
73 | } | |
74 | ||
75 | removeVideoChannel (videoChannel: VideoChannel) { | |
76 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost) | |
77 | .pipe( | |
78 | map(this.restExtractor.extractDataBool), | |
79 | catchError(err => this.restExtractor.handleError(err)) | |
80 | ) | |
81 | } | |
82 | } |