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 } from '@app/shared/shared-forms'
23 import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
24 import { CustomConfig, ServerConfig } from '@shared/models'
25 import { SelectOptionsItem } from 'src/types/select-options-item.model'
28 selector: 'my-edit-custom-config',
29 templateUrl: './edit-custom-config.component.html',
30 styleUrls: [ './edit-custom-config.component.scss' ]
32 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
33 // FIXME: use built-in router
34 @ViewChild('nav') nav: NgbNav
37 customConfig: CustomConfig
39 resolutions: { id: string, label: string, description?: string }[] = []
40 liveResolutions: { id: string, label: string, description?: string }[] = []
42 transcodingThreadOptions: SelectOptionsItem[] = []
43 liveMaxDurationOptions: SelectOptionsItem[] = []
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 { id: 0, label: $localize`Auto (via ffmpeg)` },
101 { id: 1, label: '1' },
102 { id: 2, label: '2' },
103 { id: 4, label: '4' },
104 { id: 8, label: '8' },
105 { id: 12, label: '12' },
106 { id: 16, label: '16' },
107 { id: 32, label: '32' }
110 this.liveMaxDurationOptions = [
111 { id: -1, label: $localize`No limit` },
112 { id: 1000 * 3600, label: $localize`1 hour` },
113 { id: 1000 * 3600 * 3, label: $localize`3 hours` },
114 { id: 1000 * 3600 * 5, label: $localize`5 hours` },
115 { id: 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 const profiles = type === 'live'
138 ? this.serverConfig.live.transcoding.availableProfiles
139 : this.serverConfig.transcoding.availableProfiles
141 return profiles.map(p => ({ id: p, label: p }))
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
233 concurrency: CONCURRENCY_VALIDATOR,
251 email: ADMIN_EMAIL_VALIDATOR
257 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
258 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
262 threads: TRANSCODING_THREADS_VALIDATOR,
263 allowAdditionalExtensions: null,
264 allowAudioFiles: null,
266 concurrency: CONCURRENCY_VALIDATOR,
279 maxInstanceLives: null,
285 threads: TRANSCODING_THREADS_VALIDATOR,
310 indexUrl: INDEX_URL_VALIDATOR
327 url: SEARCH_INDEX_URL_VALIDATOR,
328 disableLocalSearch: null,
329 isDefaultSearch: null
334 const defaultValues = {
345 for (const resolution of this.resolutions) {
346 defaultValues.transcoding.resolutions[resolution.id] = 'false'
347 formGroupData.transcoding.resolutions[resolution.id] = null
350 for (const resolution of this.liveResolutions) {
351 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
352 formGroupData.live.transcoding.resolutions[resolution.id] = null
355 this.buildForm(formGroupData)
358 this.checkTranscodingFields()
359 this.checkSignupField()
362 ngAfterViewChecked () {
363 if (!this.initDone) {
369 isTranscodingEnabled () {
370 return this.form.value['transcoding']['enabled'] === true
374 return this.form.value['live']['enabled'] === true
377 isLiveTranscodingEnabled () {
378 return this.form.value['live']['transcoding']['enabled'] === true
382 return this.form.value['signup']['enabled'] === true
385 isSearchIndexEnabled () {
386 return this.form.value['search']['searchIndex']['enabled'] === true
389 isAutoFollowIndexEnabled () {
390 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
393 trendingVideosAlgorithmsEnabledIncludes (algorithm: string) {
394 return this.form.value['trending']['videos']['algorithms']['enabled'].find((e: string) => e === algorithm)
397 async formValidated () {
398 const value: CustomConfig = this.form.getRawValue()
400 this.configService.updateCustomConfig(value)
403 this.customConfig = res
405 // Reload general configuration
406 this.serverService.resetConfig()
410 this.notifier.success($localize`Configuration updated.`)
413 err => this.notifier.error(err.message)
419 'customizations': 'advanced-configuration'
421 const hash = window.location.hash.replace('#', '')
423 if (hash && Object.keys(hashToNav).includes(hash)) {
424 this.nav.select(hashToNav[hash])
425 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
429 hasConsistentOptions () {
430 if (this.hasLiveAllowReplayConsistentOptions()) return true
435 hasLiveAllowReplayConsistentOptions () {
436 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
443 private updateForm () {
444 this.form.patchValue(this.customConfig)
447 private loadForm () {
449 this.configService.getCustomConfig(),
450 this.serverService.getVideoLanguages(),
451 this.serverService.getVideoCategories()
453 ([ config, languages, categories ]) => {
454 this.customConfig = config
456 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
457 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
460 // Force form validation
464 err => this.notifier.error(err.message)
468 private checkTranscodingFields () {
469 const hlsControl = this.form.get('transcoding.hls.enabled')
470 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
472 webtorrentControl.valueChanges
473 .subscribe(newValue => {
474 if (newValue === false && !hlsControl.disabled) {
478 if (newValue === true && !hlsControl.enabled) {
483 hlsControl.valueChanges
484 .subscribe(newValue => {
485 if (newValue === false && !webtorrentControl.disabled) {
486 webtorrentControl.disable()
489 if (newValue === true && !webtorrentControl.enabled) {
490 webtorrentControl.enable()
495 private checkSignupField () {
496 const signupControl = this.form.get('signup.enabled')
498 signupControl.valueChanges
500 .subscribe(([ oldValue, newValue ]) => {
501 if (oldValue !== true && newValue === true) {
502 // tslint:disable:max-line-length
503 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
505 this.form.patchValue({