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