]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
add default trending page choice, revert comments count for hot strategy
[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 defaultTrendingRoute: null,
190 pages: {
191 hot: {
192 enabled: null
193 }
194 },
195
196 customizations: {
197 javascript: null,
198 css: null
199 }
200 },
201 theme: {
202 default: null
203 },
204 services: {
205 twitter: {
206 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
207 whitelisted: null
208 }
209 },
210 cache: {
211 previews: {
212 size: CACHE_PREVIEWS_SIZE_VALIDATOR
213 },
214 captions: {
215 size: CACHE_CAPTIONS_SIZE_VALIDATOR
216 }
217 },
218 signup: {
219 enabled: null,
220 limit: SIGNUP_LIMIT_VALIDATOR,
221 requiresEmailVerification: null
222 },
223 import: {
224 videos: {
225 http: {
226 enabled: null
227 },
228 torrent: {
229 enabled: null
230 }
231 }
232 },
233 admin: {
234 email: ADMIN_EMAIL_VALIDATOR
235 },
236 contactForm: {
237 enabled: null
238 },
239 user: {
240 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
241 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
242 },
243 transcoding: {
244 enabled: null,
245 threads: TRANSCODING_THREADS_VALIDATOR,
246 allowAdditionalExtensions: null,
247 allowAudioFiles: null,
248 resolutions: {},
249 hls: {
250 enabled: null
251 },
252 webtorrent: {
253 enabled: null
254 }
255 },
256 live: {
257 enabled: null,
258
259 maxDuration: null,
260 maxInstanceLives: null,
261 maxUserLives: null,
262 allowReplay: null,
263
264 transcoding: {
265 enabled: null,
266 threads: TRANSCODING_THREADS_VALIDATOR,
267 resolutions: {}
268 }
269 },
270 autoBlacklist: {
271 videos: {
272 ofUsers: {
273 enabled: null
274 }
275 }
276 },
277 followers: {
278 instance: {
279 enabled: null,
280 manualApproval: null
281 }
282 },
283 followings: {
284 instance: {
285 autoFollowBack: {
286 enabled: null
287 },
288 autoFollowIndex: {
289 enabled: null,
290 indexUrl: INDEX_URL_VALIDATOR
291 }
292 }
293 },
294 broadcastMessage: {
295 enabled: null,
296 level: null,
297 dismissable: null,
298 message: null
299 },
300 search: {
301 remoteUri: {
302 users: null,
303 anonymous: null
304 },
305 searchIndex: {
306 enabled: null,
307 url: SEARCH_INDEX_URL_VALIDATOR,
308 disableLocalSearch: null,
309 isDefaultSearch: null
310 }
311 }
312 }
313
314 const defaultValues = {
315 transcoding: {
316 resolutions: {}
317 },
318 live: {
319 transcoding: {
320 resolutions: {}
321 }
322 }
323 }
324
325 for (const resolution of this.resolutions) {
326 defaultValues.transcoding.resolutions[resolution.id] = 'false'
327 formGroupData.transcoding.resolutions[resolution.id] = null
328 }
329
330 for (const resolution of this.liveResolutions) {
331 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
332 formGroupData.live.transcoding.resolutions[resolution.id] = null
333 }
334
335 this.buildForm(formGroupData)
336 this.loadForm()
337
338 this.checkTranscodingFields()
339 this.checkSignupField()
340 }
341
342 ngAfterViewChecked () {
343 if (!this.initDone) {
344 this.initDone = true
345 this.gotoAnchor()
346 }
347 }
348
349 isTranscodingEnabled () {
350 return this.form.value['transcoding']['enabled'] === true
351 }
352
353 isLiveEnabled () {
354 return this.form.value['live']['enabled'] === true
355 }
356
357 isLiveTranscodingEnabled () {
358 return this.form.value['live']['transcoding']['enabled'] === true
359 }
360
361 isSignupEnabled () {
362 return this.form.value['signup']['enabled'] === true
363 }
364
365 isSearchIndexEnabled () {
366 return this.form.value['search']['searchIndex']['enabled'] === true
367 }
368
369 isAutoFollowIndexEnabled () {
370 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
371 }
372
373 isTrendingHotEnabled () {
374 return this.form.value['instance']['pages']['hot']['enabled'] === true
375 }
376
377 async formValidated () {
378 const value: CustomConfig = this.form.getRawValue()
379
380 this.configService.updateCustomConfig(value)
381 .subscribe(
382 res => {
383 this.customConfig = res
384
385 // Reload general configuration
386 this.serverService.resetConfig()
387
388 this.updateForm()
389
390 this.notifier.success($localize`Configuration updated.`)
391 },
392
393 err => this.notifier.error(err.message)
394 )
395 }
396
397 gotoAnchor () {
398 const hashToNav = {
399 'customizations': 'advanced-configuration'
400 }
401 const hash = window.location.hash.replace('#', '')
402
403 if (hash && Object.keys(hashToNav).includes(hash)) {
404 this.nav.select(hashToNav[hash])
405 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
406 }
407 }
408
409 hasConsistentOptions () {
410 if (this.hasLiveAllowReplayConsistentOptions()) return true
411
412 return false
413 }
414
415 hasLiveAllowReplayConsistentOptions () {
416 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
417 return false
418 }
419
420 return true
421 }
422
423 private updateForm () {
424 this.form.patchValue(this.customConfig)
425 }
426
427 private loadForm () {
428 forkJoin([
429 this.configService.getCustomConfig(),
430 this.serverService.getVideoLanguages(),
431 this.serverService.getVideoCategories()
432 ]).subscribe(
433 ([ config, languages, categories ]) => {
434 this.customConfig = config
435
436 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
437 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
438
439 this.updateForm()
440 // Force form validation
441 this.forceCheck()
442 },
443
444 err => this.notifier.error(err.message)
445 )
446 }
447
448 private checkTranscodingFields () {
449 const hlsControl = this.form.get('transcoding.hls.enabled')
450 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
451
452 webtorrentControl.valueChanges
453 .subscribe(newValue => {
454 if (newValue === false && !hlsControl.disabled) {
455 hlsControl.disable()
456 }
457
458 if (newValue === true && !hlsControl.enabled) {
459 hlsControl.enable()
460 }
461 })
462
463 hlsControl.valueChanges
464 .subscribe(newValue => {
465 if (newValue === false && !webtorrentControl.disabled) {
466 webtorrentControl.disable()
467 }
468
469 if (newValue === true && !webtorrentControl.enabled) {
470 webtorrentControl.enable()
471 }
472 })
473 }
474
475 private checkSignupField () {
476 const signupControl = this.form.get('signup.enabled')
477
478 signupControl.valueChanges
479 .pipe(pairwise())
480 .subscribe(([ oldValue, newValue ]) => {
481 if (oldValue !== true && newValue === true) {
482 // tslint:disable:max-line-length
483 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
484
485 this.form.patchValue({
486 autoBlacklist: {
487 videos: {
488 ofUsers: {
489 enabled: true
490 }
491 }
492 }
493 })
494 }
495 })
496 }
497 }