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'
23 import { pairwise } from 'rxjs/operators'
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 languageItems: SelectOptionsItem[] = []
43 categoryItems: SelectOptionsItem[] = []
45 signupAlertMessage: string
47 private serverConfig: ServerConfig
50 private viewportScroller: ViewportScroller,
51 protected formValidatorService: FormValidatorService,
52 private notifier: Notifier,
53 private configService: ConfigService,
54 private serverService: ServerService
61 label: $localize`Audio-only`,
62 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
66 label: $localize`240p`
70 label: $localize`360p`
74 label: $localize`480p`
78 label: $localize`720p`
82 label: $localize`1080p`
86 label: $localize`1440p`
90 label: $localize`2160p`
94 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
96 this.transcodingThreadOptions = [
97 { value: 0, label: $localize`Auto (via ffmpeg)` },
98 { value: 1, label: '1' },
99 { value: 2, label: '2' },
100 { value: 4, label: '4' },
101 { value: 8, label: '8' }
104 this.liveMaxDurationOptions = [
105 { value: -1, label: $localize`No limit` },
106 { value: 1000 * 3600, label: $localize`1 hour` },
107 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
108 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
109 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
113 get videoQuotaOptions () {
114 return this.configService.videoQuotaOptions
117 get videoQuotaDailyOptions () {
118 return this.configService.videoQuotaDailyOptions
121 get availableThemes () {
122 return this.serverConfig.theme.registered
126 get liveRTMPPort () {
127 return this.serverConfig.live.rtmp.port
130 getTotalTranscodingThreads () {
131 const transcodingEnabled = this.form.value['transcoding']['enabled']
132 const transcodingThreads = this.form.value['transcoding']['threads']
133 const liveTranscodingEnabled = this.form.value['live']['transcoding']['enabled']
134 const liveTranscodingThreads = this.form.value['live']['transcoding']['threads']
136 // checks whether all enabled method are on fixed values and not on auto (= 0)
137 let noneOnAuto = !transcodingEnabled || +transcodingThreads > 0
138 noneOnAuto &&= !liveTranscodingEnabled || +liveTranscodingThreads > 0
140 // count total of fixed value, repalcing auto by a single thread (knowing it will display "at least")
142 if (transcodingEnabled) value += +transcodingThreads || 1
143 if (liveTranscodingEnabled) value += +liveTranscodingThreads || 1
147 atMost: noneOnAuto, // auto switches everything to a least estimation since ffmpeg will take as many threads as possible
154 getResolutionKey (resolution: string) {
155 return 'transcoding.resolutions.' + resolution
159 this.serverConfig = this.serverService.getTmpConfig()
160 this.serverService.getConfig()
161 .subscribe(config => {
162 this.serverConfig = config
165 const formGroupData: { [key in keyof CustomConfig ]: any } = {
167 name: INSTANCE_NAME_VALIDATOR,
168 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
172 defaultNSFWPolicy: null,
177 creationReason: null,
178 moderationInformation: null,
180 maintenanceLifetime: null,
183 hardwareInformation: null,
188 defaultClientRoute: null,
200 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
206 size: CACHE_PREVIEWS_SIZE_VALIDATOR
209 size: CACHE_CAPTIONS_SIZE_VALIDATOR
214 limit: SIGNUP_LIMIT_VALIDATOR,
215 requiresEmailVerification: null
228 email: ADMIN_EMAIL_VALIDATOR
234 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
235 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
239 threads: TRANSCODING_THREADS_VALIDATOR,
240 allowAdditionalExtensions: null,
241 allowAudioFiles: null,
254 maxInstanceLives: null,
260 threads: TRANSCODING_THREADS_VALIDATOR,
284 indexUrl: INDEX_URL_VALIDATOR
301 url: SEARCH_INDEX_URL_VALIDATOR,
302 disableLocalSearch: null,
303 isDefaultSearch: null
308 const defaultValues = {
319 for (const resolution of this.resolutions) {
320 defaultValues.transcoding.resolutions[resolution.id] = 'false'
321 formGroupData.transcoding.resolutions[resolution.id] = null
324 for (const resolution of this.liveResolutions) {
325 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
326 formGroupData.live.transcoding.resolutions[resolution.id] = null
329 this.buildForm(formGroupData)
332 this.checkTranscodingFields()
333 this.checkSignupField()
336 ngAfterViewChecked () {
337 if (!this.initDone) {
343 isTranscodingEnabled () {
344 return this.form.value['transcoding']['enabled'] === true
348 return this.form.value['live']['enabled'] === true
351 isLiveTranscodingEnabled () {
352 return this.form.value['live']['transcoding']['enabled'] === true
356 return this.form.value['signup']['enabled'] === true
359 isSearchIndexEnabled () {
360 return this.form.value['search']['searchIndex']['enabled'] === true
363 isAutoFollowIndexEnabled () {
364 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
367 async formValidated () {
368 const value: CustomConfig = this.form.getRawValue()
370 this.configService.updateCustomConfig(value)
373 this.customConfig = res
375 // Reload general configuration
376 this.serverService.resetConfig()
380 this.notifier.success($localize`Configuration updated.`)
383 err => this.notifier.error(err.message)
389 'customizations': 'advanced-configuration'
391 const hash = window.location.hash.replace('#', '')
393 if (hash && Object.keys(hashToNav).includes(hash)) {
394 this.nav.select(hashToNav[hash])
395 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
399 hasConsistentOptions () {
400 if (this.hasLiveAllowReplayConsistentOptions()) return true
405 hasLiveAllowReplayConsistentOptions () {
406 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
413 private updateForm () {
414 this.form.patchValue(this.customConfig)
417 private loadForm () {
419 this.configService.getCustomConfig(),
420 this.serverService.getVideoLanguages(),
421 this.serverService.getVideoCategories()
423 ([ config, languages, categories ]) => {
424 this.customConfig = config
426 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
427 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
430 // Force form validation
434 err => this.notifier.error(err.message)
438 private checkTranscodingFields () {
439 const hlsControl = this.form.get('transcoding.hls.enabled')
440 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
442 webtorrentControl.valueChanges
443 .subscribe(newValue => {
444 if (newValue === false && !hlsControl.disabled) {
448 if (newValue === true && !hlsControl.enabled) {
453 hlsControl.valueChanges
454 .subscribe(newValue => {
455 if (newValue === false && !webtorrentControl.disabled) {
456 webtorrentControl.disable()
459 if (newValue === true && !webtorrentControl.enabled) {
460 webtorrentControl.enable()
465 private checkSignupField () {
466 const signupControl = this.form.get('signup.enabled')
468 signupControl.valueChanges
470 .subscribe(([ oldValue, newValue ]) => {
471 if (oldValue !== true && newValue === true) {
472 // tslint:disable:max-line-length
473 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
475 this.form.patchValue({