]>
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 | 4 | import { Injectable } from '@angular/core' |
213bb3bb | 5 | import { ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core' |
a3b472a1 C |
6 | import { |
7 | ActorImage, | |
8 | ResultList, | |
9 | VideoChannel as VideoChannelServer, | |
10 | VideoChannelCreate, | |
11 | VideoChannelUpdate, | |
12 | VideosImportInChannelCreate | |
13 | } from '@shared/models' | |
67ed6552 C |
14 | import { environment } from '../../../../environments/environment' |
15 | import { Account } from '../account' | |
d3e91a5f | 16 | import { AccountService } from '../account/account.service' |
d3e91a5f C |
17 | import { VideoChannel } from './video-channel.model' |
18 | ||
19 | @Injectable() | |
20 | export class VideoChannelService { | |
170726f5 C |
21 | static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/' |
22 | ||
23 | videoChannelLoaded = new ReplaySubject<VideoChannel>(1) | |
24 | ||
9df52d66 C |
25 | constructor ( |
26 | private authHttp: HttpClient, | |
27 | private restService: RestService, | |
213bb3bb C |
28 | private restExtractor: RestExtractor, |
29 | private serverService: ServerService | |
9df52d66 C |
30 | ) { } |
31 | ||
22a16e36 C |
32 | static extractVideoChannels (result: ResultList<VideoChannelServer>) { |
33 | const videoChannels: VideoChannel[] = [] | |
34 | ||
35 | for (const videoChannelJSON of result.data) { | |
36 | videoChannels.push(new VideoChannel(videoChannelJSON)) | |
37 | } | |
38 | ||
39 | return { data: videoChannels, total: result.total } | |
40 | } | |
41 | ||
8a19bee1 C |
42 | getVideoChannel (videoChannelName: string) { |
43 | return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName) | |
db400f44 C |
44 | .pipe( |
45 | map(videoChannelHash => new VideoChannel(videoChannelHash)), | |
46 | tap(videoChannel => this.videoChannelLoaded.next(videoChannel)), | |
e4f0e92e | 47 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 48 | ) |
170726f5 C |
49 | } |
50 | ||
dc2b2938 C |
51 | listAccountVideoChannels (options: { |
52 | account: Account | |
53 | componentPagination?: ComponentPaginationLight | |
54 | withStats?: boolean | |
55 | sort?: string | |
bc99dfe5 | 56 | search?: string |
dc2b2938 C |
57 | }): Observable<ResultList<VideoChannel>> { |
58 | const { account, componentPagination, withStats = false, sort, search } = options | |
59 | ||
213bb3bb C |
60 | const defaultCount = this.serverService.getHTMLConfig().videoChannels.maxPerUser |
61 | ||
c8487f3f | 62 | const pagination = componentPagination |
4beda9e1 | 63 | ? this.restService.componentToRestPagination(componentPagination) |
213bb3bb | 64 | : { start: 0, count: defaultCount } |
c8487f3f C |
65 | |
66 | let params = new HttpParams() | |
dc2b2938 | 67 | params = this.restService.addRestGetParams(params, pagination, sort) |
747c5628 | 68 | params = params.set('withStats', withStats + '') |
c8487f3f | 69 | |
dc2b2938 | 70 | if (search) params = params.set('search', search) |
bc99dfe5 | 71 | |
c8487f3f C |
72 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels' |
73 | return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params }) | |
db400f44 | 74 | .pipe( |
22a16e36 | 75 | map(res => VideoChannelService.extractVideoChannels(res)), |
e4f0e92e | 76 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 77 | ) |
d3e91a5f C |
78 | } |
79 | ||
08c1efbe C |
80 | createVideoChannel (videoChannel: VideoChannelCreate) { |
81 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
e8bffe96 | 82 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
08c1efbe C |
83 | } |
84 | ||
22a16e36 C |
85 | updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) { |
86 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel) | |
e8bffe96 | 87 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
08c1efbe C |
88 | } |
89 | ||
cdeddff1 C |
90 | changeVideoChannelImage (videoChannelName: string, avatarForm: FormData, type: 'avatar' | 'banner') { |
91 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type + '/pick' | |
52d9f792 | 92 | |
d0800f76 | 93 | return this.authHttp.post<{ avatars?: ActorImage[], banners?: ActorImage[] }>(url, avatarForm) |
e4f0e92e | 94 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
52d9f792 C |
95 | } |
96 | ||
cdeddff1 C |
97 | deleteVideoChannelImage (videoChannelName: string, type: 'avatar' | 'banner') { |
98 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/' + type | |
1ea7da81 RK |
99 | |
100 | return this.authHttp.delete(url) | |
e8bffe96 | 101 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
1ea7da81 RK |
102 | } |
103 | ||
08c1efbe | 104 | removeVideoChannel (videoChannel: VideoChannel) { |
4035d2b6 | 105 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost) |
e8bffe96 | 106 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
08c1efbe | 107 | } |
2a491182 | 108 | |
a3b472a1 | 109 | importVideos (videoChannelName: string, externalChannelUrl: string, syncId?: number) { |
2a491182 | 110 | const path = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/import-videos' |
a3b472a1 C |
111 | |
112 | const body: VideosImportInChannelCreate = { | |
113 | externalChannelUrl, | |
114 | videoChannelSyncId: syncId | |
115 | } | |
116 | ||
117 | return this.authHttp.post(path, body) | |
2a491182 F |
118 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
119 | } | |
d3e91a5f | 120 | } |