1 import { Component, OnInit, AfterViewChecked, 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 { NgbTabset } 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 @ViewChild('tabs') tabs: NgbTabset
24 customConfig: CustomConfig
26 resolutions: { id: string, label: string, description?: string }[] = []
27 transcodingThreadOptions: { label: string, value: number }[] = []
29 languageItems: SelectItem[] = []
30 categoryItems: SelectItem[] = []
32 private serverConfig: ServerConfig
35 private viewportScroller: ViewportScroller,
36 protected formValidatorService: FormValidatorService,
37 private customConfigValidatorsService: CustomConfigValidatorsService,
38 private userValidatorsService: UserValidatorsService,
39 private notifier: Notifier,
40 private configService: ConfigService,
41 private serverService: ServerService,
49 label: this.i18n('Audio-only'),
50 description: this.i18n('A <code>.mp4</code> that keeps the original audio track, with no video')
54 label: this.i18n('240p')
58 label: this.i18n('360p')
62 label: this.i18n('480p')
66 label: this.i18n('720p')
70 label: this.i18n('1080p')
74 label: this.i18n('2160p')
78 this.transcodingThreadOptions = [
79 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
80 { value: 1, label: '1' },
81 { value: 2, label: '2' },
82 { value: 4, label: '4' },
83 { value: 8, label: '8' }
87 get videoQuotaOptions () {
88 return this.configService.videoQuotaOptions
91 get videoQuotaDailyOptions () {
92 return this.configService.videoQuotaDailyOptions
95 get availableThemes () {
96 return this.serverConfig.theme.registered
100 getResolutionKey (resolution: string) {
101 return 'transcoding.resolutions.' + resolution
105 this.serverConfig = this.serverService.getTmpConfig()
106 this.serverService.getConfig()
107 .subscribe(config => this.serverConfig = config)
109 const formGroupData: { [key in keyof CustomConfig ]: any } = {
111 name: this.customConfigValidatorsService.INSTANCE_NAME,
112 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
116 defaultNSFWPolicy: null,
121 creationReason: null,
122 moderationInformation: null,
124 maintenanceLifetime: null,
127 hardwareInformation: null,
132 defaultClientRoute: null,
144 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
150 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
153 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
158 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
159 requiresEmailVerification: null
172 email: this.customConfigValidatorsService.ADMIN_EMAIL
178 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
179 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
183 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
184 allowAdditionalExtensions: null,
185 allowAudioFiles: null,
214 indexUrl: this.customConfigValidatorsService.INDEX_URL
220 const defaultValues = {
225 for (const resolution of this.resolutions) {
226 defaultValues.transcoding.resolutions[resolution.id] = 'false'
227 formGroupData.transcoding.resolutions[resolution.id] = null
230 this.buildForm(formGroupData)
232 this.checkTranscodingFields()
235 ngAfterViewChecked () {
236 if (!this.initDone) {
242 isTranscodingEnabled () {
243 return this.form.value['transcoding']['enabled'] === true
247 return this.form.value['signup']['enabled'] === true
250 isAutoFollowIndexEnabled () {
251 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
254 async formValidated () {
255 this.configService.updateCustomConfig(this.form.getRawValue())
258 this.customConfig = res
260 // Reload general configuration
261 this.serverService.resetConfig()
265 this.notifier.success(this.i18n('Configuration updated.'))
268 err => this.notifier.error(err.message)
272 getSelectedLanguageLabel () {
273 return this.i18n('{{\'{0} languages selected')
276 getDefaultLanguageLabel () {
277 return this.i18n('No language')
280 getSelectedCategoryLabel () {
281 return this.i18n('{{\'{0} categories selected')
284 getDefaultCategoryLabel () {
285 return this.i18n('No category')
290 'customizations': 'advanced-configuration'
292 const hash = window.location.hash.replace('#', '')
294 if (hash && Object.keys(hashToTab).includes(hash)) {
295 this.tabs.select(hashToTab[hash])
296 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
300 private updateForm () {
301 this.form.patchValue(this.customConfig)
304 private loadForm () {
306 this.configService.getCustomConfig(),
307 this.serverService.getVideoLanguages(),
308 this.serverService.getVideoCategories()
310 ([ config, languages, categories ]) => {
311 this.customConfig = config
313 this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
314 this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
317 // Force form validation
321 err => this.notifier.error(err.message)
325 private checkTranscodingFields () {
326 const hlsControl = this.form.get('transcoding.hls.enabled')
327 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
329 webtorrentControl.valueChanges
330 .subscribe(newValue => {
331 if (newValue === false && !hlsControl.disabled) {
335 if (newValue === true && !hlsControl.enabled) {
340 hlsControl.valueChanges
341 .subscribe(newValue => {
342 if (newValue === false && !webtorrentControl.disabled) {
343 webtorrentControl.disable()
346 if (newValue === true && !webtorrentControl.enabled) {
347 webtorrentControl.enable()