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