user-edit.ts 3.14 KB
Newer Older
Chocobozzz's avatar
Chocobozzz committed
1
import { Directive, OnInit } from '@angular/core'
Chocobozzz's avatar
Chocobozzz committed
2
3
4
import { ConfigService } from '@app/+admin/config/shared/config.service'
import { AuthService, ScreenService, ServerService, User } from '@app/core'
import { FormReactive } from '@app/shared/shared-forms'
Chocobozzz's avatar
Chocobozzz committed
5
import { USER_ROLE_LABELS } from '@shared/core-utils/users'
Chocobozzz's avatar
Chocobozzz committed
6
import { HTMLServerConfig, UserAdminFlag, UserRole, VideoResolution } from '@shared/models'
Chocobozzz's avatar
Chocobozzz committed
7
import { SelectOptionsItem } from '../../../../types/select-options-item.model'
Chocobozzz's avatar
Chocobozzz committed
8

Chocobozzz's avatar
Chocobozzz committed
9
@Directive()
Chocobozzz's avatar
Chocobozzz committed
10
// tslint:disable-next-line: directive-class-suffix
Chocobozzz's avatar
Chocobozzz committed
11
export abstract class UserEdit extends FormReactive implements OnInit {
Chocobozzz's avatar
Chocobozzz committed
12
13
  videoQuotaOptions: SelectOptionsItem[] = []
  videoQuotaDailyOptions: SelectOptionsItem[] = []
Chocobozzz's avatar
Chocobozzz committed
14
  username: string
15
  user: User
16

Chocobozzz's avatar
Chocobozzz committed
17
18
  roles: { value: string, label: string }[] = []

Chocobozzz's avatar
Chocobozzz committed
19
  protected serverConfig: HTMLServerConfig
Chocobozzz's avatar
Chocobozzz committed
20

21
  protected abstract serverService: ServerService
Chocobozzz's avatar
Chocobozzz committed
22
  protected abstract configService: ConfigService
23
  protected abstract screenService: ScreenService
Chocobozzz's avatar
Chocobozzz committed
24
  protected abstract auth: AuthService
Chocobozzz's avatar
Chocobozzz committed
25
26
  abstract isCreation (): boolean
  abstract getFormButtonTitle (): string
27

Chocobozzz's avatar
Chocobozzz committed
28
  ngOnInit (): void {
Chocobozzz's avatar
Chocobozzz committed
29
    this.serverConfig = this.serverService.getHTMLConfig()
Chocobozzz's avatar
Chocobozzz committed
30
31

    this.buildRoles()
Chocobozzz's avatar
Chocobozzz committed
32
33
  }

34
35
36
37
38
39
40
41
42
43
  get subscribersCount () {
    const forAccount = this.user
      ? this.user.account.followersCount
      : 0
    const forChannels = this.user
      ? this.user.videoChannels.map(c => c.followersCount).reduce((a, b) => a + b, 0)
      : 0
    return forAccount + forChannels
  }

Chocobozzz's avatar
Chocobozzz committed
44
45
46
47
48
  getAuthPlugins () {
    return this.serverConfig.plugin.registeredIdAndPassAuths.map(p => p.npmName)
      .concat(this.serverConfig.plugin.registeredExternalAuths.map(p => p.npmName))
  }

49
50
51
52
  isInBigView () {
    return this.screenService.getWindowInnerWidth() > 1600
  }

Chocobozzz's avatar
Chocobozzz committed
53
  buildRoles () {
Chocobozzz's avatar
Chocobozzz committed
54
55
56
    const authUser = this.auth.getUser()

    if (authUser.role === UserRole.ADMINISTRATOR) {
Chocobozzz's avatar
Chocobozzz committed
57
      this.roles = Object.keys(USER_ROLE_LABELS)
Chocobozzz's avatar
Chocobozzz committed
58
            .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] }))
Chocobozzz's avatar
Chocobozzz committed
59
      return
Chocobozzz's avatar
Chocobozzz committed
60
61
    }

Chocobozzz's avatar
Chocobozzz committed
62
    this.roles = [
Chocobozzz's avatar
Chocobozzz committed
63
64
65
66
      { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] }
    ]
  }

67
68
69
  isTranscodingInformationDisplayed () {
    const formVideoQuota = parseInt(this.form.value['videoQuota'], 10)

Chocobozzz's avatar
Chocobozzz committed
70
    return this.serverConfig.transcoding.enabledResolutions.length !== 0 &&
71
72
73
74
           formVideoQuota > 0
  }

  computeQuotaWithTranscoding () {
Chocobozzz's avatar
Chocobozzz committed
75
    const transcodingConfig = this.serverConfig.transcoding
Chocobozzz's avatar
Chocobozzz committed
76
77

    const resolutions = transcodingConfig.enabledResolutions
78
    const higherResolution = VideoResolution.H_4K
79
80
81
82
83
84
    let multiplier = 0

    for (const resolution of resolutions) {
      multiplier += resolution / higherResolution
    }

Chocobozzz's avatar
Chocobozzz committed
85
86
    if (transcodingConfig.hls.enabled) multiplier *= 2

87
88
    return multiplier * parseInt(this.form.value['videoQuota'], 10)
  }
Chocobozzz's avatar
Chocobozzz committed
89

90
91
92
93
  resetPassword () {
    return
  }

Chocobozzz's avatar
Chocobozzz committed
94
  protected buildAdminFlags (formValue: any) {
95
    return formValue.byPassAutoBlock ? UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
Chocobozzz's avatar
Chocobozzz committed
96
97
  }

Chocobozzz's avatar
Chocobozzz committed
98
  protected buildQuotaOptions () {
Chocobozzz's avatar
Chocobozzz committed
99
100
    this.videoQuotaOptions = this.configService.videoQuotaOptions
    this.videoQuotaDailyOptions = this.configService.videoQuotaDailyOptions
Chocobozzz's avatar
Chocobozzz committed
101
  }
Chocobozzz's avatar
Chocobozzz committed
102
}