]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { catchError, map, tap } from 'rxjs/operators' |
d3e91a5f | 2 | import { Injectable } from '@angular/core' |
db400f44 | 3 | import { Observable, ReplaySubject } from 'rxjs' |
d3e91a5f | 4 | import { RestExtractor } from '../rest/rest-extractor.service' |
d3e91a5f | 5 | import { HttpClient } from '@angular/common/http' |
08c1efbe | 6 | import { VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '../../../../../shared/models/videos' |
d3e91a5f C |
7 | import { AccountService } from '../account/account.service' |
8 | import { ResultList } from '../../../../../shared' | |
9 | import { VideoChannel } from './video-channel.model' | |
170726f5 | 10 | import { environment } from '../../../environments/environment' |
ad9e39fb | 11 | import { Account } from '@app/shared/account/account.model' |
52d9f792 | 12 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' |
d3e91a5f C |
13 | |
14 | @Injectable() | |
15 | export class VideoChannelService { | |
170726f5 C |
16 | static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/' |
17 | ||
18 | videoChannelLoaded = new ReplaySubject<VideoChannel>(1) | |
19 | ||
d3e91a5f C |
20 | constructor ( |
21 | private authHttp: HttpClient, | |
db400f44 | 22 | private restExtractor: RestExtractor |
d3e91a5f C |
23 | ) {} |
24 | ||
170726f5 C |
25 | getVideoChannel (videoChannelUUID: string) { |
26 | return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID) | |
db400f44 C |
27 | .pipe( |
28 | map(videoChannelHash => new VideoChannel(videoChannelHash)), | |
29 | tap(videoChannel => this.videoChannelLoaded.next(videoChannel)), | |
e4f0e92e | 30 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 31 | ) |
170726f5 C |
32 | } |
33 | ||
ad9e39fb C |
34 | listAccountVideoChannels (account: Account): Observable<ResultList<VideoChannel>> { |
35 | return this.authHttp.get<ResultList<VideoChannelServer>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels') | |
db400f44 C |
36 | .pipe( |
37 | map(res => this.extractVideoChannels(res)), | |
e4f0e92e | 38 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 39 | ) |
d3e91a5f C |
40 | } |
41 | ||
08c1efbe C |
42 | createVideoChannel (videoChannel: VideoChannelCreate) { |
43 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
db400f44 C |
44 | .pipe( |
45 | map(this.restExtractor.extractDataBool), | |
46 | catchError(err => this.restExtractor.handleError(err)) | |
47 | ) | |
08c1efbe C |
48 | } |
49 | ||
50 | updateVideoChannel (videoChannelUUID: string, videoChannel: VideoChannelUpdate) { | |
51 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID, videoChannel) | |
db400f44 C |
52 | .pipe( |
53 | map(this.restExtractor.extractDataBool), | |
54 | catchError(err => this.restExtractor.handleError(err)) | |
55 | ) | |
08c1efbe C |
56 | } |
57 | ||
52d9f792 C |
58 | changeVideoChannelAvatar (videoChannelUUID: string, avatarForm: FormData) { |
59 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID + '/avatar/pick' | |
60 | ||
61 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) | |
e4f0e92e | 62 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
52d9f792 C |
63 | } |
64 | ||
08c1efbe C |
65 | removeVideoChannel (videoChannel: VideoChannel) { |
66 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid) | |
db400f44 C |
67 | .pipe( |
68 | map(this.restExtractor.extractDataBool), | |
69 | catchError(err => this.restExtractor.handleError(err)) | |
70 | ) | |
08c1efbe C |
71 | } |
72 | ||
d3e91a5f C |
73 | private extractVideoChannels (result: ResultList<VideoChannelServer>) { |
74 | const videoChannels: VideoChannel[] = [] | |
75 | ||
76 | for (const videoChannelJSON of result.data) { | |
77 | videoChannels.push(new VideoChannel(videoChannelJSON)) | |
78 | } | |
79 | ||
80 | return { data: videoChannels, total: result.total } | |
81 | } | |
82 | } |