1 import { forkJoin } from 'rxjs'
2 import { ViewportScroller } from '@angular/common'
3 import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { Notifier } from '@app/core'
6 import { ServerService } from '@app/core/server/server.service'
9 CACHE_CAPTIONS_SIZE_VALIDATOR,
10 CACHE_PREVIEWS_SIZE_VALIDATOR,
12 INSTANCE_NAME_VALIDATOR,
13 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
14 SEARCH_INDEX_URL_VALIDATOR,
15 SERVICES_TWITTER_USERNAME_VALIDATOR,
16 SIGNUP_LIMIT_VALIDATOR,
17 TRANSCODING_THREADS_VALIDATOR
18 } from '@app/shared/form-validators/custom-config-validators'
19 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
20 import { FormReactive, FormValidatorService, SelectOptionsItem } from '@app/shared/shared-forms'
21 import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
22 import { CustomConfig, ServerConfig } from '@shared/models'
25 selector: 'my-edit-custom-config',
26 templateUrl: './edit-custom-config.component.html',
27 styleUrls: [ './edit-custom-config.component.scss' ]
29 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
30 // FIXME: use built-in router
31 @ViewChild('nav') nav: NgbNav
34 customConfig: CustomConfig
36 resolutions: { id: string, label: string, description?: string }[] = []
37 liveResolutions: { id: string, label: string, description?: string }[] = []
38 transcodingThreadOptions: { label: string, value: number }[] = []
39 liveMaxDurationOptions: { label: string, value: number }[] = []
41 languageItems: SelectOptionsItem[] = []
42 categoryItems: SelectOptionsItem[] = []
44 private serverConfig: ServerConfig
47 private viewportScroller: ViewportScroller,
48 protected formValidatorService: FormValidatorService,
49 private notifier: Notifier,
50 private configService: ConfigService,
51 private serverService: ServerService
58 label: $localize`Audio-only`,
59 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
63 label: $localize`240p`
67 label: $localize`360p`
71 label: $localize`480p`
75 label: $localize`720p`
79 label: $localize`1080p`
83 label: $localize`2160p`
87 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
89 this.transcodingThreadOptions = [
90 { value: 0, label: $localize`Auto (via ffmpeg)` },
91 { value: 1, label: '1' },
92 { value: 2, label: '2' },
93 { value: 4, label: '4' },
94 { value: 8, label: '8' }
97 this.liveMaxDurationOptions = [
98 { value: 0, label: $localize`No limit` },
99 { value: 1000 * 3600, label: $localize`1 hour` },
100 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
101 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
102 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
106 get videoQuotaOptions () {
107 return this.configService.videoQuotaOptions
110 get videoQuotaDailyOptions () {
111 return this.configService.videoQuotaDailyOptions
114 get availableThemes () {
115 return this.serverConfig.theme.registered
119 getResolutionKey (resolution: string) {
120 return 'transcoding.resolutions.' + resolution
124 this.serverConfig = this.serverService.getTmpConfig()
125 this.serverService.getConfig()
126 .subscribe(config => {
127 this.serverConfig = config
130 const formGroupData: { [key in keyof CustomConfig ]: any } = {
132 name: INSTANCE_NAME_VALIDATOR,
133 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
137 defaultNSFWPolicy: null,
142 creationReason: null,
143 moderationInformation: null,
145 maintenanceLifetime: null,
148 hardwareInformation: null,
153 defaultClientRoute: null,
165 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
171 size: CACHE_PREVIEWS_SIZE_VALIDATOR
174 size: CACHE_CAPTIONS_SIZE_VALIDATOR
179 limit: SIGNUP_LIMIT_VALIDATOR,
180 requiresEmailVerification: null
193 email: ADMIN_EMAIL_VALIDATOR
199 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
200 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
204 threads: TRANSCODING_THREADS_VALIDATOR,
205 allowAdditionalExtensions: null,
206 allowAudioFiles: null,
219 maxInstanceLives: null,
225 threads: TRANSCODING_THREADS_VALIDATOR,
249 indexUrl: INDEX_URL_VALIDATOR
266 url: SEARCH_INDEX_URL_VALIDATOR,
267 disableLocalSearch: null,
268 isDefaultSearch: null
273 const defaultValues = {
284 for (const resolution of this.resolutions) {
285 defaultValues.transcoding.resolutions[resolution.id] = 'false'
286 formGroupData.transcoding.resolutions[resolution.id] = null
289 for (const resolution of this.liveResolutions) {
290 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
291 formGroupData.live.transcoding.resolutions[resolution.id] = null
294 this.buildForm(formGroupData)
296 this.checkTranscodingFields()
299 ngAfterViewChecked () {
300 if (!this.initDone) {
306 isTranscodingEnabled () {
307 return this.form.value['transcoding']['enabled'] === true
311 return this.form.value['live']['enabled'] === true
314 isLiveTranscodingEnabled () {
315 return this.form.value['live']['transcoding']['enabled'] === true
319 return this.form.value['signup']['enabled'] === true
322 isSearchIndexEnabled () {
323 return this.form.value['search']['searchIndex']['enabled'] === true
326 isAutoFollowIndexEnabled () {
327 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
330 async formValidated () {
331 this.configService.updateCustomConfig(this.form.getRawValue())
334 this.customConfig = res
336 // Reload general configuration
337 this.serverService.resetConfig()
341 this.notifier.success($localize`Configuration updated.`)
344 err => this.notifier.error(err.message)
350 'customizations': 'advanced-configuration'
352 const hash = window.location.hash.replace('#', '')
354 if (hash && Object.keys(hashToNav).includes(hash)) {
355 this.nav.select(hashToNav[hash])
356 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
360 hasConsistentOptions () {
361 if (this.hasLiveAllowReplayConsistentOptions()) return true
366 hasLiveAllowReplayConsistentOptions () {
367 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
374 private updateForm () {
375 this.form.patchValue(this.customConfig)
378 private loadForm () {
380 this.configService.getCustomConfig(),
381 this.serverService.getVideoLanguages(),
382 this.serverService.getVideoCategories()
384 ([ config, languages, categories ]) => {
385 this.customConfig = config
387 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
388 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
391 // Force form validation
395 err => this.notifier.error(err.message)
399 private checkTranscodingFields () {
400 const hlsControl = this.form.get('transcoding.hls.enabled')
401 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
403 webtorrentControl.valueChanges
404 .subscribe(newValue => {
405 if (newValue === false && !hlsControl.disabled) {
409 if (newValue === true && !hlsControl.enabled) {
414 hlsControl.valueChanges
415 .subscribe(newValue => {
416 if (newValue === false && !webtorrentControl.disabled) {
417 webtorrentControl.disable()
420 if (newValue === true && !webtorrentControl.enabled) {
421 webtorrentControl.enable()