1 import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core'
2 import { ConfigService } from '@app/+admin/config/shared/config.service'
3 import { ServerService } from '@app/core/server/server.service'
4 import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
5 import { Notifier } from '@app/core'
6 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { SelectItem } from 'primeng/api'
10 import { forkJoin } from 'rxjs'
11 import { ServerConfig } from '@shared/models'
12 import { ViewportScroller } from '@angular/common'
13 import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
16 selector: 'my-edit-custom-config',
17 templateUrl: './edit-custom-config.component.html',
18 styleUrls: [ './edit-custom-config.component.scss' ]
20 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
21 // FIXME: use built-in router
22 @ViewChild('nav') nav: NgbNav
25 customConfig: CustomConfig
27 resolutions: { id: string, label: string, description?: string }[] = []
28 transcodingThreadOptions: { label: string, value: number }[] = []
30 languageItems: SelectItem[] = []
31 categoryItems: SelectItem[] = []
33 private serverConfig: ServerConfig
36 private viewportScroller: ViewportScroller,
37 protected formValidatorService: FormValidatorService,
38 private customConfigValidatorsService: CustomConfigValidatorsService,
39 private userValidatorsService: UserValidatorsService,
40 private notifier: Notifier,
41 private configService: ConfigService,
42 private serverService: ServerService,
50 label: this.i18n('Audio-only'),
51 description: this.i18n('A <code>.mp4</code> that keeps the original audio track, with no video')
55 label: this.i18n('240p')
59 label: this.i18n('360p')
63 label: this.i18n('480p')
67 label: this.i18n('720p')
71 label: this.i18n('1080p')
75 label: this.i18n('2160p')
79 this.transcodingThreadOptions = [
80 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
81 { value: 1, label: '1' },
82 { value: 2, label: '2' },
83 { value: 4, label: '4' },
84 { value: 8, label: '8' }
88 get videoQuotaOptions () {
89 return this.configService.videoQuotaOptions
92 get videoQuotaDailyOptions () {
93 return this.configService.videoQuotaDailyOptions
96 get availableThemes () {
97 return this.serverConfig.theme.registered
101 getResolutionKey (resolution: string) {
102 return 'transcoding.resolutions.' + resolution
106 this.serverConfig = this.serverService.getTmpConfig()
107 this.serverService.getConfig()
108 .subscribe(config => this.serverConfig = config)
110 const formGroupData: { [key in keyof CustomConfig ]: any } = {
112 name: this.customConfigValidatorsService.INSTANCE_NAME,
113 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
117 defaultNSFWPolicy: null,
122 creationReason: null,
123 moderationInformation: null,
125 maintenanceLifetime: null,
128 hardwareInformation: null,
133 defaultClientRoute: null,
145 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
151 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
154 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
159 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
160 requiresEmailVerification: null
173 email: this.customConfigValidatorsService.ADMIN_EMAIL
179 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
180 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
184 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
185 allowAdditionalExtensions: null,
186 allowAudioFiles: null,
215 indexUrl: this.customConfigValidatorsService.INDEX_URL
221 const defaultValues = {
226 for (const resolution of this.resolutions) {
227 defaultValues.transcoding.resolutions[resolution.id] = 'false'
228 formGroupData.transcoding.resolutions[resolution.id] = null
231 this.buildForm(formGroupData)
233 this.checkTranscodingFields()
236 ngAfterViewChecked () {
237 if (!this.initDone) {
243 isTranscodingEnabled () {
244 return this.form.value['transcoding']['enabled'] === true
248 return this.form.value['signup']['enabled'] === true
251 isAutoFollowIndexEnabled () {
252 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
255 async formValidated () {
256 this.configService.updateCustomConfig(this.form.getRawValue())
259 this.customConfig = res
261 // Reload general configuration
262 this.serverService.resetConfig()
266 this.notifier.success(this.i18n('Configuration updated.'))
269 err => this.notifier.error(err.message)
273 getSelectedLanguageLabel () {
274 return this.i18n('{{\'{0} languages selected')
277 getDefaultLanguageLabel () {
278 return this.i18n('No language')
281 getSelectedCategoryLabel () {
282 return this.i18n('{{\'{0} categories selected')
285 getDefaultCategoryLabel () {
286 return this.i18n('No category')
291 'customizations': 'advanced-configuration'
293 const hash = window.location.hash.replace('#', '')
295 if (hash && Object.keys(hashToNav).includes(hash)) {
296 this.nav.select(hashToNav[hash])
297 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
301 private updateForm () {
302 this.form.patchValue(this.customConfig)
305 private loadForm () {
307 this.configService.getCustomConfig(),
308 this.serverService.getVideoLanguages(),
309 this.serverService.getVideoCategories()
311 ([ config, languages, categories ]) => {
312 this.customConfig = config
314 this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
315 this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
318 // Force form validation
322 err => this.notifier.error(err.message)
326 private checkTranscodingFields () {
327 const hlsControl = this.form.get('transcoding.hls.enabled')
328 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
330 webtorrentControl.valueChanges
331 .subscribe(newValue => {
332 if (newValue === false && !hlsControl.disabled) {
336 if (newValue === true && !hlsControl.enabled) {
341 hlsControl.valueChanges
342 .subscribe(newValue => {
343 if (newValue === false && !webtorrentControl.disabled) {
344 webtorrentControl.disable()
347 if (newValue === true && !webtorrentControl.enabled) {
348 webtorrentControl.enable()