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'
11 ADMIN_EMAIL_VALIDATOR,
12 CACHE_CAPTIONS_SIZE_VALIDATOR,
13 CACHE_PREVIEWS_SIZE_VALIDATOR,
14 CONCURRENCY_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 SEARCH_INDEX_URL_VALIDATOR,
22 SERVICES_TWITTER_USERNAME_VALIDATOR,
23 SIGNUP_LIMIT_VALIDATOR,
24 SIGNUP_MINIMUM_AGE_VALIDATOR,
25 TRANSCODING_THREADS_VALIDATOR
26 } from '@app/shared/form-validators/custom-config-validators'
27 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
28 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
29 import { CustomPageService } from '@app/shared/shared-main/custom-page'
30 import { CustomConfig, CustomPage, ServerConfig } from '@shared/models'
31 import { EditConfigurationService } from './edit-configuration.service'
33 type ComponentCustomConfig = CustomConfig & {
34 instanceCustomHomepage: CustomPage
38 selector: 'my-edit-custom-config',
39 templateUrl: './edit-custom-config.component.html',
40 styleUrls: [ './edit-custom-config.component.scss' ]
42 export class EditCustomConfigComponent extends FormReactive implements OnInit {
45 customConfig: ComponentCustomConfig
46 serverConfig: ServerConfig
50 languageItems: SelectOptionsItem[] = []
51 categoryItems: SelectOptionsItem[] = []
54 private router: Router,
55 private route: ActivatedRoute,
56 protected formValidatorService: FormValidatorService,
57 private notifier: Notifier,
58 private configService: ConfigService,
59 private customPage: CustomPageService,
60 private serverService: ServerService,
61 private editConfigurationService: EditConfigurationService
67 this.serverConfig = this.serverService.getTmpConfig()
68 this.serverService.getConfig()
69 .subscribe(config => this.serverConfig = config)
71 const formGroupData: { [key in keyof ComponentCustomConfig ]: any } = {
73 name: INSTANCE_NAME_VALIDATOR,
74 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
78 defaultNSFWPolicy: null,
84 moderationInformation: null,
86 maintenanceLifetime: null,
89 hardwareInformation: null,
94 defaultClientRoute: null,
106 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
112 size: CACHE_PREVIEWS_SIZE_VALIDATOR
115 size: CACHE_CAPTIONS_SIZE_VALIDATOR
118 size: CACHE_CAPTIONS_SIZE_VALIDATOR
123 limit: SIGNUP_LIMIT_VALIDATOR,
124 requiresEmailVerification: null,
125 minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR
129 concurrency: CONCURRENCY_VALIDATOR,
147 email: ADMIN_EMAIL_VALIDATOR
153 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
154 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
158 threads: TRANSCODING_THREADS_VALIDATOR,
159 allowAdditionalExtensions: null,
160 allowAudioFiles: null,
162 concurrency: CONCURRENCY_VALIDATOR,
174 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
175 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
176 maxUserLives: MAX_USER_LIVES_VALIDATOR,
181 threads: TRANSCODING_THREADS_VALIDATOR,
206 indexUrl: INDEX_URL_VALIDATOR
223 url: SEARCH_INDEX_URL_VALIDATOR,
224 disableLocalSearch: null,
225 isDefaultSearch: null
229 instanceCustomHomepage: {
234 const defaultValues = {
245 for (const resolution of this.editConfigurationService.getVODResolutions()) {
246 defaultValues.transcoding.resolutions[resolution.id] = 'false'
247 formGroupData.transcoding.resolutions[resolution.id] = null
250 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
251 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
252 formGroupData.live.transcoding.resolutions[resolution.id] = null
255 this.buildForm(formGroupData)
257 if (this.route.snapshot.fragment) {
258 this.onNavChange(this.route.snapshot.fragment)
261 this.loadConfigAndUpdateForm()
262 this.loadCategoriesAndLanguages()
265 async formValidated () {
266 const value: ComponentCustomConfig = this.form.getRawValue()
269 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
270 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
274 const instanceCustomHomepage = {
275 content: value.instanceCustomHomepage.content
278 this.customConfig = { ...resConfig, instanceCustomHomepage }
280 // Reload general configuration
281 this.serverService.resetConfig()
282 .subscribe(config => this.serverConfig = config)
286 this.notifier.success($localize`Configuration updated.`)
289 err => this.notifier.error(err.message)
293 hasConsistentOptions () {
294 if (this.hasLiveAllowReplayConsistentOptions()) return true
299 hasLiveAllowReplayConsistentOptions () {
301 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
302 this.editConfigurationService.isLiveEnabled(this.form) &&
303 this.form.value['live']['allowReplay'] === true
311 onNavChange (newActiveNav: string) {
312 this.activeNav = newActiveNav
314 this.router.navigate([], { fragment: this.activeNav })
317 grabAllErrors (errorObjectArg?: any) {
318 const errorObject = errorObjectArg || this.formErrors
320 let acc: string[] = []
322 for (const key of Object.keys(errorObject)) {
323 const value = errorObject[key]
326 if (typeof value === 'string') {
329 acc = acc.concat(this.grabAllErrors(value))
336 private updateForm () {
337 this.form.patchValue(this.customConfig)
340 private loadConfigAndUpdateForm () {
342 this.configService.getCustomConfig(),
343 this.customPage.getInstanceHomepage()
345 .subscribe(([ config, homepage ]) => {
346 this.customConfig = { ...config, instanceCustomHomepage: homepage }
349 // Force form validation
353 err => this.notifier.error(err.message)
357 private loadCategoriesAndLanguages () {
359 this.serverService.getVideoLanguages(),
360 this.serverService.getVideoCategories()
362 ([ languages, categories ]) => {
363 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
364 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
367 err => this.notifier.error(err.message)