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,
111 size: CACHE_PREVIEWS_SIZE_VALIDATOR
114 size: CACHE_CAPTIONS_SIZE_VALIDATOR
117 size: CACHE_CAPTIONS_SIZE_VALIDATOR
122 limit: SIGNUP_LIMIT_VALIDATOR,
123 requiresEmailVerification: null,
124 minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR
128 concurrency: CONCURRENCY_VALIDATOR,
146 email: ADMIN_EMAIL_VALIDATOR
152 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
153 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
156 maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR
160 threads: TRANSCODING_THREADS_VALIDATOR,
161 allowAdditionalExtensions: null,
162 allowAudioFiles: null,
164 concurrency: CONCURRENCY_VALIDATOR,
176 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
177 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
178 maxUserLives: MAX_USER_LIVES_VALIDATOR,
183 threads: TRANSCODING_THREADS_VALIDATOR,
208 indexUrl: INDEX_URL_VALIDATOR
225 url: SEARCH_INDEX_URL_VALIDATOR,
226 disableLocalSearch: null,
227 isDefaultSearch: null
231 instanceCustomHomepage: {
236 const defaultValues = {
247 for (const resolution of this.editConfigurationService.getVODResolutions()) {
248 defaultValues.transcoding.resolutions[resolution.id] = 'false'
249 formGroupData.transcoding.resolutions[resolution.id] = null
252 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
253 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
254 formGroupData.live.transcoding.resolutions[resolution.id] = null
257 this.buildForm(formGroupData)
259 if (this.route.snapshot.fragment) {
260 this.onNavChange(this.route.snapshot.fragment)
263 this.loadConfigAndUpdateForm()
264 this.loadCategoriesAndLanguages()
266 if (!this.isUpdateAllowed()) {
272 const value: ComponentCustomConfig = this.form.getRawValue()
275 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
276 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
279 next: ([ resConfig ]) => {
280 const instanceCustomHomepage = {
281 content: value.instanceCustomHomepage.content
284 this.customConfig = { ...resConfig, instanceCustomHomepage }
286 // Reload general configuration
287 this.serverService.resetConfig()
288 .subscribe(config => {
289 this.serverConfig = config
294 this.notifier.success($localize`Configuration updated.`)
297 error: err => this.notifier.error(err.message)
302 return this.serverConfig.webadmin.configuration.edition.allowed === true
305 hasConsistentOptions () {
306 if (this.hasLiveAllowReplayConsistentOptions()) return true
311 hasLiveAllowReplayConsistentOptions () {
313 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
314 this.editConfigurationService.isLiveEnabled(this.form) &&
315 this.form.value['live']['allowReplay'] === true
323 onNavChange (newActiveNav: string) {
324 this.activeNav = newActiveNav
326 this.router.navigate([], { fragment: this.activeNav })
329 grabAllErrors (errorObjectArg?: any) {
330 const errorObject = errorObjectArg || this.formErrors
332 let acc: string[] = []
334 for (const key of Object.keys(errorObject)) {
335 const value = errorObject[key]
338 if (typeof value === 'string') {
341 acc = acc.concat(this.grabAllErrors(value))
348 private updateForm () {
349 this.form.patchValue(this.customConfig)
352 private loadConfigAndUpdateForm () {
354 this.configService.getCustomConfig(),
355 this.customPage.getInstanceHomepage()
357 next: ([ config, homepage ]) => {
358 this.customConfig = { ...config, instanceCustomHomepage: homepage }
361 // Force form validation
365 error: err => this.notifier.error(err.message)
369 private loadCategoriesAndLanguages () {
371 this.serverService.getVideoLanguages(),
372 this.serverService.getVideoCategories()
374 next: ([ languages, categories ]) => {
375 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
376 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
379 error: err => this.notifier.error(err.message)