diff options
Diffstat (limited to 'client/src/app/shared/shared-main/video-channel')
3 files changed, 138 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-main/video-channel/index.ts b/client/src/app/shared/shared-main/video-channel/index.ts new file mode 100644 index 000000000..1fcf6d3be --- /dev/null +++ b/client/src/app/shared/shared-main/video-channel/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './video-channel.model' | ||
2 | export * from './video-channel.service' | ||
diff --git a/client/src/app/shared/shared-main/video-channel/video-channel.model.ts b/client/src/app/shared/shared-main/video-channel/video-channel.model.ts new file mode 100644 index 000000000..123389afb --- /dev/null +++ b/client/src/app/shared/shared-main/video-channel/video-channel.model.ts | |||
@@ -0,0 +1,42 @@ | |||
1 | import { VideoChannel as ServerVideoChannel, ViewsPerDate, Account } from '@shared/models' | ||
2 | import { Actor } from '../account/actor.model' | ||
3 | |||
4 | export class VideoChannel extends Actor implements ServerVideoChannel { | ||
5 | displayName: string | ||
6 | description: string | ||
7 | support: string | ||
8 | isLocal: boolean | ||
9 | nameWithHost: string | ||
10 | nameWithHostForced: string | ||
11 | |||
12 | ownerAccount?: Account | ||
13 | ownerBy?: string | ||
14 | ownerAvatarUrl?: string | ||
15 | |||
16 | videosCount?: number | ||
17 | |||
18 | viewsPerDay?: ViewsPerDate[] | ||
19 | |||
20 | constructor (hash: ServerVideoChannel) { | ||
21 | super(hash) | ||
22 | |||
23 | this.displayName = hash.displayName | ||
24 | this.description = hash.description | ||
25 | this.support = hash.support | ||
26 | this.isLocal = hash.isLocal | ||
27 | this.nameWithHost = Actor.CREATE_BY_STRING(this.name, this.host) | ||
28 | this.nameWithHostForced = Actor.CREATE_BY_STRING(this.name, this.host, true) | ||
29 | |||
30 | this.videosCount = hash.videosCount | ||
31 | |||
32 | if (hash.viewsPerDay) { | ||
33 | this.viewsPerDay = hash.viewsPerDay.map(v => ({ ...v, date: new Date(v.date) })) | ||
34 | } | ||
35 | |||
36 | if (hash.ownerAccount) { | ||
37 | this.ownerAccount = hash.ownerAccount | ||
38 | this.ownerBy = Actor.CREATE_BY_STRING(hash.ownerAccount.name, hash.ownerAccount.host) | ||
39 | this.ownerAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.ownerAccount) | ||
40 | } | ||
41 | } | ||
42 | } | ||
diff --git a/client/src/app/shared/shared-main/video-channel/video-channel.service.ts b/client/src/app/shared/shared-main/video-channel/video-channel.service.ts new file mode 100644 index 000000000..5483e305f --- /dev/null +++ b/client/src/app/shared/shared-main/video-channel/video-channel.service.ts | |||
@@ -0,0 +1,94 @@ | |||
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 { Avatar, 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 | static extractVideoChannels (result: ResultList<VideoChannelServer>) { | ||
19 | const videoChannels: VideoChannel[] = [] | ||
20 | |||
21 | for (const videoChannelJSON of result.data) { | ||
22 | videoChannels.push(new VideoChannel(videoChannelJSON)) | ||
23 | } | ||
24 | |||
25 | return { data: videoChannels, total: result.total } | ||
26 | } | ||
27 | |||
28 | constructor ( | ||
29 | private authHttp: HttpClient, | ||
30 | private restService: RestService, | ||
31 | private restExtractor: RestExtractor | ||
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 ( | ||
44 | account: Account, | ||
45 | componentPagination?: ComponentPaginationLight, | ||
46 | withStats = false | ||
47 | ): Observable<ResultList<VideoChannel>> { | ||
48 | const pagination = componentPagination | ||
49 | ? this.restService.componentPaginationToRestPagination(componentPagination) | ||
50 | : { start: 0, count: 20 } | ||
51 | |||
52 | let params = new HttpParams() | ||
53 | params = this.restService.addRestGetParams(params, pagination) | ||
54 | params = params.set('withStats', withStats + '') | ||
55 | |||
56 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels' | ||
57 | return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params }) | ||
58 | .pipe( | ||
59 | map(res => VideoChannelService.extractVideoChannels(res)), | ||
60 | catchError(err => this.restExtractor.handleError(err)) | ||
61 | ) | ||
62 | } | ||
63 | |||
64 | createVideoChannel (videoChannel: VideoChannelCreate) { | ||
65 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | ||
66 | .pipe( | ||
67 | map(this.restExtractor.extractDataBool), | ||
68 | catchError(err => this.restExtractor.handleError(err)) | ||
69 | ) | ||
70 | } | ||
71 | |||
72 | updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) { | ||
73 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel) | ||
74 | .pipe( | ||
75 | map(this.restExtractor.extractDataBool), | ||
76 | catchError(err => this.restExtractor.handleError(err)) | ||
77 | ) | ||
78 | } | ||
79 | |||
80 | changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) { | ||
81 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick' | ||
82 | |||
83 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) | ||
84 | .pipe(catchError(err => this.restExtractor.handleError(err))) | ||
85 | } | ||
86 | |||
87 | removeVideoChannel (videoChannel: VideoChannel) { | ||
88 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost) | ||
89 | .pipe( | ||
90 | map(this.restExtractor.extractDataBool), | ||
91 | catchError(err => this.restExtractor.handleError(err)) | ||
92 | ) | ||
93 | } | ||
94 | } | ||