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'
25 selector: 'my-edit-custom-config',
26 templateUrl: './edit-custom-config.component.html',
27 styleUrls: [ './edit-custom-config.component.scss' ]
29 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
30 // FIXME: use built-in router
31 @ViewChild('nav') nav: NgbNav
34 customConfig: CustomConfig
36 resolutions: { id: string, label: string, description?: string }[] = []
37 liveResolutions: { id: string, label: string, description?: string }[] = []
38 transcodingThreadOptions: { label: string, value: number }[] = []
40 languageItems: SelectOptionsItem[] = []
41 categoryItems: SelectOptionsItem[] = []
43 private serverConfig: ServerConfig
46 private viewportScroller: ViewportScroller,
47 protected formValidatorService: FormValidatorService,
48 private notifier: Notifier,
49 private configService: ConfigService,
50 private serverService: ServerService
57 label: $localize`Audio-only`,
58 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
62 label: $localize`240p`
66 label: $localize`360p`
70 label: $localize`480p`
74 label: $localize`720p`
78 label: $localize`1080p`
82 label: $localize`2160p`
86 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
88 this.transcodingThreadOptions = [
89 { value: 0, label: $localize`Auto (via ffmpeg)` },
90 { value: 1, label: '1' },
91 { value: 2, label: '2' },
92 { value: 4, label: '4' },
93 { value: 8, label: '8' }
97 get videoQuotaOptions () {
98 return this.configService.videoQuotaOptions
101 get videoQuotaDailyOptions () {
102 return this.configService.videoQuotaDailyOptions
105 get availableThemes () {
106 return this.serverConfig.theme.registered
110 getResolutionKey (resolution: string) {
111 return 'transcoding.resolutions.' + resolution
115 this.serverConfig = this.serverService.getTmpConfig()
116 this.serverService.getConfig()
117 .subscribe(config => this.serverConfig = config)
119 const formGroupData: { [key in keyof CustomConfig ]: any } = {
121 name: INSTANCE_NAME_VALIDATOR,
122 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
126 defaultNSFWPolicy: null,
131 creationReason: null,
132 moderationInformation: null,
134 maintenanceLifetime: null,
137 hardwareInformation: null,
142 defaultClientRoute: null,
154 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
160 size: CACHE_PREVIEWS_SIZE_VALIDATOR
163 size: CACHE_CAPTIONS_SIZE_VALIDATOR
168 limit: SIGNUP_LIMIT_VALIDATOR,
169 requiresEmailVerification: null
182 email: ADMIN_EMAIL_VALIDATOR
188 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
189 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
193 threads: TRANSCODING_THREADS_VALIDATOR,
194 allowAdditionalExtensions: null,
195 allowAudioFiles: null,
209 threads: TRANSCODING_THREADS_VALIDATOR,
233 indexUrl: INDEX_URL_VALIDATOR
250 url: SEARCH_INDEX_URL_VALIDATOR,
251 disableLocalSearch: null,
252 isDefaultSearch: null
257 const defaultValues = {
268 for (const resolution of this.resolutions) {
269 defaultValues.transcoding.resolutions[resolution.id] = 'false'
270 formGroupData.transcoding.resolutions[resolution.id] = null
273 for (const resolution of this.liveResolutions) {
274 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
275 formGroupData.live.transcoding.resolutions[resolution.id] = null
278 this.buildForm(formGroupData)
280 this.checkTranscodingFields()
283 ngAfterViewChecked () {
284 if (!this.initDone) {
290 isTranscodingEnabled () {
291 return this.form.value['transcoding']['enabled'] === true
295 return this.form.value['live']['enabled'] === true
298 isLiveTranscodingEnabled () {
299 return this.form.value['live']['transcoding']['enabled'] === true
303 return this.form.value['signup']['enabled'] === true
306 isSearchIndexEnabled () {
307 return this.form.value['search']['searchIndex']['enabled'] === true
310 isAutoFollowIndexEnabled () {
311 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
314 async formValidated () {
315 this.configService.updateCustomConfig(this.form.getRawValue())
318 this.customConfig = res
320 // Reload general configuration
321 this.serverService.resetConfig()
325 this.notifier.success($localize`Configuration updated.`)
328 err => this.notifier.error(err.message)
334 'customizations': 'advanced-configuration'
336 const hash = window.location.hash.replace('#', '')
338 if (hash && Object.keys(hashToNav).includes(hash)) {
339 this.nav.select(hashToNav[hash])
340 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
344 private updateForm () {
345 this.form.patchValue(this.customConfig)
348 private loadForm () {
350 this.configService.getCustomConfig(),
351 this.serverService.getVideoLanguages(),
352 this.serverService.getVideoCategories()
354 ([ config, languages, categories ]) => {
355 this.customConfig = config
357 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
358 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
361 // Force form validation
365 err => this.notifier.error(err.message)
369 private checkTranscodingFields () {
370 const hlsControl = this.form.get('transcoding.hls.enabled')
371 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
373 webtorrentControl.valueChanges
374 .subscribe(newValue => {
375 if (newValue === false && !hlsControl.disabled) {
379 if (newValue === true && !hlsControl.enabled) {
384 hlsControl.valueChanges
385 .subscribe(newValue => {
386 if (newValue === false && !webtorrentControl.disabled) {
387 webtorrentControl.disable()
390 if (newValue === true && !webtorrentControl.enabled) {
391 webtorrentControl.enable()