Improving select displays, focus box-shadows for paginators, instructions for index url
[oweals/peertube.git] / client / src / app / +admin / users / user-edit / user-edit.ts
1 import { AuthService, ServerService } from '../../../core'
2 import { FormReactive } from '../../../shared'
3 import { ServerConfig, USER_ROLE_LABELS, UserRole, VideoResolution } from '../../../../../../shared'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { UserAdminFlag } from '@shared/models/users/user-flag.model'
6 import { OnInit } from '@angular/core'
7 import { User } from '@app/shared/users/user.model'
8 import { ScreenService } from '@app/shared/misc/screen.service'
9
10 export abstract class UserEdit extends FormReactive implements OnInit {
11   videoQuotaOptions: { value: string, label: string, disabled?: boolean }[] = []
12   videoQuotaDailyOptions: { value: string, label: string, disabled?: boolean }[] = []
13   username: string
14   user: User
15
16   roles: { value: string, label: string }[] = []
17
18   protected serverConfig: ServerConfig
19
20   protected abstract serverService: ServerService
21   protected abstract configService: ConfigService
22   protected abstract screenService: ScreenService
23   protected abstract auth: AuthService
24   abstract isCreation (): boolean
25   abstract getFormButtonTitle (): string
26
27   ngOnInit (): void {
28     this.serverConfig = this.serverService.getTmpConfig()
29     this.serverService.getConfig()
30         .subscribe(config => this.serverConfig = config)
31
32     this.buildRoles()
33   }
34
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
45   isInBigView () {
46     return this.screenService.getWindowInnerWidth() > 1600
47   }
48
49   buildRoles () {
50     const authUser = this.auth.getUser()
51
52     if (authUser.role === UserRole.ADMINISTRATOR) {
53       this.roles = Object.keys(USER_ROLE_LABELS)
54             .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] }))
55       return
56     }
57
58     this.roles = [
59       { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] }
60     ]
61   }
62
63   isTranscodingInformationDisplayed () {
64     const formVideoQuota = parseInt(this.form.value['videoQuota'], 10)
65
66     return this.serverConfig.transcoding.enabledResolutions.length !== 0 &&
67            formVideoQuota > 0
68   }
69
70   computeQuotaWithTranscoding () {
71     const transcodingConfig = this.serverConfig.transcoding
72
73     const resolutions = transcodingConfig.enabledResolutions
74     const higherResolution = VideoResolution.H_4K
75     let multiplier = 0
76
77     for (const resolution of resolutions) {
78       multiplier += resolution / higherResolution
79     }
80
81     if (transcodingConfig.hls.enabled) multiplier *= 2
82
83     return multiplier * parseInt(this.form.value['videoQuota'], 10)
84   }
85
86   resetPassword () {
87     return
88   }
89
90   protected buildAdminFlags (formValue: any) {
91     return formValue.byPassAutoBlacklist ? UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
92   }
93
94   protected buildQuotaOptions () {
95     // These are used by a HTML select, so convert key into strings
96     this.videoQuotaOptions = this.configService
97                                  .videoQuotaOptions.map(q => ({ value: q.value?.toString(), label: q.label, disabled: q.disabled }))
98
99     this.videoQuotaDailyOptions = this.configService
100                                       .videoQuotaDailyOptions.map(q => ({ value: q.value?.toString(), label: q.label, disabled: q.disabled }))
101
102     console.log(
103       this.videoQuotaOptions,
104       this.videoQuotaDailyOptions
105     )
106   }
107 }