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