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