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 MAX_VIDEO_CHANNELS_PER_USER_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, FormValidatorService } 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 private router: Router,
56 private route: ActivatedRoute,
57 protected formValidatorService: FormValidatorService,
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 requiresEmailVerification: null,
136 minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR
140 concurrency: CONCURRENCY_VALIDATOR,
148 videoChannelSynchronization: {
161 email: ADMIN_EMAIL_VALIDATOR
167 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
168 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
171 maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR
175 threads: TRANSCODING_THREADS_VALIDATOR,
176 allowAdditionalExtensions: null,
177 allowAudioFiles: null,
179 concurrency: CONCURRENCY_VALIDATOR,
181 alwaysTranscodeOriginalResolution: null,
192 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
193 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
194 maxUserLives: MAX_USER_LIVES_VALIDATOR,
202 threads: TRANSCODING_THREADS_VALIDATOR,
205 alwaysTranscodeOriginalResolution: null
231 indexUrl: INDEX_URL_VALIDATOR
248 url: SEARCH_INDEX_URL_VALIDATOR,
249 disableLocalSearch: null,
250 isDefaultSearch: null
254 instanceCustomHomepage: {
259 const defaultValues = {
270 for (const resolution of this.editConfigurationService.getVODResolutions()) {
271 defaultValues.transcoding.resolutions[resolution.id] = 'false'
272 formGroupData.transcoding.resolutions[resolution.id] = null
275 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
276 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
277 formGroupData.live.transcoding.resolutions[resolution.id] = null
280 this.buildForm(formGroupData)
282 if (this.route.snapshot.fragment) {
283 this.onNavChange(this.route.snapshot.fragment)
286 this.loadConfigAndUpdateForm()
287 this.loadCategoriesAndLanguages()
289 if (!this.isUpdateAllowed()) {
295 const value: ComponentCustomConfig = this.form.getRawValue()
298 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
299 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
302 next: ([ resConfig ]) => {
303 const instanceCustomHomepage = {
304 content: value.instanceCustomHomepage.content
307 this.customConfig = { ...resConfig, instanceCustomHomepage }
309 // Reload general configuration
310 this.serverService.resetConfig()
311 .subscribe(config => {
312 this.serverConfig = config
317 this.notifier.success($localize`Configuration updated.`)
320 error: err => this.notifier.error(err.message)
325 return this.serverConfig.webadmin.configuration.edition.allowed === true
328 hasConsistentOptions () {
329 if (this.hasLiveAllowReplayConsistentOptions()) return true
334 hasLiveAllowReplayConsistentOptions () {
336 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
337 this.editConfigurationService.isLiveEnabled(this.form) &&
338 this.form.value['live']['allowReplay'] === true
346 onNavChange (newActiveNav: string) {
347 this.activeNav = newActiveNav
349 this.router.navigate([], { fragment: this.activeNav })
352 grabAllErrors (errorObjectArg?: any) {
353 const errorObject = errorObjectArg || this.formErrors
355 let acc: string[] = []
357 for (const key of Object.keys(errorObject)) {
358 const value = errorObject[key]
361 if (typeof value === 'string') {
364 acc = acc.concat(this.grabAllErrors(value))
371 private updateForm () {
372 this.form.patchValue(this.customConfig)
375 private loadConfigAndUpdateForm () {
377 this.configService.getCustomConfig(),
378 this.customPage.getInstanceHomepage()
380 next: ([ config, homepage ]) => {
381 this.customConfig = { ...config, instanceCustomHomepage: homepage }
384 // Force form validation
388 error: err => this.notifier.error(err.message)
392 private loadCategoriesAndLanguages () {
394 this.serverService.getVideoLanguages(),
395 this.serverService.getVideoCategories()
397 next: ([ languages, categories ]) => {
398 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
399 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
402 error: err => this.notifier.error(err.message)