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`2160p`
90 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
92 this.transcodingThreadOptions = [
93 { value: 0, label: $localize`Auto (via ffmpeg)` },
94 { value: 1, label: '1' },
95 { value: 2, label: '2' },
96 { value: 4, label: '4' },
97 { value: 8, label: '8' }
100 this.liveMaxDurationOptions = [
101 { value: -1, label: $localize`No limit` },
102 { value: 1000 * 3600, label: $localize`1 hour` },
103 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
104 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
105 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
109 get videoQuotaOptions () {
110 return this.configService.videoQuotaOptions
113 get videoQuotaDailyOptions () {
114 return this.configService.videoQuotaDailyOptions
117 get availableThemes () {
118 return this.serverConfig.theme.registered
122 getTotalTranscodingThreads () {
123 const transcodingEnabled = this.form.value['transcoding']['enabled']
124 const transcodingThreads = this.form.value['transcoding']['threads']
125 const liveTranscodingEnabled = this.form.value['live']['transcoding']['enabled']
126 const liveTranscodingThreads = this.form.value['live']['transcoding']['threads']
128 // checks whether all enabled method are on fixed values and not on auto (= 0)
129 let noneOnAuto = !transcodingEnabled || +transcodingThreads > 0
130 noneOnAuto &&= !liveTranscodingEnabled || +liveTranscodingThreads > 0
132 // count total of fixed value, repalcing auto by a single thread (knowing it will display "at least")
134 if (transcodingEnabled) value += +transcodingThreads || 1
135 if (liveTranscodingEnabled) value += +liveTranscodingThreads || 1
139 atMost: noneOnAuto, // auto switches everything to a least estimation since ffmpeg will take as many threads as possible
146 getResolutionKey (resolution: string) {
147 return 'transcoding.resolutions.' + resolution
151 this.serverConfig = this.serverService.getTmpConfig()
152 this.serverService.getConfig()
153 .subscribe(config => {
154 this.serverConfig = config
157 const formGroupData: { [key in keyof CustomConfig ]: any } = {
159 name: INSTANCE_NAME_VALIDATOR,
160 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
164 defaultNSFWPolicy: null,
169 creationReason: null,
170 moderationInformation: null,
172 maintenanceLifetime: null,
175 hardwareInformation: null,
180 defaultClientRoute: null,
192 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
198 size: CACHE_PREVIEWS_SIZE_VALIDATOR
201 size: CACHE_CAPTIONS_SIZE_VALIDATOR
206 limit: SIGNUP_LIMIT_VALIDATOR,
207 requiresEmailVerification: null
220 email: ADMIN_EMAIL_VALIDATOR
226 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
227 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
231 threads: TRANSCODING_THREADS_VALIDATOR,
232 allowAdditionalExtensions: null,
233 allowAudioFiles: null,
246 maxInstanceLives: null,
252 threads: TRANSCODING_THREADS_VALIDATOR,
276 indexUrl: INDEX_URL_VALIDATOR
293 url: SEARCH_INDEX_URL_VALIDATOR,
294 disableLocalSearch: null,
295 isDefaultSearch: null
300 const defaultValues = {
311 for (const resolution of this.resolutions) {
312 defaultValues.transcoding.resolutions[resolution.id] = 'false'
313 formGroupData.transcoding.resolutions[resolution.id] = null
316 for (const resolution of this.liveResolutions) {
317 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
318 formGroupData.live.transcoding.resolutions[resolution.id] = null
321 this.buildForm(formGroupData)
324 this.checkTranscodingFields()
325 this.checkSignupField()
328 ngAfterViewChecked () {
329 if (!this.initDone) {
335 isTranscodingEnabled () {
336 return this.form.value['transcoding']['enabled'] === true
340 return this.form.value['live']['enabled'] === true
343 isLiveTranscodingEnabled () {
344 return this.form.value['live']['transcoding']['enabled'] === true
348 return this.form.value['signup']['enabled'] === true
351 isSearchIndexEnabled () {
352 return this.form.value['search']['searchIndex']['enabled'] === true
355 isAutoFollowIndexEnabled () {
356 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
359 async formValidated () {
360 const value: CustomConfig = this.form.getRawValue()
362 this.configService.updateCustomConfig(value)
365 this.customConfig = res
367 // Reload general configuration
368 this.serverService.resetConfig()
372 this.notifier.success($localize`Configuration updated.`)
375 err => this.notifier.error(err.message)
381 'customizations': 'advanced-configuration'
383 const hash = window.location.hash.replace('#', '')
385 if (hash && Object.keys(hashToNav).includes(hash)) {
386 this.nav.select(hashToNav[hash])
387 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
391 hasConsistentOptions () {
392 if (this.hasLiveAllowReplayConsistentOptions()) return true
397 hasLiveAllowReplayConsistentOptions () {
398 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
405 private updateForm () {
406 this.form.patchValue(this.customConfig)
409 private loadForm () {
411 this.configService.getCustomConfig(),
412 this.serverService.getVideoLanguages(),
413 this.serverService.getVideoCategories()
415 ([ config, languages, categories ]) => {
416 this.customConfig = config
418 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
419 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
422 // Force form validation
426 err => this.notifier.error(err.message)
430 private checkTranscodingFields () {
431 const hlsControl = this.form.get('transcoding.hls.enabled')
432 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
434 webtorrentControl.valueChanges
435 .subscribe(newValue => {
436 if (newValue === false && !hlsControl.disabled) {
440 if (newValue === true && !hlsControl.enabled) {
445 hlsControl.valueChanges
446 .subscribe(newValue => {
447 if (newValue === false && !webtorrentControl.disabled) {
448 webtorrentControl.disable()
451 if (newValue === true && !webtorrentControl.enabled) {
452 webtorrentControl.enable()
457 private checkSignupField () {
458 const signupControl = this.form.get('signup.enabled')
460 signupControl.valueChanges
462 .subscribe(([ oldValue, newValue ]) => {
463 if (oldValue !== true && newValue === true) {
464 // tslint:disable:max-line-length
465 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
467 this.form.patchValue({