Add hardware information in instance config
[oweals/peertube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1 import { Component, OnInit } 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 { first } from 'rxjs/operators'
12
13 @Component({
14   selector: 'my-edit-custom-config',
15   templateUrl: './edit-custom-config.component.html',
16   styleUrls: [ './edit-custom-config.component.scss' ]
17 })
18 export class EditCustomConfigComponent extends FormReactive implements OnInit {
19   customConfig: CustomConfig
20
21   resolutions: { id: string, label: string }[] = []
22   transcodingThreadOptions: { label: string, value: number }[] = []
23
24   languageItems: SelectItem[] = []
25   categoryItems: SelectItem[] = []
26
27   constructor (
28     protected formValidatorService: FormValidatorService,
29     private customConfigValidatorsService: CustomConfigValidatorsService,
30     private userValidatorsService: UserValidatorsService,
31     private notifier: Notifier,
32     private configService: ConfigService,
33     private serverService: ServerService,
34     private i18n: I18n
35   ) {
36     super()
37
38     this.resolutions = [
39       {
40         id: '240p',
41         label: this.i18n('240p')
42       },
43       {
44         id: '360p',
45         label: this.i18n('360p')
46       },
47       {
48         id: '480p',
49         label: this.i18n('480p')
50       },
51       {
52         id: '720p',
53         label: this.i18n('720p')
54       },
55       {
56         id: '1080p',
57         label: this.i18n('1080p')
58       },
59       {
60         id: '2160p',
61         label: this.i18n('2160p')
62       }
63     ]
64
65     this.transcodingThreadOptions = [
66       { value: 0, label: this.i18n('Auto (via ffmpeg)') },
67       { value: 1, label: '1' },
68       { value: 2, label: '2' },
69       { value: 4, label: '4' },
70       { value: 8, label: '8' }
71     ]
72   }
73
74   get videoQuotaOptions () {
75     return this.configService.videoQuotaOptions
76   }
77
78   get videoQuotaDailyOptions () {
79     return this.configService.videoQuotaDailyOptions
80   }
81
82   get availableThemes () {
83     return this.serverService.getConfig().theme.registered
84       .map(t => t.name)
85   }
86
87   getResolutionKey (resolution: string) {
88     return 'transcoding.resolutions.' + resolution
89   }
90
91   ngOnInit () {
92     const formGroupData: { [key in keyof CustomConfig ]: any } = {
93       instance: {
94         name: this.customConfigValidatorsService.INSTANCE_NAME,
95         shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
96         description: null,
97
98         isNSFW: false,
99         defaultNSFWPolicy: null,
100
101         terms: null,
102         codeOfConduct: null,
103
104         creationReason: null,
105         moderationInformation: null,
106         administrator: null,
107         maintenanceLifetime: null,
108         businessModel: null,
109
110         hardwareInformation: null,
111
112         categories: null,
113         languages: null,
114
115         defaultClientRoute: null,
116
117         customizations: {
118           javascript: null,
119           css: null
120         }
121       },
122       theme: {
123         default: null
124       },
125       services: {
126         twitter: {
127           username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
128           whitelisted: null
129         }
130       },
131       cache: {
132         previews: {
133           size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
134         },
135         captions: {
136           size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
137         }
138       },
139       signup: {
140         enabled: null,
141         limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
142         requiresEmailVerification: null
143       },
144       import: {
145         videos: {
146           http: {
147             enabled: null
148           },
149           torrent: {
150             enabled: null
151           }
152         }
153       },
154       admin: {
155         email: this.customConfigValidatorsService.ADMIN_EMAIL
156       },
157       contactForm: {
158         enabled: null
159       },
160       user: {
161         videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
162         videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
163       },
164       transcoding: {
165         enabled: null,
166         threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
167         allowAdditionalExtensions: null,
168         allowAudioFiles: null,
169         resolutions: {}
170       },
171       autoBlacklist: {
172         videos: {
173           ofUsers: {
174             enabled: null
175           }
176         }
177       },
178       followers: {
179         instance: {
180           enabled: null,
181           manualApproval: null
182         }
183       },
184       followings: {
185         instance: {
186           autoFollowBack: {
187             enabled: null
188           },
189           autoFollowIndex: {
190             enabled: null,
191             indexUrl: this.customConfigValidatorsService.INDEX_URL
192           }
193         }
194       }
195     }
196
197     const defaultValues = {
198       transcoding: {
199         resolutions: {}
200       }
201     }
202     for (const resolution of this.resolutions) {
203       defaultValues.transcoding.resolutions[resolution.id] = 'false'
204       formGroupData.transcoding.resolutions[resolution.id] = null
205     }
206
207     this.buildForm(formGroupData)
208
209     forkJoin([
210       this.configService.getCustomConfig(),
211       this.serverService.videoLanguagesLoaded.pipe(first()), // First so the observable completes
212       this.serverService.videoCategoriesLoaded.pipe(first())
213     ]).subscribe(
214       ([ config ]) => {
215         this.customConfig = config
216
217         const languages = this.serverService.getVideoLanguages()
218         this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
219
220         const categories = this.serverService.getVideoCategories()
221         this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
222
223         this.updateForm()
224         // Force form validation
225         this.forceCheck()
226       },
227
228       err => this.notifier.error(err.message)
229     )
230   }
231
232   isTranscodingEnabled () {
233     return this.form.value['transcoding']['enabled'] === true
234   }
235
236   isSignupEnabled () {
237     return this.form.value['signup']['enabled'] === true
238   }
239
240   async formValidated () {
241     this.configService.updateCustomConfig(this.form.value)
242       .subscribe(
243         res => {
244           this.customConfig = res
245
246           // Reload general configuration
247           this.serverService.loadConfig()
248
249           this.updateForm()
250
251           this.notifier.success(this.i18n('Configuration updated.'))
252         },
253
254         err => this.notifier.error(err.message)
255       )
256   }
257
258   getSelectedLanguageLabel () {
259     return this.i18n('{{\'{0} languages selected')
260   }
261
262   getDefaultLanguageLabel () {
263     return this.i18n('No language')
264   }
265
266   getSelectedCategoryLabel () {
267     return this.i18n('{{\'{0} categories selected')
268   }
269
270   getDefaultCategoryLabel () {
271     return this.i18n('No category')
272   }
273
274   private updateForm () {
275     this.form.patchValue(this.customConfig)
276   }
277 }