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'
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 }[] = []
16 roles: { value: string, label: string }[] = []
18 protected serverConfig: ServerConfig
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
28 this.serverConfig = this.serverService.getTmpConfig()
29 this.serverService.getConfig()
30 .subscribe(config => this.serverConfig = config)
35 get subscribersCount () {
36 const forAccount = this.user
37 ? this.user.account.followersCount
39 const forChannels = this.user
40 ? this.user.videoChannels.map(c => c.followersCount).reduce((a, b) => a + b, 0)
42 return forAccount + forChannels
46 return this.screenService.getWindowInnerWidth() > 1600
50 const authUser = this.auth.getUser()
52 if (authUser.role === UserRole.ADMINISTRATOR) {
53 this.roles = Object.keys(USER_ROLE_LABELS)
54 .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] }))
59 { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] }
63 isTranscodingInformationDisplayed () {
64 const formVideoQuota = parseInt(this.form.value['videoQuota'], 10)
66 return this.serverConfig.transcoding.enabledResolutions.length !== 0 &&
70 computeQuotaWithTranscoding () {
71 const transcodingConfig = this.serverConfig.transcoding
73 const resolutions = transcodingConfig.enabledResolutions
74 const higherResolution = VideoResolution.H_4K
77 for (const resolution of resolutions) {
78 multiplier += resolution / higherResolution
81 if (transcodingConfig.hls.enabled) multiplier *= 2
83 return multiplier * parseInt(this.form.value['videoQuota'], 10)
90 protected buildAdminFlags (formValue: any) {
91 return formValue.byPassAutoBlock ? UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
94 protected buildQuotaOptions () {
95 // These are used by a HTML select, so convert key into strings
96 this.videoQuotaOptions = this.configService
97 .videoQuotaOptions.map(q => ({
98 value: q.value?.toString(),
103 this.videoQuotaDailyOptions = this.configService
104 .videoQuotaDailyOptions.map(q => ({
105 value: q.value?.toString(),