]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-edit/user-edit.ts
Merge branch 'master' into release/3.3.0
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-edit.ts
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 { HTMLServerConfig, UserAdminFlag, UserRole, VideoResolution } from '@shared/models'
7 import { SelectOptionsItem } from '../../../../types/select-options-item.model'
8
9 @Directive()
10 // tslint:disable-next-line: directive-class-suffix
11 export 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: HTMLServerConfig
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.getHTMLConfig()
30
31 this.buildRoles()
32 }
33
34 get subscribersCount () {
35 const forAccount = this.user
36 ? this.user.account.followersCount
37 : 0
38 const forChannels = this.user
39 ? this.user.videoChannels.map(c => c.followersCount).reduce((a, b) => a + b, 0)
40 : 0
41 return forAccount + forChannels
42 }
43
44 getAuthPlugins () {
45 return this.serverConfig.plugin.registeredIdAndPassAuths.map(p => p.npmName)
46 .concat(this.serverConfig.plugin.registeredExternalAuths.map(p => p.npmName))
47 }
48
49 isInBigView () {
50 return this.screenService.getWindowInnerWidth() > 1600
51 }
52
53 buildRoles () {
54 const authUser = this.auth.getUser()
55
56 if (authUser.role === UserRole.ADMINISTRATOR) {
57 this.roles = Object.keys(USER_ROLE_LABELS)
58 .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] }))
59 return
60 }
61
62 this.roles = [
63 { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] }
64 ]
65 }
66
67 isTranscodingInformationDisplayed () {
68 const formVideoQuota = parseInt(this.form.value['videoQuota'], 10)
69
70 return this.serverConfig.transcoding.enabledResolutions.length !== 0 &&
71 formVideoQuota > 0
72 }
73
74 computeQuotaWithTranscoding () {
75 const transcodingConfig = this.serverConfig.transcoding
76
77 const resolutions = transcodingConfig.enabledResolutions
78 const higherResolution = VideoResolution.H_4K
79 let multiplier = 0
80
81 for (const resolution of resolutions) {
82 multiplier += resolution / higherResolution
83 }
84
85 if (transcodingConfig.hls.enabled) multiplier *= 2
86
87 return multiplier * parseInt(this.form.value['videoQuota'], 10)
88 }
89
90 resetPassword () {
91 return
92 }
93
94 protected buildAdminFlags (formValue: any) {
95 return formValue.byPassAutoBlock ? UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
96 }
97
98 protected buildQuotaOptions () {
99 this.videoQuotaOptions = this.configService.videoQuotaOptions
100 this.videoQuotaDailyOptions = this.configService.videoQuotaDailyOptions
101 }
102 }