]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
:sparkles: Add input-password #3375
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
CommitLineData
ccc00cb2 1import { forkJoin } from 'rxjs'
45e0d669 2import { ViewportScroller } from '@angular/common'
67ed6552
C
3import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core'
4import { ConfigService } from '@app/+admin/config/shared/config.service'
5import { Notifier } from '@app/core'
6import { ServerService } from '@app/core/server/server.service'
52c4976f 7import {
7ed1edbb
C
8 ADMIN_EMAIL_VALIDATOR,
9 CACHE_CAPTIONS_SIZE_VALIDATOR,
10 CACHE_PREVIEWS_SIZE_VALIDATOR,
11 INDEX_URL_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'
19import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
20import { FormReactive, FormValidatorService, SelectOptionsItem } from '@app/shared/shared-forms'
45c6bcf3 21import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
67ed6552 22import { CustomConfig, ServerConfig } from '@shared/models'
16a173bb 23import { pairwise } from 'rxjs/operators'
fd206f0b
C
24
25@Component({
26 selector: 'my-edit-custom-config',
27 templateUrl: './edit-custom-config.component.html',
28 styleUrls: [ './edit-custom-config.component.scss' ]
29})
45e0d669 30export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
45c6bcf3
C
31 // FIXME: use built-in router
32 @ViewChild('nav') nav: NgbNav
45e0d669
RK
33
34 initDone = false
bee0abff 35 customConfig: CustomConfig
bee0abff 36
46db9430 37 resolutions: { id: string, label: string, description?: string }[] = []
c6c0fa6c 38 liveResolutions: { id: string, label: string, description?: string }[] = []
3827c3b3 39 transcodingThreadOptions: { label: string, value: number }[] = []
fb719404 40 liveMaxDurationOptions: { label: string, value: number }[] = []
fd206f0b 41
52c4976f
C
42 languageItems: SelectOptionsItem[] = []
43 categoryItems: SelectOptionsItem[] = []
ccc00cb2 44
16a173bb
C
45 signupAlertMessage: string
46
ba430d75
C
47 private serverConfig: ServerConfig
48
fd206f0b 49 constructor (
45e0d669 50 private viewportScroller: ViewportScroller,
d18d6478 51 protected formValidatorService: FormValidatorService,
f8b2c1b4 52 private notifier: Notifier,
fd206f0b 53 private configService: ConfigService,
66357162 54 private serverService: ServerService
fd206f0b
C
55 ) {
56 super()
3827c3b3
C
57
58 this.resolutions = [
2fa9c40e 59 {
5c7d6508 60 id: '0p',
66357162
C
61 label: $localize`Audio-only`,
62 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
5c7d6508 63 },
00aa1f0d
C
64 {
65 id: '240p',
66357162 66 label: $localize`240p`
00aa1f0d
C
67 },
68 {
69 id: '360p',
66357162 70 label: $localize`360p`
00aa1f0d
C
71 },
72 {
73 id: '480p',
66357162 74 label: $localize`480p`
00aa1f0d
C
75 },
76 {
77 id: '720p',
66357162 78 label: $localize`720p`
00aa1f0d
C
79 },
80 {
81 id: '1080p',
66357162 82 label: $localize`1080p`
00aa1f0d
C
83 },
84 {
85 id: '2160p',
66357162 86 label: $localize`2160p`
00aa1f0d 87 }
3827c3b3
C
88 ]
89
c6c0fa6c
C
90 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
91
3827c3b3 92 this.transcodingThreadOptions = [
66357162 93 { value: 0, label: $localize`Auto (via ffmpeg)` },
3827c3b3
C
94 { value: 1, label: '1' },
95 { value: 2, label: '2' },
96 { value: 4, label: '4' },
97 { value: 8, label: '8' }
98 ]
fb719404
C
99
100 this.liveMaxDurationOptions = [
210856a7 101 { value: null, label: $localize`No limit` },
fb719404
C
102 { value: 1000 * 3600, label: $localize`1 hour` },
103 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
104 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
105 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
106 ]
fd206f0b
C
107 }
108
41a676db 109 get videoQuotaOptions () {
3827c3b3 110 return this.configService.videoQuotaOptions
41a676db
C
111 }
112
113 get videoQuotaDailyOptions () {
3827c3b3 114 return this.configService.videoQuotaDailyOptions
41a676db
C
115 }
116
7cd4d2ba 117 get availableThemes () {
ba430d75 118 return this.serverConfig.theme.registered
ffb321be 119 .map(t => t.name)
7cd4d2ba
C
120 }
121
fd206f0b 122 getResolutionKey (resolution: string) {
3866f1a0 123 return 'transcoding.resolutions.' + resolution
fd206f0b
C
124 }
125
d18d6478 126 ngOnInit () {
ba430d75
C
127 this.serverConfig = this.serverService.getTmpConfig()
128 this.serverService.getConfig()
fb719404
C
129 .subscribe(config => {
130 this.serverConfig = config
131 })
ba430d75 132
3866f1a0
C
133 const formGroupData: { [key in keyof CustomConfig ]: any } = {
134 instance: {
7ed1edbb
C
135 name: INSTANCE_NAME_VALIDATOR,
136 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
3866f1a0 137 description: null,
ccc00cb2 138
f8802489 139 isNSFW: false,
3866f1a0 140 defaultNSFWPolicy: null,
ccc00cb2
C
141
142 terms: null,
143 codeOfConduct: null,
8ae03c37
C
144
145 creationReason: null,
ccc00cb2
C
146 moderationInformation: null,
147 administrator: null,
148 maintenanceLifetime: null,
149 businessModel: null,
150
be04c6fd
C
151 hardwareInformation: null,
152
ccc00cb2
C
153 categories: null,
154 languages: null,
155
156 defaultClientRoute: null,
157
3866f1a0
C
158 customizations: {
159 javascript: null,
160 css: null
161 }
162 },
7cd4d2ba
C
163 theme: {
164 default: null
165 },
3866f1a0
C
166 services: {
167 twitter: {
7ed1edbb 168 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
3866f1a0
C
169 whitelisted: null
170 }
171 },
172 cache: {
173 previews: {
7ed1edbb 174 size: CACHE_PREVIEWS_SIZE_VALIDATOR
3866f1a0
C
175 },
176 captions: {
7ed1edbb 177 size: CACHE_CAPTIONS_SIZE_VALIDATOR
3866f1a0
C
178 }
179 },
180 signup: {
181 enabled: null,
7ed1edbb 182 limit: SIGNUP_LIMIT_VALIDATOR,
3866f1a0
C
183 requiresEmailVerification: null
184 },
185 import: {
186 videos: {
187 http: {
188 enabled: null
189 },
190 torrent: {
191 enabled: null
192 }
193 }
194 },
195 admin: {
7ed1edbb 196 email: ADMIN_EMAIL_VALIDATOR
3866f1a0
C
197 },
198 contactForm: {
199 enabled: null
200 },
201 user: {
7ed1edbb
C
202 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
203 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
3866f1a0
C
204 },
205 transcoding: {
206 enabled: null,
7ed1edbb 207 threads: TRANSCODING_THREADS_VALIDATOR,
3866f1a0 208 allowAdditionalExtensions: null,
536598cf 209 allowAudioFiles: null,
5d9e4eaa
C
210 resolutions: {},
211 hls: {
212 enabled: null
5a71acd2
C
213 },
214 webtorrent: {
215 enabled: null
5d9e4eaa 216 }
7ccddd7b 217 },
c6c0fa6c
C
218 live: {
219 enabled: null,
220
fb719404 221 maxDuration: null,
a056ca48
C
222 maxInstanceLives: null,
223 maxUserLives: null,
fb719404
C
224 allowReplay: null,
225
c6c0fa6c
C
226 transcoding: {
227 enabled: null,
228 threads: TRANSCODING_THREADS_VALIDATOR,
229 resolutions: {}
230 }
231 },
7ccddd7b
JM
232 autoBlacklist: {
233 videos: {
234 ofUsers: {
235 enabled: null
236 }
237 }
0dc64777
C
238 },
239 followers: {
240 instance: {
241 enabled: null,
242 manualApproval: null
243 }
e1b49ee5
C
244 },
245 followings: {
246 instance: {
247 autoFollowBack: {
248 enabled: null
249 },
250 autoFollowIndex: {
251 enabled: null,
7ed1edbb 252 indexUrl: INDEX_URL_VALIDATOR
e1b49ee5
C
253 }
254 }
72c33e71
C
255 },
256 broadcastMessage: {
257 enabled: null,
258 level: null,
259 dismissable: null,
260 message: null
5fb2e288
C
261 },
262 search: {
263 remoteUri: {
264 users: null,
265 anonymous: null
266 },
267 searchIndex: {
268 enabled: null,
7ed1edbb 269 url: SEARCH_INDEX_URL_VALIDATOR,
5fb2e288
C
270 disableLocalSearch: null,
271 isDefaultSearch: null
272 }
3866f1a0 273 }
fd206f0b
C
274 }
275
3866f1a0
C
276 const defaultValues = {
277 transcoding: {
278 resolutions: {}
c6c0fa6c
C
279 },
280 live: {
281 transcoding: {
282 resolutions: {}
283 }
3866f1a0
C
284 }
285 }
c6c0fa6c 286
fd206f0b 287 for (const resolution of this.resolutions) {
00aa1f0d
C
288 defaultValues.transcoding.resolutions[resolution.id] = 'false'
289 formGroupData.transcoding.resolutions[resolution.id] = null
fd206f0b
C
290 }
291
c6c0fa6c
C
292 for (const resolution of this.liveResolutions) {
293 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
294 formGroupData.live.transcoding.resolutions[resolution.id] = null
295 }
296
d18d6478 297 this.buildForm(formGroupData)
04cda1d7 298 this.loadForm()
16a173bb 299
04cda1d7 300 this.checkTranscodingFields()
16a173bb 301 this.checkSignupField()
fd206f0b
C
302 }
303
45e0d669
RK
304 ngAfterViewChecked () {
305 if (!this.initDone) {
306 this.initDone = true
307 this.gotoAnchor()
308 }
309 }
310
fd206f0b 311 isTranscodingEnabled () {
3866f1a0 312 return this.form.value['transcoding']['enabled'] === true
fd206f0b
C
313 }
314
c6c0fa6c
C
315 isLiveEnabled () {
316 return this.form.value['live']['enabled'] === true
317 }
318
319 isLiveTranscodingEnabled () {
320 return this.form.value['live']['transcoding']['enabled'] === true
321 }
322
fd206f0b 323 isSignupEnabled () {
3866f1a0 324 return this.form.value['signup']['enabled'] === true
fd206f0b
C
325 }
326
5fb2e288
C
327 isSearchIndexEnabled () {
328 return this.form.value['search']['searchIndex']['enabled'] === true
329 }
330
4ee6a8b1
C
331 isAutoFollowIndexEnabled () {
332 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
333 }
334
1f30a185 335 async formValidated () {
210856a7
C
336 const value: CustomConfig = this.form.getRawValue()
337
338 // Transform "null" to null
339 const maxDuration = value.live.maxDuration as any
340 if (maxDuration === 'null') value.live.maxDuration = null
341
342 this.configService.updateCustomConfig(value)
fd206f0b
C
343 .subscribe(
344 res => {
345 this.customConfig = res
346
347 // Reload general configuration
ba430d75 348 this.serverService.resetConfig()
fd206f0b
C
349
350 this.updateForm()
66b16caf 351
66357162 352 this.notifier.success($localize`Configuration updated.`)
fd206f0b
C
353 },
354
f8b2c1b4 355 err => this.notifier.error(err.message)
fd206f0b
C
356 )
357 }
358
45e0d669 359 gotoAnchor () {
45c6bcf3 360 const hashToNav = {
45e0d669
RK
361 'customizations': 'advanced-configuration'
362 }
363 const hash = window.location.hash.replace('#', '')
364
45c6bcf3
C
365 if (hash && Object.keys(hashToNav).includes(hash)) {
366 this.nav.select(hashToNav[hash])
45e0d669
RK
367 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
368 }
369 }
370
fb719404
C
371 hasConsistentOptions () {
372 if (this.hasLiveAllowReplayConsistentOptions()) return true
373
374 return false
375 }
376
377 hasLiveAllowReplayConsistentOptions () {
378 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
379 return false
380 }
381
382 return true
383 }
384
fd206f0b 385 private updateForm () {
3866f1a0 386 this.form.patchValue(this.customConfig)
fd206f0b 387 }
04cda1d7
C
388
389 private loadForm () {
390 forkJoin([
391 this.configService.getCustomConfig(),
392 this.serverService.getVideoLanguages(),
393 this.serverService.getVideoCategories()
394 ]).subscribe(
395 ([ config, languages, categories ]) => {
396 this.customConfig = config
397
52c4976f
C
398 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
399 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
04cda1d7
C
400
401 this.updateForm()
402 // Force form validation
403 this.forceCheck()
404 },
405
406 err => this.notifier.error(err.message)
407 )
408 }
409
410 private checkTranscodingFields () {
411 const hlsControl = this.form.get('transcoding.hls.enabled')
412 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
413
414 webtorrentControl.valueChanges
415 .subscribe(newValue => {
416 if (newValue === false && !hlsControl.disabled) {
417 hlsControl.disable()
418 }
419
420 if (newValue === true && !hlsControl.enabled) {
421 hlsControl.enable()
422 }
423 })
424
425 hlsControl.valueChanges
426 .subscribe(newValue => {
427 if (newValue === false && !webtorrentControl.disabled) {
428 webtorrentControl.disable()
429 }
430
431 if (newValue === true && !webtorrentControl.enabled) {
432 webtorrentControl.enable()
433 }
434 })
435 }
16a173bb
C
436
437 private checkSignupField () {
438 const signupControl = this.form.get('signup.enabled')
439
440 signupControl.valueChanges
441 .pipe(pairwise())
442 .subscribe(([ oldValue, newValue ]) => {
443 if (oldValue !== true && newValue === true) {
444 // tslint:disable:max-line-length
445 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
446
447 this.form.patchValue({
448 autoBlacklist: {
449 videos: {
450 ofUsers: {
451 enabled: true
452 }
453 }
454 }
455 })
456 }
457 })
458 }
fd206f0b 459}