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 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'
34 type ComponentCustomConfig = CustomConfig & {
35 instanceCustomHomepage: CustomPage
39 selector: 'my-edit-custom-config',
40 templateUrl: './edit-custom-config.component.html',
41 styleUrls: [ './edit-custom-config.component.scss' ]
43 export class EditCustomConfigComponent extends FormReactive implements OnInit {
46 customConfig: ComponentCustomConfig
47 serverConfig: HTMLServerConfig
51 languageItems: SelectOptionsItem[] = []
52 categoryItems: SelectOptionsItem[] = []
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
68 this.serverConfig = this.serverService.getHTMLConfig()
70 const formGroupData: { [key in keyof ComponentCustomConfig ]: any } = {
72 name: INSTANCE_NAME_VALIDATOR,
73 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
77 defaultNSFWPolicy: null,
83 moderationInformation: null,
85 maintenanceLifetime: null,
88 hardwareInformation: null,
93 defaultClientRoute: null,
105 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
112 preferAuthorDisplayName: null
117 redirectOnSingleExternalAuth: null
123 size: CACHE_PREVIEWS_SIZE_VALIDATOR
126 size: CACHE_CAPTIONS_SIZE_VALIDATOR
129 size: CACHE_CAPTIONS_SIZE_VALIDATOR
134 limit: SIGNUP_LIMIT_VALIDATOR,
135 requiresApproval: null,
136 requiresEmailVerification: null,
137 minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR
141 concurrency: CONCURRENCY_VALIDATOR,
149 videoChannelSynchronization: {
162 email: ADMIN_EMAIL_VALIDATOR
173 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
174 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
177 maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR
181 threads: TRANSCODING_THREADS_VALIDATOR,
182 allowAdditionalExtensions: null,
183 allowAudioFiles: null,
185 concurrency: CONCURRENCY_VALIDATOR,
187 alwaysTranscodeOriginalResolution: null,
201 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
202 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
203 maxUserLives: MAX_USER_LIVES_VALIDATOR,
211 threads: TRANSCODING_THREADS_VALIDATOR,
214 alwaysTranscodeOriginalResolution: null,
246 indexUrl: INDEX_URL_VALIDATOR
263 url: SEARCH_INDEX_URL_VALIDATOR,
264 disableLocalSearch: null,
265 isDefaultSearch: null
269 instanceCustomHomepage: {
274 const defaultValues = {
276 resolutions: {} as { [id: string]: string }
280 resolutions: {} as { [id: string]: string }
285 for (const resolution of this.editConfigurationService.getVODResolutions()) {
286 defaultValues.transcoding.resolutions[resolution.id] = 'false'
287 formGroupData.transcoding.resolutions[resolution.id] = null
290 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
291 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
292 formGroupData.live.transcoding.resolutions[resolution.id] = null
295 this.buildForm(formGroupData)
297 if (this.route.snapshot.fragment) {
298 this.onNavChange(this.route.snapshot.fragment)
301 this.loadConfigAndUpdateForm()
302 this.loadCategoriesAndLanguages()
304 if (!this.isUpdateAllowed()) {
311 if (!this.form.valid) return
313 const value: ComponentCustomConfig = this.form.getRawValue()
316 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
317 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
320 next: ([ resConfig ]) => {
321 const instanceCustomHomepage = {
322 content: value.instanceCustomHomepage.content
325 this.customConfig = { ...resConfig, instanceCustomHomepage }
327 // Reload general configuration
328 this.serverService.resetConfig()
329 .subscribe(config => {
330 this.serverConfig = config
335 this.notifier.success($localize`Configuration updated.`)
338 error: err => this.notifier.error(err.message)
343 return this.serverConfig.webadmin.configuration.edition.allowed === true
346 hasConsistentOptions () {
347 if (this.hasLiveAllowReplayConsistentOptions()) return true
352 hasLiveAllowReplayConsistentOptions () {
354 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
355 this.editConfigurationService.isLiveEnabled(this.form) &&
356 this.form.value['live']['allowReplay'] === true
364 onNavChange (newActiveNav: string) {
365 this.activeNav = newActiveNav
367 this.router.navigate([], { fragment: this.activeNav })
370 grabAllErrors (errorObjectArg?: any) {
371 const errorObject = errorObjectArg || this.formErrors
373 let acc: string[] = []
375 for (const key of Object.keys(errorObject)) {
376 const value = errorObject[key]
379 if (typeof value === 'string') {
382 acc = acc.concat(this.grabAllErrors(value))
389 private updateForm () {
390 this.form.patchValue(this.customConfig)
393 private loadConfigAndUpdateForm () {
395 this.configService.getCustomConfig(),
396 this.customPage.getInstanceHomepage()
398 next: ([ config, homepage ]) => {
399 this.customConfig = { ...config, instanceCustomHomepage: homepage }
402 this.markAllAsDirty()
405 error: err => this.notifier.error(err.message)
409 private loadCategoriesAndLanguages () {
411 this.serverService.getVideoLanguages(),
412 this.serverService.getVideoCategories()
414 next: ([ languages, categories ]) => {
415 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
416 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
419 error: err => this.notifier.error(err.message)