]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Form validators refractoring
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { Router } from '@angular/router'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { ConfirmService } from '@app/core'
6 import { ServerService } from '@app/core/server/server.service'
7 import { FormReactive, USER_VIDEO_QUOTA } from '@app/shared'
8 import {
9 ADMIN_EMAIL,
10 CACHE_PREVIEWS_SIZE,
11 INSTANCE_NAME,
12 INSTANCE_SHORT_DESCRIPTION,
13 SERVICES_TWITTER_USERNAME,
14 SIGNUP_LIMIT,
15 TRANSCODING_THREADS
16 } from '@app/shared/forms/form-validators/custom-config'
17 import { NotificationsService } from 'angular2-notifications'
18 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
19 import { I18n } from '@ngx-translate/i18n-polyfill'
20 import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
21
22 @Component({
23 selector: 'my-edit-custom-config',
24 templateUrl: './edit-custom-config.component.html',
25 styleUrls: [ './edit-custom-config.component.scss' ]
26 })
27 export class EditCustomConfigComponent extends FormReactive implements OnInit {
28 customConfig: CustomConfig
29 resolutions = [ '240p', '360p', '480p', '720p', '1080p' ]
30
31 videoQuotaOptions = [
32 { value: -1, label: 'Unlimited' },
33 { value: 0, label: '0' },
34 { value: 100 * 1024 * 1024, label: '100MB' },
35 { value: 500 * 1024 * 1024, label: '500MB' },
36 { value: 1024 * 1024 * 1024, label: '1GB' },
37 { value: 5 * 1024 * 1024 * 1024, label: '5GB' },
38 { value: 20 * 1024 * 1024 * 1024, label: '20GB' },
39 { value: 50 * 1024 * 1024 * 1024, label: '50GB' }
40 ]
41 transcodingThreadOptions = [
42 { value: 1, label: '1' },
43 { value: 2, label: '2' },
44 { value: 4, label: '4' },
45 { value: 8, label: '8' }
46 ]
47
48 private oldCustomJavascript: string
49 private oldCustomCSS: string
50
51 constructor (
52 protected formValidatorService: FormValidatorService,
53 private router: Router,
54 private notificationsService: NotificationsService,
55 private configService: ConfigService,
56 private serverService: ServerService,
57 private confirmService: ConfirmService,
58 private i18n: I18n
59 ) {
60 super()
61 }
62
63 getResolutionKey (resolution: string) {
64 return 'transcodingResolution' + resolution
65 }
66
67 ngOnInit () {
68 const formGroupData = {
69 instanceName: INSTANCE_NAME,
70 instanceShortDescription: INSTANCE_SHORT_DESCRIPTION,
71 instanceDescription: null,
72 instanceTerms: null,
73 instanceDefaultClientRoute: null,
74 instanceDefaultNSFWPolicy: null,
75 servicesTwitterUsername: SERVICES_TWITTER_USERNAME,
76 servicesTwitterWhitelisted: null,
77 cachePreviewsSize: CACHE_PREVIEWS_SIZE,
78 signupEnabled: null,
79 signupLimit: SIGNUP_LIMIT,
80 adminEmail: ADMIN_EMAIL,
81 userVideoQuota: USER_VIDEO_QUOTA,
82 transcodingThreads: TRANSCODING_THREADS,
83 transcodingEnabled: null,
84 customizationJavascript: null,
85 customizationCSS: null
86 }
87
88 const defaultValues: BuildFormDefaultValues = {}
89 for (const resolution of this.resolutions) {
90 const key = this.getResolutionKey(resolution)
91 defaultValues[key] = 'false'
92 formGroupData[key] = null
93 }
94
95 this.buildForm(formGroupData)
96
97 this.configService.getCustomConfig()
98 .subscribe(
99 res => {
100 this.customConfig = res
101
102 this.oldCustomCSS = this.customConfig.instance.customizations.css
103 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
104
105 this.updateForm()
106 // Force form validation
107 this.forceCheck()
108 },
109
110 err => this.notificationsService.error(this.i18n('Error'), err.message)
111 )
112 }
113
114 isTranscodingEnabled () {
115 return this.form.value['transcodingEnabled'] === true
116 }
117
118 isSignupEnabled () {
119 return this.form.value['signupEnabled'] === true
120 }
121
122 async formValidated () {
123 const newCustomizationJavascript = this.form.value['customizationJavascript']
124 const newCustomizationCSS = this.form.value['customizationCSS']
125
126 const customizations = []
127 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
128 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
129
130 if (customizations.length !== 0) {
131 const customizationsText = customizations.join('/')
132
133 // FIXME: i18n service does not support string concatenation
134 const message = this.i18n('You set custom {{customizationsText}}. ', { customizationsText }) +
135 this.i18n('This could lead to security issues or bugs if you do not understand it. ') +
136 this.i18n('Are you sure you want to update the configuration?')
137 const label = this.i18n(
138 'Please type "I understand the {{customizationsText}} I set" to confirm.',
139 { customizationsText }
140 )
141 const expectedInputValue = this.i18n('I understand the {{customizationsText}} I set', { customizationsText })
142
143 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
144 if (confirmRes === false) return
145 }
146
147 const data: CustomConfig = {
148 instance: {
149 name: this.form.value['instanceName'],
150 shortDescription: this.form.value['instanceShortDescription'],
151 description: this.form.value['instanceDescription'],
152 terms: this.form.value['instanceTerms'],
153 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
154 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
155 customizations: {
156 javascript: this.form.value['customizationJavascript'],
157 css: this.form.value['customizationCSS']
158 }
159 },
160 services: {
161 twitter: {
162 username: this.form.value['servicesTwitterUsername'],
163 whitelisted: this.form.value['servicesTwitterWhitelisted']
164 }
165 },
166 cache: {
167 previews: {
168 size: this.form.value['cachePreviewsSize']
169 }
170 },
171 signup: {
172 enabled: this.form.value['signupEnabled'],
173 limit: this.form.value['signupLimit']
174 },
175 admin: {
176 email: this.form.value['adminEmail']
177 },
178 user: {
179 videoQuota: this.form.value['userVideoQuota']
180 },
181 transcoding: {
182 enabled: this.form.value['transcodingEnabled'],
183 threads: this.form.value['transcodingThreads'],
184 resolutions: {
185 '240p': this.form.value[this.getResolutionKey('240p')],
186 '360p': this.form.value[this.getResolutionKey('360p')],
187 '480p': this.form.value[this.getResolutionKey('480p')],
188 '720p': this.form.value[this.getResolutionKey('720p')],
189 '1080p': this.form.value[this.getResolutionKey('1080p')]
190 }
191 }
192 }
193
194 this.configService.updateCustomConfig(data)
195 .subscribe(
196 res => {
197 this.customConfig = res
198
199 // Reload general configuration
200 this.serverService.loadConfig()
201
202 this.updateForm()
203
204 this.notificationsService.success(this.i18n('Success'), this.i18n('Configuration updated.'))
205 },
206
207 err => this.notificationsService.error(this.i18n('Error'), err.message)
208 )
209 }
210
211 private updateForm () {
212 const data = {
213 instanceName: this.customConfig.instance.name,
214 instanceShortDescription: this.customConfig.instance.shortDescription,
215 instanceDescription: this.customConfig.instance.description,
216 instanceTerms: this.customConfig.instance.terms,
217 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
218 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
219 servicesTwitterUsername: this.customConfig.services.twitter.username,
220 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
221 cachePreviewsSize: this.customConfig.cache.previews.size,
222 signupEnabled: this.customConfig.signup.enabled,
223 signupLimit: this.customConfig.signup.limit,
224 adminEmail: this.customConfig.admin.email,
225 userVideoQuota: this.customConfig.user.videoQuota,
226 transcodingThreads: this.customConfig.transcoding.threads,
227 transcodingEnabled: this.customConfig.transcoding.enabled,
228 customizationJavascript: this.customConfig.instance.customizations.javascript,
229 customizationCSS: this.customConfig.instance.customizations.css
230 }
231
232 for (const resolution of this.resolutions) {
233 const key = this.getResolutionKey(resolution)
234 data[key] = this.customConfig.transcoding.resolutions[resolution]
235 }
236
237 this.form.patchValue(data)
238 }
239
240 }