1 import { forkJoin } from 'rxjs'
2 import { pairwise } from 'rxjs/operators'
3 import { ViewportScroller } from '@angular/common'
4 import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core'
5 import { ConfigService } from '@app/+admin/config/shared/config.service'
6 import { Notifier } from '@app/core'
7 import { ServerService } from '@app/core/server/server.service'
10 CACHE_CAPTIONS_SIZE_VALIDATOR,
11 CACHE_PREVIEWS_SIZE_VALIDATOR,
13 INSTANCE_NAME_VALIDATOR,
14 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
15 SEARCH_INDEX_URL_VALIDATOR,
16 SERVICES_TWITTER_USERNAME_VALIDATOR,
17 SIGNUP_LIMIT_VALIDATOR,
18 TRANSCODING_THREADS_VALIDATOR
19 } from '@app/shared/form-validators/custom-config-validators'
20 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
21 import { FormReactive, FormValidatorService, SelectOptionsItem } from '@app/shared/shared-forms'
22 import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
23 import { CustomConfig, ServerConfig } from '@shared/models'
26 selector: 'my-edit-custom-config',
27 templateUrl: './edit-custom-config.component.html',
28 styleUrls: [ './edit-custom-config.component.scss' ]
30 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
31 // FIXME: use built-in router
32 @ViewChild('nav') nav: NgbNav
35 customConfig: CustomConfig
37 resolutions: { id: string, label: string, description?: string }[] = []
38 liveResolutions: { id: string, label: string, description?: string }[] = []
39 transcodingThreadOptions: { label: string, value: number }[] = []
40 liveMaxDurationOptions: { label: string, value: number }[] = []
42 vodTranscodingProfileOptions: string[] = []
43 liveTranscodingProfileOptions: string[] = []
45 languageItems: SelectOptionsItem[] = []
46 categoryItems: SelectOptionsItem[] = []
48 signupAlertMessage: string
50 private serverConfig: ServerConfig
53 private viewportScroller: ViewportScroller,
54 protected formValidatorService: FormValidatorService,
55 private notifier: Notifier,
56 private configService: ConfigService,
57 private serverService: ServerService
64 label: $localize`Audio-only`,
65 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
69 label: $localize`240p`
73 label: $localize`360p`
77 label: $localize`480p`
81 label: $localize`720p`
85 label: $localize`1080p`
89 label: $localize`1440p`
93 label: $localize`2160p`
97 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
99 this.transcodingThreadOptions = [
100 { value: 0, label: $localize`Auto (via ffmpeg)` },
101 { value: 1, label: '1' },
102 { value: 2, label: '2' },
103 { value: 4, label: '4' },
104 { value: 8, label: '8' }
107 this.vodTranscodingProfileOptions = [ 'default' ]
108 this.liveTranscodingProfileOptions = [ 'default' ]
110 this.liveMaxDurationOptions = [
111 { value: -1, label: $localize`No limit` },
112 { value: 1000 * 3600, label: $localize`1 hour` },
113 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
114 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
115 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
119 get videoQuotaOptions () {
120 return this.configService.videoQuotaOptions
123 get videoQuotaDailyOptions () {
124 return this.configService.videoQuotaDailyOptions
127 get availableThemes () {
128 return this.serverConfig.theme.registered
132 get liveRTMPPort () {
133 return this.serverConfig.live.rtmp.port
136 getAvailableTranscodingProfile (type: 'live' | 'vod') {
137 if (type === 'live') {
138 return this.serverConfig.live.transcoding.availableProfiles
141 return this.serverConfig.transcoding.availableProfiles
144 getTotalTranscodingThreads () {
145 const transcodingEnabled = this.form.value['transcoding']['enabled']
146 const transcodingThreads = this.form.value['transcoding']['threads']
147 const liveTranscodingEnabled = this.form.value['live']['transcoding']['enabled']
148 const liveTranscodingThreads = this.form.value['live']['transcoding']['threads']
150 // checks whether all enabled method are on fixed values and not on auto (= 0)
151 let noneOnAuto = !transcodingEnabled || +transcodingThreads > 0
152 noneOnAuto &&= !liveTranscodingEnabled || +liveTranscodingThreads > 0
154 // count total of fixed value, repalcing auto by a single thread (knowing it will display "at least")
156 if (transcodingEnabled) value += +transcodingThreads || 1
157 if (liveTranscodingEnabled) value += +liveTranscodingThreads || 1
161 atMost: noneOnAuto, // auto switches everything to a least estimation since ffmpeg will take as many threads as possible
168 getResolutionKey (resolution: string) {
169 return 'transcoding.resolutions.' + resolution
173 this.serverConfig = this.serverService.getTmpConfig()
174 this.serverService.getConfig()
175 .subscribe(config => {
176 this.serverConfig = config
179 const formGroupData: { [key in keyof CustomConfig ]: any } = {
181 name: INSTANCE_NAME_VALIDATOR,
182 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
186 defaultNSFWPolicy: null,
191 creationReason: null,
192 moderationInformation: null,
194 maintenanceLifetime: null,
197 hardwareInformation: null,
202 defaultClientRoute: null,
214 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
220 size: CACHE_PREVIEWS_SIZE_VALIDATOR
223 size: CACHE_CAPTIONS_SIZE_VALIDATOR
228 limit: SIGNUP_LIMIT_VALIDATOR,
229 requiresEmailVerification: null
250 email: ADMIN_EMAIL_VALIDATOR
256 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
257 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
261 threads: TRANSCODING_THREADS_VALIDATOR,
262 allowAdditionalExtensions: null,
263 allowAudioFiles: null,
277 maxInstanceLives: null,
283 threads: TRANSCODING_THREADS_VALIDATOR,
308 indexUrl: INDEX_URL_VALIDATOR
325 url: SEARCH_INDEX_URL_VALIDATOR,
326 disableLocalSearch: null,
327 isDefaultSearch: null
332 const defaultValues = {
343 for (const resolution of this.resolutions) {
344 defaultValues.transcoding.resolutions[resolution.id] = 'false'
345 formGroupData.transcoding.resolutions[resolution.id] = null
348 for (const resolution of this.liveResolutions) {
349 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
350 formGroupData.live.transcoding.resolutions[resolution.id] = null
353 this.buildForm(formGroupData)
356 this.checkTranscodingFields()
357 this.checkSignupField()
360 ngAfterViewChecked () {
361 if (!this.initDone) {
367 isTranscodingEnabled () {
368 return this.form.value['transcoding']['enabled'] === true
372 return this.form.value['live']['enabled'] === true
375 isLiveTranscodingEnabled () {
376 return this.form.value['live']['transcoding']['enabled'] === true
380 return this.form.value['signup']['enabled'] === true
383 isSearchIndexEnabled () {
384 return this.form.value['search']['searchIndex']['enabled'] === true
387 isAutoFollowIndexEnabled () {
388 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
391 trendingVideosAlgorithmsEnabledIncludes (algorithm: string) {
392 return this.form.value['trending']['videos']['algorithms']['enabled'].find((e: string) => e === algorithm)
395 async formValidated () {
396 const value: CustomConfig = this.form.getRawValue()
398 this.configService.updateCustomConfig(value)
401 this.customConfig = res
403 // Reload general configuration
404 this.serverService.resetConfig()
408 this.notifier.success($localize`Configuration updated.`)
411 err => this.notifier.error(err.message)
417 'customizations': 'advanced-configuration'
419 const hash = window.location.hash.replace('#', '')
421 if (hash && Object.keys(hashToNav).includes(hash)) {
422 this.nav.select(hashToNav[hash])
423 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
427 hasConsistentOptions () {
428 if (this.hasLiveAllowReplayConsistentOptions()) return true
433 hasLiveAllowReplayConsistentOptions () {
434 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
441 private updateForm () {
442 this.form.patchValue(this.customConfig)
445 private loadForm () {
447 this.configService.getCustomConfig(),
448 this.serverService.getVideoLanguages(),
449 this.serverService.getVideoCategories()
451 ([ config, languages, categories ]) => {
452 this.customConfig = config
454 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
455 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
458 // Force form validation
462 err => this.notifier.error(err.message)
466 private checkTranscodingFields () {
467 const hlsControl = this.form.get('transcoding.hls.enabled')
468 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
470 webtorrentControl.valueChanges
471 .subscribe(newValue => {
472 if (newValue === false && !hlsControl.disabled) {
476 if (newValue === true && !hlsControl.enabled) {
481 hlsControl.valueChanges
482 .subscribe(newValue => {
483 if (newValue === false && !webtorrentControl.disabled) {
484 webtorrentControl.disable()
487 if (newValue === true && !webtorrentControl.enabled) {
488 webtorrentControl.enable()
493 private checkSignupField () {
494 const signupControl = this.form.get('signup.enabled')
496 signupControl.valueChanges
498 .subscribe(([ oldValue, newValue ]) => {
499 if (oldValue !== true && newValue === true) {
500 // tslint:disable:max-line-length
501 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
503 this.form.patchValue({