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