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