]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-edit.ts
Support custom value in ng-select
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-edit.ts
CommitLineData
bd45d503 1import { Directive, OnInit } from '@angular/core'
67ed6552
C
2import { ConfigService } from '@app/+admin/config/shared/config.service'
3import { AuthService, ScreenService, ServerService, User } from '@app/core'
4import { FormReactive } from '@app/shared/shared-forms'
bd45d503
C
5import { USER_ROLE_LABELS } from '@shared/core-utils/users'
6import { ServerConfig, UserAdminFlag, UserRole, VideoResolution } from '@shared/models'
8094a898 7
583eb04b 8@Directive()
a02b93ce 9// tslint:disable-next-line: directive-class-suffix
ba430d75 10export abstract class UserEdit extends FormReactive implements OnInit {
2bc9bd08
RK
11 videoQuotaOptions: { value: string, label: string, disabled?: boolean }[] = []
12 videoQuotaDailyOptions: { value: string, label: string, disabled?: boolean }[] = []
2fbe7f19 13 username: string
76314386 14 user: User
954605a8 15
a31bec51
C
16 roles: { value: string, label: string }[] = []
17
ba430d75
C
18 protected serverConfig: ServerConfig
19
6a84aafd 20 protected abstract serverService: ServerService
3827c3b3 21 protected abstract configService: ConfigService
76314386 22 protected abstract screenService: ScreenService
a95a4cc8 23 protected abstract auth: AuthService
8094a898
C
24 abstract isCreation (): boolean
25 abstract getFormButtonTitle (): string
6a84aafd 26
ba430d75
C
27 ngOnInit (): void {
28 this.serverConfig = this.serverService.getTmpConfig()
29 this.serverService.getConfig()
30 .subscribe(config => this.serverConfig = config)
a31bec51
C
31
32 this.buildRoles()
ba430d75
C
33 }
34
76314386
RK
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
6d989edc
C
45 getAuthPlugins () {
46 return this.serverConfig.plugin.registeredIdAndPassAuths.map(p => p.npmName)
47 .concat(this.serverConfig.plugin.registeredExternalAuths.map(p => p.npmName))
48 }
49
76314386
RK
50 isInBigView () {
51 return this.screenService.getWindowInnerWidth() > 1600
52 }
53
a31bec51 54 buildRoles () {
a95a4cc8
C
55 const authUser = this.auth.getUser()
56
57 if (authUser.role === UserRole.ADMINISTRATOR) {
a31bec51 58 this.roles = Object.keys(USER_ROLE_LABELS)
a95a4cc8 59 .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] }))
a31bec51 60 return
a95a4cc8
C
61 }
62
a31bec51 63 this.roles = [
a95a4cc8
C
64 { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] }
65 ]
66 }
67
6a84aafd
C
68 isTranscodingInformationDisplayed () {
69 const formVideoQuota = parseInt(this.form.value['videoQuota'], 10)
70
ba430d75 71 return this.serverConfig.transcoding.enabledResolutions.length !== 0 &&
6a84aafd
C
72 formVideoQuota > 0
73 }
74
75 computeQuotaWithTranscoding () {
ba430d75 76 const transcodingConfig = this.serverConfig.transcoding
09209296
C
77
78 const resolutions = transcodingConfig.enabledResolutions
ad3405d0 79 const higherResolution = VideoResolution.H_4K
6a84aafd
C
80 let multiplier = 0
81
82 for (const resolution of resolutions) {
83 multiplier += resolution / higherResolution
84 }
85
09209296
C
86 if (transcodingConfig.hls.enabled) multiplier *= 2
87
6a84aafd
C
88 return multiplier * parseInt(this.form.value['videoQuota'], 10)
89 }
3827c3b3 90
b426edd4
C
91 resetPassword () {
92 return
93 }
94
1eddc9a7 95 protected buildAdminFlags (formValue: any) {
3487330d 96 return formValue.byPassAutoBlock ? UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
1eddc9a7
C
97 }
98
3827c3b3
C
99 protected buildQuotaOptions () {
100 // These are used by a HTML select, so convert key into strings
101 this.videoQuotaOptions = this.configService
8e4aff44 102 .videoQuotaOptions.map(q => ({
471ee394
RK
103 value: q.value?.toString(),
104 label: q.label,
105 disabled: q.disabled
8e4aff44 106 }))
3827c3b3
C
107
108 this.videoQuotaDailyOptions = this.configService
471ee394
RK
109 .videoQuotaDailyOptions.map(q => ({
110 value: q.value?.toString(),
111 label: q.label,
112 disabled: q.disabled
113 }))
3827c3b3 114 }
8094a898 115}