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 transcodingThreadOptions: { label: string, value: number }[] = []
39 languageItems: SelectOptionsItem[] = []
40 categoryItems: SelectOptionsItem[] = []
42 private serverConfig: ServerConfig
45 private viewportScroller: ViewportScroller,
46 protected formValidatorService: FormValidatorService,
47 private notifier: Notifier,
48 private configService: ConfigService,
49 private serverService: ServerService
56 label: $localize`Audio-only`,
57 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
61 label: $localize`240p`
65 label: $localize`360p`
69 label: $localize`480p`
73 label: $localize`720p`
77 label: $localize`1080p`
81 label: $localize`2160p`
85 this.transcodingThreadOptions = [
86 { value: 0, label: $localize`Auto (via ffmpeg)` },
87 { value: 1, label: '1' },
88 { value: 2, label: '2' },
89 { value: 4, label: '4' },
90 { value: 8, label: '8' }
94 get videoQuotaOptions () {
95 return this.configService.videoQuotaOptions
98 get videoQuotaDailyOptions () {
99 return this.configService.videoQuotaDailyOptions
102 get availableThemes () {
103 return this.serverConfig.theme.registered
107 getResolutionKey (resolution: string) {
108 return 'transcoding.resolutions.' + resolution
112 this.serverConfig = this.serverService.getTmpConfig()
113 this.serverService.getConfig()
114 .subscribe(config => this.serverConfig = config)
116 const formGroupData: { [key in keyof CustomConfig ]: any } = {
118 name: INSTANCE_NAME_VALIDATOR,
119 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
123 defaultNSFWPolicy: null,
128 creationReason: null,
129 moderationInformation: null,
131 maintenanceLifetime: null,
134 hardwareInformation: null,
139 defaultClientRoute: null,
151 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
157 size: CACHE_PREVIEWS_SIZE_VALIDATOR
160 size: CACHE_CAPTIONS_SIZE_VALIDATOR
165 limit: SIGNUP_LIMIT_VALIDATOR,
166 requiresEmailVerification: null
179 email: ADMIN_EMAIL_VALIDATOR
185 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
186 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
190 threads: TRANSCODING_THREADS_VALIDATOR,
191 allowAdditionalExtensions: null,
192 allowAudioFiles: null,
221 indexUrl: INDEX_URL_VALIDATOR
238 url: SEARCH_INDEX_URL_VALIDATOR,
239 disableLocalSearch: null,
240 isDefaultSearch: null
245 const defaultValues = {
250 for (const resolution of this.resolutions) {
251 defaultValues.transcoding.resolutions[resolution.id] = 'false'
252 formGroupData.transcoding.resolutions[resolution.id] = null
255 this.buildForm(formGroupData)
257 this.checkTranscodingFields()
260 ngAfterViewChecked () {
261 if (!this.initDone) {
267 isTranscodingEnabled () {
268 return this.form.value['transcoding']['enabled'] === true
272 return this.form.value['signup']['enabled'] === true
275 isSearchIndexEnabled () {
276 return this.form.value['search']['searchIndex']['enabled'] === true
279 isAutoFollowIndexEnabled () {
280 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
283 async formValidated () {
284 this.configService.updateCustomConfig(this.form.getRawValue())
287 this.customConfig = res
289 // Reload general configuration
290 this.serverService.resetConfig()
294 this.notifier.success($localize`Configuration updated.`)
297 err => this.notifier.error(err.message)
303 'customizations': 'advanced-configuration'
305 const hash = window.location.hash.replace('#', '')
307 if (hash && Object.keys(hashToNav).includes(hash)) {
308 this.nav.select(hashToNav[hash])
309 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
313 private updateForm () {
314 this.form.patchValue(this.customConfig)
317 private loadForm () {
319 this.configService.getCustomConfig(),
320 this.serverService.getVideoLanguages(),
321 this.serverService.getVideoCategories()
323 ([ config, languages, categories ]) => {
324 this.customConfig = config
326 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
327 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
330 // Force form validation
334 err => this.notifier.error(err.message)
338 private checkTranscodingFields () {
339 const hlsControl = this.form.get('transcoding.hls.enabled')
340 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
342 webtorrentControl.valueChanges
343 .subscribe(newValue => {
344 if (newValue === false && !hlsControl.disabled) {
348 if (newValue === true && !hlsControl.enabled) {
353 hlsControl.valueChanges
354 .subscribe(newValue => {
355 if (newValue === false && !webtorrentControl.disabled) {
356 webtorrentControl.disable()
359 if (newValue === true && !webtorrentControl.enabled) {
360 webtorrentControl.enable()