]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Add description for sensitive configuration options, reword others
[github/Chocobozzz/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 { ServerConfig } from '@shared/models'
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, description?: string }[] = []
22 transcodingThreadOptions: { label: string, value: number }[] = []
23
24 languageItems: SelectItem[] = []
25 categoryItems: SelectItem[] = []
26
27 isAutoFollowIndexEnabled = false
28
29 private serverConfig: ServerConfig
30
31 constructor (
32 protected formValidatorService: FormValidatorService,
33 private customConfigValidatorsService: CustomConfigValidatorsService,
34 private userValidatorsService: UserValidatorsService,
35 private notifier: Notifier,
36 private configService: ConfigService,
37 private serverService: ServerService,
38 private i18n: I18n
39 ) {
40 super()
41
42 this.resolutions = [
43 {
44 id: '0p',
45 label: this.i18n('Audio-only'),
46 description: this.i18n('A <code>.mp4</code> that keeps the original audio track, with no video')
47 },
48 {
49 id: '240p',
50 label: this.i18n('240p')
51 },
52 {
53 id: '360p',
54 label: this.i18n('360p')
55 },
56 {
57 id: '480p',
58 label: this.i18n('480p')
59 },
60 {
61 id: '720p',
62 label: this.i18n('720p')
63 },
64 {
65 id: '1080p',
66 label: this.i18n('1080p')
67 },
68 {
69 id: '2160p',
70 label: this.i18n('2160p')
71 }
72 ]
73
74 this.transcodingThreadOptions = [
75 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
76 { value: 1, label: '1' },
77 { value: 2, label: '2' },
78 { value: 4, label: '4' },
79 { value: 8, label: '8' }
80 ]
81 }
82
83 get videoQuotaOptions () {
84 return this.configService.videoQuotaOptions
85 }
86
87 get videoQuotaDailyOptions () {
88 return this.configService.videoQuotaDailyOptions
89 }
90
91 get availableThemes () {
92 return this.serverConfig.theme.registered
93 .map(t => t.name)
94 }
95
96 getResolutionKey (resolution: string) {
97 return 'transcoding.resolutions.' + resolution
98 }
99
100 ngOnInit () {
101 this.serverConfig = this.serverService.getTmpConfig()
102 this.serverService.getConfig()
103 .subscribe(config => this.serverConfig = config)
104
105 const formGroupData: { [key in keyof CustomConfig ]: any } = {
106 instance: {
107 name: this.customConfigValidatorsService.INSTANCE_NAME,
108 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
109 description: null,
110
111 isNSFW: false,
112 defaultNSFWPolicy: null,
113
114 terms: null,
115 codeOfConduct: null,
116
117 creationReason: null,
118 moderationInformation: null,
119 administrator: null,
120 maintenanceLifetime: null,
121 businessModel: null,
122
123 hardwareInformation: null,
124
125 categories: null,
126 languages: null,
127
128 defaultClientRoute: null,
129
130 customizations: {
131 javascript: null,
132 css: null
133 }
134 },
135 theme: {
136 default: null
137 },
138 services: {
139 twitter: {
140 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
141 whitelisted: null
142 }
143 },
144 cache: {
145 previews: {
146 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
147 },
148 captions: {
149 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
150 }
151 },
152 signup: {
153 enabled: null,
154 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
155 requiresEmailVerification: null
156 },
157 import: {
158 videos: {
159 http: {
160 enabled: null
161 },
162 torrent: {
163 enabled: null
164 }
165 }
166 },
167 admin: {
168 email: this.customConfigValidatorsService.ADMIN_EMAIL
169 },
170 contactForm: {
171 enabled: null
172 },
173 user: {
174 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
175 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
176 },
177 transcoding: {
178 enabled: null,
179 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
180 allowAdditionalExtensions: null,
181 allowAudioFiles: null,
182 resolutions: {},
183 hls: {
184 enabled: null
185 },
186 webtorrent: {
187 enabled: null
188 }
189 },
190 autoBlacklist: {
191 videos: {
192 ofUsers: {
193 enabled: null
194 }
195 }
196 },
197 followers: {
198 instance: {
199 enabled: null,
200 manualApproval: null
201 }
202 },
203 followings: {
204 instance: {
205 autoFollowBack: {
206 enabled: null
207 },
208 autoFollowIndex: {
209 enabled: null,
210 indexUrl: this.customConfigValidatorsService.INDEX_URL
211 }
212 }
213 }
214 }
215
216 const defaultValues = {
217 transcoding: {
218 resolutions: {}
219 }
220 }
221 for (const resolution of this.resolutions) {
222 defaultValues.transcoding.resolutions[resolution.id] = 'false'
223 formGroupData.transcoding.resolutions[resolution.id] = null
224 }
225
226 this.buildForm(formGroupData)
227 this.loadForm()
228 this.checkTranscodingFields()
229 }
230
231 isTranscodingEnabled () {
232 return this.form.value['transcoding']['enabled'] === true
233 }
234
235 isSignupEnabled () {
236 return this.form.value['signup']['enabled'] === true
237 }
238
239 async formValidated () {
240 this.configService.updateCustomConfig(this.form.getRawValue())
241 .subscribe(
242 res => {
243 this.customConfig = res
244
245 // Reload general configuration
246 this.serverService.resetConfig()
247
248 this.updateForm()
249
250 this.notifier.success(this.i18n('Configuration updated.'))
251 },
252
253 err => this.notifier.error(err.message)
254 )
255 }
256
257 getSelectedLanguageLabel () {
258 return this.i18n('{{\'{0} languages selected')
259 }
260
261 getDefaultLanguageLabel () {
262 return this.i18n('No language')
263 }
264
265 getSelectedCategoryLabel () {
266 return this.i18n('{{\'{0} categories selected')
267 }
268
269 getDefaultCategoryLabel () {
270 return this.i18n('No category')
271 }
272
273 private updateForm () {
274 this.form.patchValue(this.customConfig)
275 }
276
277 private loadForm () {
278 forkJoin([
279 this.configService.getCustomConfig(),
280 this.serverService.getVideoLanguages(),
281 this.serverService.getVideoCategories()
282 ]).subscribe(
283 ([ config, languages, categories ]) => {
284 this.customConfig = config
285
286 this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
287 this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
288
289 this.updateForm()
290 // Force form validation
291 this.forceCheck()
292 },
293
294 err => this.notifier.error(err.message)
295 )
296 }
297
298 private checkTranscodingFields () {
299 const hlsControl = this.form.get('transcoding.hls.enabled')
300 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
301
302 webtorrentControl.valueChanges
303 .subscribe(newValue => {
304 if (newValue === false && !hlsControl.disabled) {
305 hlsControl.disable()
306 }
307
308 if (newValue === true && !hlsControl.enabled) {
309 hlsControl.enable()
310 }
311 })
312
313 hlsControl.valueChanges
314 .subscribe(newValue => {
315 if (newValue === false && !webtorrentControl.disabled) {
316 webtorrentControl.disable()
317 }
318
319 if (newValue === true && !webtorrentControl.enabled) {
320 webtorrentControl.enable()
321 }
322 })
323 }
324 }