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