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()
262 if (!this.isUpdateAllowed()) {
268 const value: ComponentCustomConfig = this.form.getRawValue()
271 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
272 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
275 next: ([ resConfig ]) => {
276 const instanceCustomHomepage = {
277 content: value.instanceCustomHomepage.content
280 this.customConfig = { ...resConfig, instanceCustomHomepage }
282 // Reload general configuration
283 this.serverService.resetConfig()
284 .subscribe(config => {
285 this.serverConfig = config
290 this.notifier.success($localize`Configuration updated.`)
293 error: err => this.notifier.error(err.message)
298 return this.serverConfig.webadmin.configuration.edition.allowed === true
301 hasConsistentOptions () {
302 if (this.hasLiveAllowReplayConsistentOptions()) return true
307 hasLiveAllowReplayConsistentOptions () {
309 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
310 this.editConfigurationService.isLiveEnabled(this.form) &&
311 this.form.value['live']['allowReplay'] === true
319 onNavChange (newActiveNav: string) {
320 this.activeNav = newActiveNav
322 this.router.navigate([], { fragment: this.activeNav })
325 grabAllErrors (errorObjectArg?: any) {
326 const errorObject = errorObjectArg || this.formErrors
328 let acc: string[] = []
330 for (const key of Object.keys(errorObject)) {
331 const value = errorObject[key]
334 if (typeof value === 'string') {
337 acc = acc.concat(this.grabAllErrors(value))
344 private updateForm () {
345 this.form.patchValue(this.customConfig)
348 private loadConfigAndUpdateForm () {
350 this.configService.getCustomConfig(),
351 this.customPage.getInstanceHomepage()
353 next: ([ config, homepage ]) => {
354 this.customConfig = { ...config, instanceCustomHomepage: homepage }
357 // Force form validation
361 error: err => this.notifier.error(err.message)
365 private loadCategoriesAndLanguages () {
367 this.serverService.getVideoLanguages(),
368 this.serverService.getVideoCategories()
370 next: ([ languages, categories ]) => {
371 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
372 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
375 error: err => this.notifier.error(err.message)