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