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