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, HTMLServerConfig } 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: HTMLServerConfig
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.getHTMLConfig()
69 const formGroupData: { [key in keyof ComponentCustomConfig ]: any } = {
71 name: INSTANCE_NAME_VALIDATOR,
72 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
76 defaultNSFWPolicy: null,
82 moderationInformation: null,
84 maintenanceLifetime: null,
87 hardwareInformation: null,
92 defaultClientRoute: null,
104 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
110 size: CACHE_PREVIEWS_SIZE_VALIDATOR
113 size: CACHE_CAPTIONS_SIZE_VALIDATOR
116 size: CACHE_CAPTIONS_SIZE_VALIDATOR
121 limit: SIGNUP_LIMIT_VALIDATOR,
122 requiresEmailVerification: null,
123 minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR
127 concurrency: CONCURRENCY_VALIDATOR,
145 email: ADMIN_EMAIL_VALIDATOR
151 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
152 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
156 threads: TRANSCODING_THREADS_VALIDATOR,
157 allowAdditionalExtensions: null,
158 allowAudioFiles: null,
160 concurrency: CONCURRENCY_VALIDATOR,
172 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
173 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
174 maxUserLives: MAX_USER_LIVES_VALIDATOR,
179 threads: TRANSCODING_THREADS_VALIDATOR,
204 indexUrl: INDEX_URL_VALIDATOR
221 url: SEARCH_INDEX_URL_VALIDATOR,
222 disableLocalSearch: null,
223 isDefaultSearch: null
227 instanceCustomHomepage: {
232 const defaultValues = {
243 for (const resolution of this.editConfigurationService.getVODResolutions()) {
244 defaultValues.transcoding.resolutions[resolution.id] = 'false'
245 formGroupData.transcoding.resolutions[resolution.id] = null
248 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
249 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
250 formGroupData.live.transcoding.resolutions[resolution.id] = null
253 this.buildForm(formGroupData)
255 if (this.route.snapshot.fragment) {
256 this.onNavChange(this.route.snapshot.fragment)
259 this.loadConfigAndUpdateForm()
260 this.loadCategoriesAndLanguages()
263 async formValidated () {
264 const value: ComponentCustomConfig = this.form.getRawValue()
267 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
268 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
272 const instanceCustomHomepage = {
273 content: value.instanceCustomHomepage.content
276 this.customConfig = { ...resConfig, instanceCustomHomepage }
278 // Reload general configuration
279 this.serverService.resetConfig()
280 .subscribe(config => this.serverConfig = config)
284 this.notifier.success($localize`Configuration updated.`)
287 err => this.notifier.error(err.message)
291 hasConsistentOptions () {
292 if (this.hasLiveAllowReplayConsistentOptions()) return true
297 hasLiveAllowReplayConsistentOptions () {
299 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
300 this.editConfigurationService.isLiveEnabled(this.form) &&
301 this.form.value['live']['allowReplay'] === true
309 onNavChange (newActiveNav: string) {
310 this.activeNav = newActiveNav
312 this.router.navigate([], { fragment: this.activeNav })
315 grabAllErrors (errorObjectArg?: any) {
316 const errorObject = errorObjectArg || this.formErrors
318 let acc: string[] = []
320 for (const key of Object.keys(errorObject)) {
321 const value = errorObject[key]
324 if (typeof value === 'string') {
327 acc = acc.concat(this.grabAllErrors(value))
334 private updateForm () {
335 this.form.patchValue(this.customConfig)
338 private loadConfigAndUpdateForm () {
340 this.configService.getCustomConfig(),
341 this.customPage.getInstanceHomepage()
343 .subscribe(([ config, homepage ]) => {
344 this.customConfig = { ...config, instanceCustomHomepage: homepage }
347 // Force form validation
351 err => this.notifier.error(err.message)
355 private loadCategoriesAndLanguages () {
357 this.serverService.getVideoLanguages(),
358 this.serverService.getVideoCategories()
360 ([ languages, categories ]) => {
361 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
362 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
365 err => this.notifier.error(err.message)