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