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,
12 CONCURRENCY_VALIDATOR,
14 INSTANCE_NAME_VALIDATOR,
15 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
16 SEARCH_INDEX_URL_VALIDATOR,
17 SERVICES_TWITTER_USERNAME_VALIDATOR,
18 SIGNUP_LIMIT_VALIDATOR,
19 TRANSCODING_THREADS_VALIDATOR
20 } from '@app/shared/form-validators/custom-config-validators'
21 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
22 import { FormReactive, FormValidatorService, SelectOptionsItem } from '@app/shared/shared-forms'
23 import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
24 import { CustomConfig, ServerConfig } from '@shared/models'
27 selector: 'my-edit-custom-config',
28 templateUrl: './edit-custom-config.component.html',
29 styleUrls: [ './edit-custom-config.component.scss' ]
31 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
32 // FIXME: use built-in router
33 @ViewChild('nav') nav: NgbNav
36 customConfig: CustomConfig
38 resolutions: { id: string, label: string, description?: string }[] = []
39 liveResolutions: { id: string, label: string, description?: string }[] = []
41 transcodingThreadOptions: SelectOptionsItem[] = []
42 liveMaxDurationOptions: SelectOptionsItem[] = []
44 languageItems: SelectOptionsItem[] = []
45 categoryItems: SelectOptionsItem[] = []
47 signupAlertMessage: string
49 private serverConfig: ServerConfig
52 private viewportScroller: ViewportScroller,
53 protected formValidatorService: FormValidatorService,
54 private notifier: Notifier,
55 private configService: ConfigService,
56 private serverService: ServerService
63 label: $localize`Audio-only`,
64 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
68 label: $localize`240p`
72 label: $localize`360p`
76 label: $localize`480p`
80 label: $localize`720p`
84 label: $localize`1080p`
88 label: $localize`1440p`
92 label: $localize`2160p`
96 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
98 this.transcodingThreadOptions = [
99 { id: 0, label: $localize`Auto (via ffmpeg)` },
100 { id: 1, label: '1' },
101 { id: 2, label: '2' },
102 { id: 4, label: '4' },
103 { id: 8, label: '8' },
104 { id: 12, label: '12' },
105 { id: 16, label: '16' },
106 { id: 32, label: '32' }
109 this.liveMaxDurationOptions = [
110 { id: -1, label: $localize`No limit` },
111 { id: 1000 * 3600, label: $localize`1 hour` },
112 { id: 1000 * 3600 * 3, label: $localize`3 hours` },
113 { id: 1000 * 3600 * 5, label: $localize`5 hours` },
114 { id: 1000 * 3600 * 10, label: $localize`10 hours` }
118 get videoQuotaOptions () {
119 return this.configService.videoQuotaOptions
122 get videoQuotaDailyOptions () {
123 return this.configService.videoQuotaDailyOptions
126 get availableThemes () {
127 return this.serverConfig.theme.registered
131 get liveRTMPPort () {
132 return this.serverConfig.live.rtmp.port
135 getAvailableTranscodingProfile (type: 'live' | 'vod') {
136 const profiles = type === 'live'
137 ? this.serverConfig.live.transcoding.availableProfiles
138 : this.serverConfig.transcoding.availableProfiles
140 return profiles.map(p => ({ id: p, label: p }))
143 getTotalTranscodingThreads () {
144 const transcodingEnabled = this.form.value['transcoding']['enabled']
145 const transcodingThreads = this.form.value['transcoding']['threads']
146 const liveTranscodingEnabled = this.form.value['live']['transcoding']['enabled']
147 const liveTranscodingThreads = this.form.value['live']['transcoding']['threads']
149 // checks whether all enabled method are on fixed values and not on auto (= 0)
150 let noneOnAuto = !transcodingEnabled || +transcodingThreads > 0
151 noneOnAuto &&= !liveTranscodingEnabled || +liveTranscodingThreads > 0
153 // count total of fixed value, repalcing auto by a single thread (knowing it will display "at least")
155 if (transcodingEnabled) value += +transcodingThreads || 1
156 if (liveTranscodingEnabled) value += +liveTranscodingThreads || 1
160 atMost: noneOnAuto, // auto switches everything to a least estimation since ffmpeg will take as many threads as possible
167 getResolutionKey (resolution: string) {
168 return 'transcoding.resolutions.' + resolution
172 this.serverConfig = this.serverService.getTmpConfig()
173 this.serverService.getConfig()
174 .subscribe(config => {
175 this.serverConfig = config
178 const formGroupData: { [key in keyof CustomConfig ]: any } = {
180 name: INSTANCE_NAME_VALIDATOR,
181 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
185 defaultNSFWPolicy: null,
190 creationReason: null,
191 moderationInformation: null,
193 maintenanceLifetime: null,
196 hardwareInformation: null,
201 defaultClientRoute: null,
213 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
219 size: CACHE_PREVIEWS_SIZE_VALIDATOR
222 size: CACHE_CAPTIONS_SIZE_VALIDATOR
227 limit: SIGNUP_LIMIT_VALIDATOR,
228 requiresEmailVerification: null
232 concurrency: CONCURRENCY_VALIDATOR,
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,
265 concurrency: CONCURRENCY_VALIDATOR,
278 maxInstanceLives: null,
284 threads: TRANSCODING_THREADS_VALIDATOR,
309 indexUrl: INDEX_URL_VALIDATOR
326 url: SEARCH_INDEX_URL_VALIDATOR,
327 disableLocalSearch: null,
328 isDefaultSearch: null
333 const defaultValues = {
344 for (const resolution of this.resolutions) {
345 defaultValues.transcoding.resolutions[resolution.id] = 'false'
346 formGroupData.transcoding.resolutions[resolution.id] = null
349 for (const resolution of this.liveResolutions) {
350 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
351 formGroupData.live.transcoding.resolutions[resolution.id] = null
354 this.buildForm(formGroupData)
357 this.checkTranscodingFields()
358 this.checkSignupField()
361 ngAfterViewChecked () {
362 if (!this.initDone) {
368 isTranscodingEnabled () {
369 return this.form.value['transcoding']['enabled'] === true
373 return this.form.value['live']['enabled'] === true
376 isLiveTranscodingEnabled () {
377 return this.form.value['live']['transcoding']['enabled'] === true
381 return this.form.value['signup']['enabled'] === true
384 isSearchIndexEnabled () {
385 return this.form.value['search']['searchIndex']['enabled'] === true
388 isAutoFollowIndexEnabled () {
389 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
392 trendingVideosAlgorithmsEnabledIncludes (algorithm: string) {
393 return this.form.value['trending']['videos']['algorithms']['enabled'].find((e: string) => e === algorithm)
396 async formValidated () {
397 const value: CustomConfig = this.form.getRawValue()
399 this.configService.updateCustomConfig(value)
402 this.customConfig = res
404 // Reload general configuration
405 this.serverService.resetConfig()
409 this.notifier.success($localize`Configuration updated.`)
412 err => this.notifier.error(err.message)
418 'customizations': 'advanced-configuration'
420 const hash = window.location.hash.replace('#', '')
422 if (hash && Object.keys(hashToNav).includes(hash)) {
423 this.nav.select(hashToNav[hash])
424 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
428 hasConsistentOptions () {
429 if (this.hasLiveAllowReplayConsistentOptions()) return true
434 hasLiveAllowReplayConsistentOptions () {
435 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
442 private updateForm () {
443 this.form.patchValue(this.customConfig)
446 private loadForm () {
448 this.configService.getCustomConfig(),
449 this.serverService.getVideoLanguages(),
450 this.serverService.getVideoCategories()
452 ([ config, languages, categories ]) => {
453 this.customConfig = config
455 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
456 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
459 // Force form validation
463 err => this.notifier.error(err.message)
467 private checkTranscodingFields () {
468 const hlsControl = this.form.get('transcoding.hls.enabled')
469 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
471 webtorrentControl.valueChanges
472 .subscribe(newValue => {
473 if (newValue === false && !hlsControl.disabled) {
477 if (newValue === true && !hlsControl.enabled) {
482 hlsControl.valueChanges
483 .subscribe(newValue => {
484 if (newValue === false && !webtorrentControl.disabled) {
485 webtorrentControl.disable()
488 if (newValue === true && !webtorrentControl.enabled) {
489 webtorrentControl.enable()
494 private checkSignupField () {
495 const signupControl = this.form.get('signup.enabled')
497 signupControl.valueChanges
499 .subscribe(([ oldValue, newValue ]) => {
500 if (oldValue !== true && newValue === true) {
501 // tslint:disable:max-line-length
502 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
504 this.form.patchValue({