]>
Commit | Line | Data |
---|---|---|
1 | import { Directive, OnInit } from '@angular/core' | |
2 | import { ConfigService } from '@app/+admin/config/shared/config.service' | |
3 | import { AuthService, ScreenService, ServerService, User } from '@app/core' | |
4 | import { FormReactive } from '@app/shared/shared-forms' | |
5 | import { USER_ROLE_LABELS } from '@shared/core-utils/users' | |
6 | import { ServerConfig, UserAdminFlag, UserRole, VideoResolution } from '@shared/models' | |
7 | ||
8 | @Directive() | |
9 | // tslint:disable-next-line: directive-class-suffix | |
10 | export abstract class UserEdit extends FormReactive implements OnInit { | |
11 | videoQuotaOptions: { value: string, label: string, disabled?: boolean }[] = [] | |
12 | videoQuotaDailyOptions: { value: string, label: string, disabled?: boolean }[] = [] | |
13 | username: string | |
14 | user: User | |
15 | ||
16 | roles: { value: string, label: string }[] = [] | |
17 | ||
18 | protected serverConfig: ServerConfig | |
19 | ||
20 | protected abstract serverService: ServerService | |
21 | protected abstract configService: ConfigService | |
22 | protected abstract screenService: ScreenService | |
23 | protected abstract auth: AuthService | |
24 | abstract isCreation (): boolean | |
25 | abstract getFormButtonTitle (): string | |
26 | ||
27 | ngOnInit (): void { | |
28 | this.serverConfig = this.serverService.getTmpConfig() | |
29 | this.serverService.getConfig() | |
30 | .subscribe(config => this.serverConfig = config) | |
31 | ||
32 | this.buildRoles() | |
33 | } | |
34 | ||
35 | get subscribersCount () { | |
36 | const forAccount = this.user | |
37 | ? this.user.account.followersCount | |
38 | : 0 | |
39 | const forChannels = this.user | |
40 | ? this.user.videoChannels.map(c => c.followersCount).reduce((a, b) => a + b, 0) | |
41 | : 0 | |
42 | return forAccount + forChannels | |
43 | } | |
44 | ||
45 | getAuthPlugins () { | |
46 | return this.serverConfig.plugin.registeredIdAndPassAuths.map(p => p.npmName) | |
47 | .concat(this.serverConfig.plugin.registeredExternalAuths.map(p => p.npmName)) | |
48 | } | |
49 | ||
50 | isInBigView () { | |
51 | return this.screenService.getWindowInnerWidth() > 1600 | |
52 | } | |
53 | ||
54 | buildRoles () { | |
55 | const authUser = this.auth.getUser() | |
56 | ||
57 | if (authUser.role === UserRole.ADMINISTRATOR) { | |
58 | this.roles = Object.keys(USER_ROLE_LABELS) | |
59 | .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] })) | |
60 | return | |
61 | } | |
62 | ||
63 | this.roles = [ | |
64 | { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] } | |
65 | ] | |
66 | } | |
67 | ||
68 | isTranscodingInformationDisplayed () { | |
69 | const formVideoQuota = parseInt(this.form.value['videoQuota'], 10) | |
70 | ||
71 | return this.serverConfig.transcoding.enabledResolutions.length !== 0 && | |
72 | formVideoQuota > 0 | |
73 | } | |
74 | ||
75 | computeQuotaWithTranscoding () { | |
76 | const transcodingConfig = this.serverConfig.transcoding | |
77 | ||
78 | const resolutions = transcodingConfig.enabledResolutions | |
79 | const higherResolution = VideoResolution.H_4K | |
80 | let multiplier = 0 | |
81 | ||
82 | for (const resolution of resolutions) { | |
83 | multiplier += resolution / higherResolution | |
84 | } | |
85 | ||
86 | if (transcodingConfig.hls.enabled) multiplier *= 2 | |
87 | ||
88 | return multiplier * parseInt(this.form.value['videoQuota'], 10) | |
89 | } | |
90 | ||
91 | resetPassword () { | |
92 | return | |
93 | } | |
94 | ||
95 | protected buildAdminFlags (formValue: any) { | |
96 | return formValue.byPassAutoBlock ? UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE | |
97 | } | |
98 | ||
99 | protected buildQuotaOptions () { | |
100 | // These are used by a HTML select, so convert key into strings | |
101 | this.videoQuotaOptions = this.configService | |
102 | .videoQuotaOptions.map(q => ({ | |
103 | value: q.value?.toString(), | |
104 | label: q.label, | |
105 | disabled: q.disabled | |
106 | })) | |
107 | ||
108 | this.videoQuotaDailyOptions = this.configService | |
109 | .videoQuotaDailyOptions.map(q => ({ | |
110 | value: q.value?.toString(), | |
111 | label: q.label, | |
112 | disabled: q.disabled | |
113 | })) | |
114 | } | |
115 | } |