]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Improve edit config submit error
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1
2 import { forkJoin } from 'rxjs'
3 import { SelectOptionsItem } from 'src/types/select-options-item.model'
4 import { Component, OnInit } from '@angular/core'
5 import { ActivatedRoute, Router } from '@angular/router'
6 import { ConfigService } from '@app/+admin/config/shared/config.service'
7 import { Notifier } from '@app/core'
8 import { ServerService } from '@app/core/server/server.service'
9 import {
10 ADMIN_EMAIL_VALIDATOR,
11 CACHE_CAPTIONS_SIZE_VALIDATOR,
12 CACHE_PREVIEWS_SIZE_VALIDATOR,
13 CONCURRENCY_VALIDATOR,
14 INDEX_URL_VALIDATOR,
15 INSTANCE_NAME_VALIDATOR,
16 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
17 MAX_INSTANCE_LIVES_VALIDATOR,
18 MAX_LIVE_DURATION_VALIDATOR,
19 MAX_USER_LIVES_VALIDATOR,
20 SEARCH_INDEX_URL_VALIDATOR,
21 SERVICES_TWITTER_USERNAME_VALIDATOR,
22 SIGNUP_LIMIT_VALIDATOR,
23 TRANSCODING_THREADS_VALIDATOR
24 } from '@app/shared/form-validators/custom-config-validators'
25 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
26 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
27 import { CustomConfig, ServerConfig } from '@shared/models'
28 import { EditConfigurationService } from './edit-configuration.service'
29
30 @Component({
31 selector: 'my-edit-custom-config',
32 templateUrl: './edit-custom-config.component.html',
33 styleUrls: [ './edit-custom-config.component.scss' ]
34 })
35 export class EditCustomConfigComponent extends FormReactive implements OnInit {
36 activeNav: string
37
38 customConfig: CustomConfig
39 serverConfig: ServerConfig
40
41 languageItems: SelectOptionsItem[] = []
42 categoryItems: SelectOptionsItem[] = []
43
44 constructor (
45 private router: Router,
46 private route: ActivatedRoute,
47 protected formValidatorService: FormValidatorService,
48 private notifier: Notifier,
49 private configService: ConfigService,
50 private serverService: ServerService,
51 private editConfigurationService: EditConfigurationService
52 ) {
53 super()
54 }
55
56 ngOnInit () {
57 this.serverConfig = this.serverService.getTmpConfig()
58 this.serverService.getConfig()
59 .subscribe(config => {
60 this.serverConfig = config
61 })
62
63 const formGroupData: { [key in keyof CustomConfig ]: any } = {
64 instance: {
65 name: INSTANCE_NAME_VALIDATOR,
66 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
67 description: null,
68
69 isNSFW: false,
70 defaultNSFWPolicy: null,
71
72 terms: null,
73 codeOfConduct: null,
74
75 creationReason: null,
76 moderationInformation: null,
77 administrator: null,
78 maintenanceLifetime: null,
79 businessModel: null,
80
81 hardwareInformation: null,
82
83 categories: null,
84 languages: null,
85
86 defaultClientRoute: null,
87
88 customizations: {
89 javascript: null,
90 css: null
91 }
92 },
93 theme: {
94 default: null
95 },
96 services: {
97 twitter: {
98 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
99 whitelisted: null
100 }
101 },
102 cache: {
103 previews: {
104 size: CACHE_PREVIEWS_SIZE_VALIDATOR
105 },
106 captions: {
107 size: CACHE_CAPTIONS_SIZE_VALIDATOR
108 }
109 },
110 signup: {
111 enabled: null,
112 limit: SIGNUP_LIMIT_VALIDATOR,
113 requiresEmailVerification: null
114 },
115 import: {
116 videos: {
117 concurrency: CONCURRENCY_VALIDATOR,
118 http: {
119 enabled: null
120 },
121 torrent: {
122 enabled: null
123 }
124 }
125 },
126 trending: {
127 videos: {
128 algorithms: {
129 enabled: null,
130 default: null
131 }
132 }
133 },
134 admin: {
135 email: ADMIN_EMAIL_VALIDATOR
136 },
137 contactForm: {
138 enabled: null
139 },
140 user: {
141 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
142 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
143 },
144 transcoding: {
145 enabled: null,
146 threads: TRANSCODING_THREADS_VALIDATOR,
147 allowAdditionalExtensions: null,
148 allowAudioFiles: null,
149 profile: null,
150 concurrency: CONCURRENCY_VALIDATOR,
151 resolutions: {},
152 hls: {
153 enabled: null
154 },
155 webtorrent: {
156 enabled: null
157 }
158 },
159 live: {
160 enabled: null,
161
162 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
163 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
164 maxUserLives: MAX_USER_LIVES_VALIDATOR,
165 allowReplay: null,
166
167 transcoding: {
168 enabled: null,
169 threads: TRANSCODING_THREADS_VALIDATOR,
170 profile: null,
171 resolutions: {}
172 }
173 },
174 autoBlacklist: {
175 videos: {
176 ofUsers: {
177 enabled: null
178 }
179 }
180 },
181 followers: {
182 instance: {
183 enabled: null,
184 manualApproval: null
185 }
186 },
187 followings: {
188 instance: {
189 autoFollowBack: {
190 enabled: null
191 },
192 autoFollowIndex: {
193 enabled: null,
194 indexUrl: INDEX_URL_VALIDATOR
195 }
196 }
197 },
198 broadcastMessage: {
199 enabled: null,
200 level: null,
201 dismissable: null,
202 message: null
203 },
204 search: {
205 remoteUri: {
206 users: null,
207 anonymous: null
208 },
209 searchIndex: {
210 enabled: null,
211 url: SEARCH_INDEX_URL_VALIDATOR,
212 disableLocalSearch: null,
213 isDefaultSearch: null
214 }
215 }
216 }
217
218 const defaultValues = {
219 transcoding: {
220 resolutions: {}
221 },
222 live: {
223 transcoding: {
224 resolutions: {}
225 }
226 }
227 }
228
229 for (const resolution of this.editConfigurationService.getVODResolutions()) {
230 defaultValues.transcoding.resolutions[resolution.id] = 'false'
231 formGroupData.transcoding.resolutions[resolution.id] = null
232 }
233
234 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
235 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
236 formGroupData.live.transcoding.resolutions[resolution.id] = null
237 }
238
239 this.buildForm(formGroupData)
240
241 if (this.route.snapshot.fragment) {
242 this.onNavChange(this.route.snapshot.fragment)
243 }
244
245 this.loadConfigAndUpdateForm()
246 this.loadCategoriesAndLanguages()
247 }
248
249 async formValidated () {
250 const value: CustomConfig = this.form.getRawValue()
251
252 this.configService.updateCustomConfig(value)
253 .subscribe(
254 res => {
255 this.customConfig = res
256
257 // Reload general configuration
258 this.serverService.resetConfig()
259
260 this.updateForm()
261
262 this.notifier.success($localize`Configuration updated.`)
263 },
264
265 err => this.notifier.error(err.message)
266 )
267 }
268
269 hasConsistentOptions () {
270 if (this.hasLiveAllowReplayConsistentOptions()) return true
271
272 return false
273 }
274
275 hasLiveAllowReplayConsistentOptions () {
276 if (
277 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
278 this.editConfigurationService.isLiveEnabled(this.form) &&
279 this.form.value['live']['allowReplay'] === true
280 ) {
281 return false
282 }
283
284 return true
285 }
286
287 onNavChange (newActiveNav: string) {
288 this.activeNav = newActiveNav
289
290 this.router.navigate([], { fragment: this.activeNav })
291 }
292
293 grabAllErrors (errorObjectArg?: any) {
294 const errorObject = errorObjectArg || this.formErrors
295
296 let acc: string[] = []
297
298 for (const key of Object.keys(errorObject)) {
299 const value = errorObject[key]
300 if (!value) continue
301
302 if (typeof value === 'string') {
303 acc.push(value)
304 } else {
305 acc = acc.concat(this.grabAllErrors(value))
306 }
307 }
308
309 return acc
310 }
311
312 private updateForm () {
313 this.form.patchValue(this.customConfig)
314 }
315
316 private loadConfigAndUpdateForm () {
317 this.configService.getCustomConfig()
318 .subscribe(config => {
319 this.customConfig = config
320
321 this.updateForm()
322 // Force form validation
323 this.forceCheck()
324 },
325
326 err => this.notifier.error(err.message)
327 )
328 }
329
330 private loadCategoriesAndLanguages () {
331 forkJoin([
332 this.serverService.getVideoLanguages(),
333 this.serverService.getVideoCategories()
334 ]).subscribe(
335 ([ languages, categories ]) => {
336 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
337 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
338 },
339
340 err => this.notifier.error(err.message)
341 )
342 }
343 }