]> 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 embed a video in Twitter
[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, SERVICES_TWITTER_USERNAME,
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 servicesTwitterUsername: '',
53 cachePreviewsSize: '',
54 signupLimit: '',
55 adminEmail: '',
56 userVideoQuota: '',
57 transcodingThreads: '',
58 customizationJavascript: '',
59 customizationCSS: ''
60 }
61 validationMessages = {
62 instanceShortDescription: INSTANCE_SHORT_DESCRIPTION.MESSAGES,
63 instanceName: INSTANCE_NAME.MESSAGES,
64 servicesTwitterUsername: SERVICES_TWITTER_USERNAME,
65 cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES,
66 signupLimit: SIGNUP_LIMIT.MESSAGES,
67 adminEmail: ADMIN_EMAIL.MESSAGES,
68 userVideoQuota: USER_VIDEO_QUOTA.MESSAGES
69 }
70
71 private oldCustomJavascript: string
72 private oldCustomCSS: string
73
74 constructor (
75 private formBuilder: FormBuilder,
76 private router: Router,
77 private notificationsService: NotificationsService,
78 private configService: ConfigService,
79 private serverService: ServerService,
80 private confirmService: ConfirmService
81 ) {
82 super()
83 }
84
85 getResolutionKey (resolution: string) {
86 return 'transcodingResolution' + resolution
87 }
88
89 buildForm () {
90 const formGroupData = {
91 instanceName: [ '', INSTANCE_NAME.VALIDATORS ],
92 instanceShortDescription: [ '', INSTANCE_SHORT_DESCRIPTION.VALIDATORS ],
93 instanceDescription: [ '' ],
94 instanceTerms: [ '' ],
95 instanceDefaultClientRoute: [ '' ],
96 instanceDefaultNSFWPolicy: [ '' ],
97 servicesTwitterUsername: [ '', SERVICES_TWITTER_USERNAME.VALIDATORS ],
98 servicesTwitterWhitelisted: [ ],
99 cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ],
100 signupEnabled: [ ],
101 signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ],
102 adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ],
103 userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ],
104 transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ],
105 transcodingEnabled: [ ],
106 customizationJavascript: [ '' ],
107 customizationCSS: [ '' ]
108 }
109
110 for (const resolution of this.resolutions) {
111 const key = this.getResolutionKey(resolution)
112 formGroupData[key] = [ false ]
113 }
114
115 this.form = this.formBuilder.group(formGroupData)
116
117 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
118 }
119
120 ngOnInit () {
121 this.buildForm()
122
123 this.configService.getCustomConfig()
124 .subscribe(
125 res => {
126 this.customConfig = res
127
128 this.oldCustomCSS = this.customConfig.instance.customizations.css
129 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
130
131 this.updateForm()
132 // Force form validation
133 this.forceCheck()
134 },
135
136 err => this.notificationsService.error('Error', err.message)
137 )
138 }
139
140 isTranscodingEnabled () {
141 return this.form.value['transcodingEnabled'] === true
142 }
143
144 isSignupEnabled () {
145 return this.form.value['signupEnabled'] === true
146 }
147
148 async formValidated () {
149 const newCustomizationJavascript = this.form.value['customizationJavascript']
150 const newCustomizationCSS = this.form.value['customizationCSS']
151
152 const customizations = []
153 if (newCustomizationJavascript && newCustomizationJavascript !== this.oldCustomJavascript) customizations.push('JavaScript')
154 if (newCustomizationCSS && newCustomizationCSS !== this.oldCustomCSS) customizations.push('CSS')
155
156 if (customizations.length !== 0) {
157 const customizationsText = customizations.join('/')
158
159 const message = `You set custom ${customizationsText}. ` +
160 'This could lead to security issues or bugs if you do not understand it. ' +
161 'Are you sure you want to update the configuration?'
162 const label = `Please type "I understand the ${customizationsText} I set" to confirm.`
163 const expectedInputValue = `I understand the ${customizationsText} I set`
164
165 const confirmRes = await this.confirmService.confirmWithInput(message, label, expectedInputValue)
166 if (confirmRes === false) return
167 }
168
169 const data: CustomConfig = {
170 instance: {
171 name: this.form.value['instanceName'],
172 shortDescription: this.form.value['instanceShortDescription'],
173 description: this.form.value['instanceDescription'],
174 terms: this.form.value['instanceTerms'],
175 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
176 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
177 customizations: {
178 javascript: this.form.value['customizationJavascript'],
179 css: this.form.value['customizationCSS']
180 }
181 },
182 services: {
183 twitter: {
184 username: this.form.value['servicesTwitterUsername'],
185 whitelisted: this.form.value['servicesTwitterWhitelisted']
186 }
187 },
188 cache: {
189 previews: {
190 size: this.form.value['cachePreviewsSize']
191 }
192 },
193 signup: {
194 enabled: this.form.value['signupEnabled'],
195 limit: this.form.value['signupLimit']
196 },
197 admin: {
198 email: this.form.value['adminEmail']
199 },
200 user: {
201 videoQuota: this.form.value['userVideoQuota']
202 },
203 transcoding: {
204 enabled: this.form.value['transcodingEnabled'],
205 threads: this.form.value['transcodingThreads'],
206 resolutions: {
207 '240p': this.form.value[this.getResolutionKey('240p')],
208 '360p': this.form.value[this.getResolutionKey('360p')],
209 '480p': this.form.value[this.getResolutionKey('480p')],
210 '720p': this.form.value[this.getResolutionKey('720p')],
211 '1080p': this.form.value[this.getResolutionKey('1080p')]
212 }
213 }
214 }
215
216 this.configService.updateCustomConfig(data)
217 .subscribe(
218 res => {
219 this.customConfig = res
220
221 // Reload general configuration
222 this.serverService.loadConfig()
223
224 this.updateForm()
225
226 this.notificationsService.success('Success', 'Configuration updated.')
227 },
228
229 err => this.notificationsService.error('Error', err.message)
230 )
231 }
232
233 private updateForm () {
234 const data = {
235 instanceName: this.customConfig.instance.name,
236 instanceShortDescription: this.customConfig.instance.shortDescription,
237 instanceDescription: this.customConfig.instance.description,
238 instanceTerms: this.customConfig.instance.terms,
239 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
240 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
241 servicesTwitterUsername: this.customConfig.services.twitter.username,
242 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
243 cachePreviewsSize: this.customConfig.cache.previews.size,
244 signupEnabled: this.customConfig.signup.enabled,
245 signupLimit: this.customConfig.signup.limit,
246 adminEmail: this.customConfig.admin.email,
247 userVideoQuota: this.customConfig.user.videoQuota,
248 transcodingThreads: this.customConfig.transcoding.threads,
249 transcodingEnabled: this.customConfig.transcoding.enabled,
250 customizationJavascript: this.customConfig.instance.customizations.javascript,
251 customizationCSS: this.customConfig.instance.customizations.css
252 }
253
254 for (const resolution of this.resolutions) {
255 const key = this.getResolutionKey(resolution)
256 data[key] = this.customConfig.transcoding.resolutions[resolution]
257 }
258
259 this.form.patchValue(data)
260 }
261
262 }