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 }[] = []
40 concurrencyOptions: number[] = []
41 transcodingThreadOptions: { label: string, value: number }[] = []
42 liveMaxDurationOptions: { label: string, value: number }[] = []
44 vodTranscodingProfileOptions: string[] = []
45 liveTranscodingProfileOptions: string[] = []
47 languageItems: SelectOptionsItem[] = []
48 categoryItems: SelectOptionsItem[] = []
50 signupAlertMessage: string
52 private serverConfig: ServerConfig
55 private viewportScroller: ViewportScroller,
56 protected formValidatorService: FormValidatorService,
57 private notifier: Notifier,
58 private configService: ConfigService,
59 private serverService: ServerService
66 label: $localize`Audio-only`,
67 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
71 label: $localize`240p`
75 label: $localize`360p`
79 label: $localize`480p`
83 label: $localize`720p`
87 label: $localize`1080p`
91 label: $localize`1440p`
95 label: $localize`2160p`
99 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
101 this.transcodingThreadOptions = [
102 { value: 0, label: $localize`Auto (via ffmpeg)` },
103 { value: 1, label: '1' },
104 { value: 2, label: '2' },
105 { value: 4, label: '4' },
106 { value: 8, label: '8' }
108 this.concurrencyOptions = [ 1, 2, 3, 4, 5, 6 ]
110 this.vodTranscodingProfileOptions = [ 'default' ]
111 this.liveTranscodingProfileOptions = [ 'default' ]
113 this.liveMaxDurationOptions = [
114 { value: -1, label: $localize`No limit` },
115 { value: 1000 * 3600, label: $localize`1 hour` },
116 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
117 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
118 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
122 get videoQuotaOptions () {
123 return this.configService.videoQuotaOptions
126 get videoQuotaDailyOptions () {
127 return this.configService.videoQuotaDailyOptions
130 get availableThemes () {
131 return this.serverConfig.theme.registered
135 get liveRTMPPort () {
136 return this.serverConfig.live.rtmp.port
139 getAvailableTranscodingProfile (type: 'live' | 'vod') {
140 if (type === 'live') {
141 return this.serverConfig.live.transcoding.availableProfiles
144 return this.serverConfig.transcoding.availableProfiles
147 getTotalTranscodingThreads () {
148 const transcodingEnabled = this.form.value['transcoding']['enabled']
149 const transcodingThreads = this.form.value['transcoding']['threads']
150 const liveTranscodingEnabled = this.form.value['live']['transcoding']['enabled']
151 const liveTranscodingThreads = this.form.value['live']['transcoding']['threads']
153 // checks whether all enabled method are on fixed values and not on auto (= 0)
154 let noneOnAuto = !transcodingEnabled || +transcodingThreads > 0
155 noneOnAuto &&= !liveTranscodingEnabled || +liveTranscodingThreads > 0
157 // count total of fixed value, repalcing auto by a single thread (knowing it will display "at least")
159 if (transcodingEnabled) value += +transcodingThreads || 1
160 if (liveTranscodingEnabled) value += +liveTranscodingThreads || 1
164 atMost: noneOnAuto, // auto switches everything to a least estimation since ffmpeg will take as many threads as possible
171 getResolutionKey (resolution: string) {
172 return 'transcoding.resolutions.' + resolution
176 this.serverConfig = this.serverService.getTmpConfig()
177 this.serverService.getConfig()
178 .subscribe(config => {
179 this.serverConfig = config
182 const formGroupData: { [key in keyof CustomConfig ]: any } = {
184 name: INSTANCE_NAME_VALIDATOR,
185 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
189 defaultNSFWPolicy: null,
194 creationReason: null,
195 moderationInformation: null,
197 maintenanceLifetime: null,
200 hardwareInformation: null,
205 defaultClientRoute: null,
217 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
223 size: CACHE_PREVIEWS_SIZE_VALIDATOR
226 size: CACHE_CAPTIONS_SIZE_VALIDATOR
231 limit: SIGNUP_LIMIT_VALIDATOR,
232 requiresEmailVerification: null
236 concurrency: CONCURRENCY_VALIDATOR,
254 email: ADMIN_EMAIL_VALIDATOR
260 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
261 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
265 threads: TRANSCODING_THREADS_VALIDATOR,
266 allowAdditionalExtensions: null,
267 allowAudioFiles: null,
269 concurrency: CONCURRENCY_VALIDATOR,
282 maxInstanceLives: null,
288 threads: TRANSCODING_THREADS_VALIDATOR,
313 indexUrl: INDEX_URL_VALIDATOR
330 url: SEARCH_INDEX_URL_VALIDATOR,
331 disableLocalSearch: null,
332 isDefaultSearch: null
337 const defaultValues = {
348 for (const resolution of this.resolutions) {
349 defaultValues.transcoding.resolutions[resolution.id] = 'false'
350 formGroupData.transcoding.resolutions[resolution.id] = null
353 for (const resolution of this.liveResolutions) {
354 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
355 formGroupData.live.transcoding.resolutions[resolution.id] = null
358 this.buildForm(formGroupData)
361 this.checkTranscodingFields()
362 this.checkSignupField()
365 ngAfterViewChecked () {
366 if (!this.initDone) {
372 isTranscodingEnabled () {
373 return this.form.value['transcoding']['enabled'] === true
377 return this.form.value['live']['enabled'] === true
380 isLiveTranscodingEnabled () {
381 return this.form.value['live']['transcoding']['enabled'] === true
385 return this.form.value['signup']['enabled'] === true
388 isSearchIndexEnabled () {
389 return this.form.value['search']['searchIndex']['enabled'] === true
392 isAutoFollowIndexEnabled () {
393 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
396 trendingVideosAlgorithmsEnabledIncludes (algorithm: string) {
397 return this.form.value['trending']['videos']['algorithms']['enabled'].find((e: string) => e === algorithm)
400 async formValidated () {
401 const value: CustomConfig = this.form.getRawValue()
403 this.configService.updateCustomConfig(value)
406 this.customConfig = res
408 // Reload general configuration
409 this.serverService.resetConfig()
413 this.notifier.success($localize`Configuration updated.`)
416 err => this.notifier.error(err.message)
422 'customizations': 'advanced-configuration'
424 const hash = window.location.hash.replace('#', '')
426 if (hash && Object.keys(hashToNav).includes(hash)) {
427 this.nav.select(hashToNav[hash])
428 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
432 hasConsistentOptions () {
433 if (this.hasLiveAllowReplayConsistentOptions()) return true
438 hasLiveAllowReplayConsistentOptions () {
439 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
446 private updateForm () {
447 this.form.patchValue(this.customConfig)
450 private loadForm () {
452 this.configService.getCustomConfig(),
453 this.serverService.getVideoLanguages(),
454 this.serverService.getVideoCategories()
456 ([ config, languages, categories ]) => {
457 this.customConfig = config
459 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
460 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
463 // Force form validation
467 err => this.notifier.error(err.message)
471 private checkTranscodingFields () {
472 const hlsControl = this.form.get('transcoding.hls.enabled')
473 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
475 webtorrentControl.valueChanges
476 .subscribe(newValue => {
477 if (newValue === false && !hlsControl.disabled) {
481 if (newValue === true && !hlsControl.enabled) {
486 hlsControl.valueChanges
487 .subscribe(newValue => {
488 if (newValue === false && !webtorrentControl.disabled) {
489 webtorrentControl.disable()
492 if (newValue === true && !webtorrentControl.enabled) {
493 webtorrentControl.enable()
498 private checkSignupField () {
499 const signupControl = this.form.get('signup.enabled')
501 signupControl.valueChanges
503 .subscribe(([ oldValue, newValue ]) => {
504 if (oldValue !== true && newValue === true) {
505 // tslint:disable:max-line-length
506 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
508 this.form.patchValue({