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