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