]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
only count comments from people other than the video author
[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 83 },
b7085c71
RK
84 {
85 id: '1440p',
86 label: $localize`1440p`
87 },
00aa1f0d
C
88 {
89 id: '2160p',
66357162 90 label: $localize`2160p`
00aa1f0d 91 }
3827c3b3
C
92 ]
93
c6c0fa6c
C
94 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
95
3827c3b3 96 this.transcodingThreadOptions = [
66357162 97 { value: 0, label: $localize`Auto (via ffmpeg)` },
3827c3b3
C
98 { value: 1, label: '1' },
99 { value: 2, label: '2' },
100 { value: 4, label: '4' },
101 { value: 8, label: '8' }
102 ]
fb719404
C
103
104 this.liveMaxDurationOptions = [
c9bc850e 105 { value: -1, label: $localize`No limit` },
fb719404
C
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 ]
fd206f0b
C
111 }
112
41a676db 113 get videoQuotaOptions () {
3827c3b3 114 return this.configService.videoQuotaOptions
41a676db
C
115 }
116
117 get videoQuotaDailyOptions () {
3827c3b3 118 return this.configService.videoQuotaDailyOptions
41a676db
C
119 }
120
7cd4d2ba 121 get availableThemes () {
ba430d75 122 return this.serverConfig.theme.registered
ffb321be 123 .map(t => t.name)
7cd4d2ba
C
124 }
125
4f20856e
C
126 get liveRTMPPort () {
127 return this.serverConfig.live.rtmp.port
128 }
129
ea5cdc11
RK
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
fd206f0b 154 getResolutionKey (resolution: string) {
3866f1a0 155 return 'transcoding.resolutions.' + resolution
fd206f0b
C
156 }
157
d18d6478 158 ngOnInit () {
ba430d75
C
159 this.serverConfig = this.serverService.getTmpConfig()
160 this.serverService.getConfig()
fb719404
C
161 .subscribe(config => {
162 this.serverConfig = config
163 })
ba430d75 164
3866f1a0
C
165 const formGroupData: { [key in keyof CustomConfig ]: any } = {
166 instance: {
7ed1edbb
C
167 name: INSTANCE_NAME_VALIDATOR,
168 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
3866f1a0 169 description: null,
ccc00cb2 170
f8802489 171 isNSFW: false,
3866f1a0 172 defaultNSFWPolicy: null,
ccc00cb2
C
173
174 terms: null,
175 codeOfConduct: null,
8ae03c37
C
176
177 creationReason: null,
ccc00cb2
C
178 moderationInformation: null,
179 administrator: null,
180 maintenanceLifetime: null,
181 businessModel: null,
182
be04c6fd
C
183 hardwareInformation: null,
184
ccc00cb2
C
185 categories: null,
186 languages: null,
187
188 defaultClientRoute: null,
189
3866f1a0
C
190 customizations: {
191 javascript: null,
192 css: null
193 }
194 },
7cd4d2ba
C
195 theme: {
196 default: null
197 },
3866f1a0
C
198 services: {
199 twitter: {
7ed1edbb 200 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
3866f1a0
C
201 whitelisted: null
202 }
203 },
204 cache: {
205 previews: {
7ed1edbb 206 size: CACHE_PREVIEWS_SIZE_VALIDATOR
3866f1a0
C
207 },
208 captions: {
7ed1edbb 209 size: CACHE_CAPTIONS_SIZE_VALIDATOR
3866f1a0
C
210 }
211 },
212 signup: {
213 enabled: null,
7ed1edbb 214 limit: SIGNUP_LIMIT_VALIDATOR,
3866f1a0
C
215 requiresEmailVerification: null
216 },
217 import: {
218 videos: {
219 http: {
220 enabled: null
221 },
222 torrent: {
223 enabled: null
224 }
225 }
226 },
227 admin: {
7ed1edbb 228 email: ADMIN_EMAIL_VALIDATOR
3866f1a0
C
229 },
230 contactForm: {
231 enabled: null
232 },
233 user: {
7ed1edbb
C
234 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
235 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
3866f1a0
C
236 },
237 transcoding: {
238 enabled: null,
7ed1edbb 239 threads: TRANSCODING_THREADS_VALIDATOR,
3866f1a0 240 allowAdditionalExtensions: null,
536598cf 241 allowAudioFiles: null,
5d9e4eaa
C
242 resolutions: {},
243 hls: {
244 enabled: null
5a71acd2
C
245 },
246 webtorrent: {
247 enabled: null
5d9e4eaa 248 }
7ccddd7b 249 },
c6c0fa6c
C
250 live: {
251 enabled: null,
252
fb719404 253 maxDuration: null,
a056ca48
C
254 maxInstanceLives: null,
255 maxUserLives: null,
fb719404
C
256 allowReplay: null,
257
c6c0fa6c
C
258 transcoding: {
259 enabled: null,
260 threads: TRANSCODING_THREADS_VALIDATOR,
261 resolutions: {}
262 }
263 },
7ccddd7b
JM
264 autoBlacklist: {
265 videos: {
266 ofUsers: {
267 enabled: null
268 }
269 }
0dc64777
C
270 },
271 followers: {
272 instance: {
273 enabled: null,
274 manualApproval: null
275 }
e1b49ee5
C
276 },
277 followings: {
278 instance: {
279 autoFollowBack: {
280 enabled: null
281 },
282 autoFollowIndex: {
283 enabled: null,
7ed1edbb 284 indexUrl: INDEX_URL_VALIDATOR
e1b49ee5
C
285 }
286 }
72c33e71
C
287 },
288 broadcastMessage: {
289 enabled: null,
290 level: null,
291 dismissable: null,
292 message: null
5fb2e288
C
293 },
294 search: {
295 remoteUri: {
296 users: null,
297 anonymous: null
298 },
299 searchIndex: {
300 enabled: null,
7ed1edbb 301 url: SEARCH_INDEX_URL_VALIDATOR,
5fb2e288
C
302 disableLocalSearch: null,
303 isDefaultSearch: null
304 }
3866f1a0 305 }
fd206f0b
C
306 }
307
3866f1a0
C
308 const defaultValues = {
309 transcoding: {
310 resolutions: {}
c6c0fa6c
C
311 },
312 live: {
313 transcoding: {
314 resolutions: {}
315 }
3866f1a0
C
316 }
317 }
c6c0fa6c 318
fd206f0b 319 for (const resolution of this.resolutions) {
00aa1f0d
C
320 defaultValues.transcoding.resolutions[resolution.id] = 'false'
321 formGroupData.transcoding.resolutions[resolution.id] = null
fd206f0b
C
322 }
323
c6c0fa6c
C
324 for (const resolution of this.liveResolutions) {
325 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
326 formGroupData.live.transcoding.resolutions[resolution.id] = null
327 }
328
d18d6478 329 this.buildForm(formGroupData)
04cda1d7 330 this.loadForm()
16a173bb 331
04cda1d7 332 this.checkTranscodingFields()
16a173bb 333 this.checkSignupField()
fd206f0b
C
334 }
335
45e0d669
RK
336 ngAfterViewChecked () {
337 if (!this.initDone) {
338 this.initDone = true
339 this.gotoAnchor()
340 }
341 }
342
fd206f0b 343 isTranscodingEnabled () {
3866f1a0 344 return this.form.value['transcoding']['enabled'] === true
fd206f0b
C
345 }
346
c6c0fa6c
C
347 isLiveEnabled () {
348 return this.form.value['live']['enabled'] === true
349 }
350
351 isLiveTranscodingEnabled () {
352 return this.form.value['live']['transcoding']['enabled'] === true
353 }
354
fd206f0b 355 isSignupEnabled () {
3866f1a0 356 return this.form.value['signup']['enabled'] === true
fd206f0b
C
357 }
358
5fb2e288
C
359 isSearchIndexEnabled () {
360 return this.form.value['search']['searchIndex']['enabled'] === true
361 }
362
4ee6a8b1
C
363 isAutoFollowIndexEnabled () {
364 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
365 }
366
1f30a185 367 async formValidated () {
210856a7
C
368 const value: CustomConfig = this.form.getRawValue()
369
210856a7 370 this.configService.updateCustomConfig(value)
fd206f0b
C
371 .subscribe(
372 res => {
373 this.customConfig = res
374
375 // Reload general configuration
ba430d75 376 this.serverService.resetConfig()
fd206f0b
C
377
378 this.updateForm()
66b16caf 379
66357162 380 this.notifier.success($localize`Configuration updated.`)
fd206f0b
C
381 },
382
f8b2c1b4 383 err => this.notifier.error(err.message)
fd206f0b
C
384 )
385 }
386
45e0d669 387 gotoAnchor () {
45c6bcf3 388 const hashToNav = {
45e0d669
RK
389 'customizations': 'advanced-configuration'
390 }
391 const hash = window.location.hash.replace('#', '')
392
45c6bcf3
C
393 if (hash && Object.keys(hashToNav).includes(hash)) {
394 this.nav.select(hashToNav[hash])
45e0d669
RK
395 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
396 }
397 }
398
fb719404
C
399 hasConsistentOptions () {
400 if (this.hasLiveAllowReplayConsistentOptions()) return true
401
402 return false
403 }
404
405 hasLiveAllowReplayConsistentOptions () {
406 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
407 return false
408 }
409
410 return true
411 }
412
fd206f0b 413 private updateForm () {
3866f1a0 414 this.form.patchValue(this.customConfig)
fd206f0b 415 }
04cda1d7
C
416
417 private loadForm () {
418 forkJoin([
419 this.configService.getCustomConfig(),
420 this.serverService.getVideoLanguages(),
421 this.serverService.getVideoCategories()
422 ]).subscribe(
423 ([ config, languages, categories ]) => {
424 this.customConfig = config
425
52c4976f
C
426 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
427 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
04cda1d7
C
428
429 this.updateForm()
430 // Force form validation
431 this.forceCheck()
432 },
433
434 err => this.notifier.error(err.message)
435 )
436 }
437
438 private checkTranscodingFields () {
439 const hlsControl = this.form.get('transcoding.hls.enabled')
440 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
441
442 webtorrentControl.valueChanges
443 .subscribe(newValue => {
444 if (newValue === false && !hlsControl.disabled) {
445 hlsControl.disable()
446 }
447
448 if (newValue === true && !hlsControl.enabled) {
449 hlsControl.enable()
450 }
451 })
452
453 hlsControl.valueChanges
454 .subscribe(newValue => {
455 if (newValue === false && !webtorrentControl.disabled) {
456 webtorrentControl.disable()
457 }
458
459 if (newValue === true && !webtorrentControl.enabled) {
460 webtorrentControl.enable()
461 }
462 })
463 }
16a173bb
C
464
465 private checkSignupField () {
466 const signupControl = this.form.get('signup.enabled')
467
468 signupControl.valueChanges
469 .pipe(pairwise())
470 .subscribe(([ oldValue, newValue ]) => {
471 if (oldValue !== true && newValue === true) {
472 // tslint:disable:max-line-length
473 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
474
475 this.form.patchValue({
476 autoBlacklist: {
477 videos: {
478 ofUsers: {
479 enabled: true
480 }
481 }
482 }
483 })
484 }
485 })
486 }
fd206f0b 487}