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