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