]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
ce01f8b596d56916a9f71aaa831538401c4094b7
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1
2 import omit from 'lodash-es/omit'
3 import { forkJoin } from 'rxjs'
4 import { SelectOptionsItem } from 'src/types/select-options-item.model'
5 import { Component, OnInit } from '@angular/core'
6 import { ActivatedRoute, Router } from '@angular/router'
7 import { ConfigService } from '@app/+admin/config/shared/config.service'
8 import { Notifier } from '@app/core'
9 import { ServerService } from '@app/core/server/server.service'
10 import {
11 ADMIN_EMAIL_VALIDATOR,
12 CACHE_CAPTIONS_SIZE_VALIDATOR,
13 CACHE_PREVIEWS_SIZE_VALIDATOR,
14 CONCURRENCY_VALIDATOR,
15 INDEX_URL_VALIDATOR,
16 INSTANCE_NAME_VALIDATOR,
17 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
18 MAX_INSTANCE_LIVES_VALIDATOR,
19 MAX_LIVE_DURATION_VALIDATOR,
20 MAX_USER_LIVES_VALIDATOR,
21 SEARCH_INDEX_URL_VALIDATOR,
22 SERVICES_TWITTER_USERNAME_VALIDATOR,
23 SIGNUP_LIMIT_VALIDATOR,
24 SIGNUP_MINIMUM_AGE_VALIDATOR,
25 TRANSCODING_THREADS_VALIDATOR,
26 MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR
27 } from '@app/shared/form-validators/custom-config-validators'
28 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
29 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
30 import { CustomPageService } from '@app/shared/shared-main/custom-page'
31 import { CustomConfig, CustomPage, HTMLServerConfig } from '@shared/models'
32 import { EditConfigurationService } from './edit-configuration.service'
33
34 type ComponentCustomConfig = CustomConfig & {
35 instanceCustomHomepage: CustomPage
36 }
37
38 @Component({
39 selector: 'my-edit-custom-config',
40 templateUrl: './edit-custom-config.component.html',
41 styleUrls: [ './edit-custom-config.component.scss' ]
42 })
43 export class EditCustomConfigComponent extends FormReactive implements OnInit {
44 activeNav: string
45
46 customConfig: ComponentCustomConfig
47 serverConfig: HTMLServerConfig
48
49 homepage: CustomPage
50
51 languageItems: SelectOptionsItem[] = []
52 categoryItems: SelectOptionsItem[] = []
53
54 constructor (
55 private router: Router,
56 private route: ActivatedRoute,
57 protected formValidatorService: FormValidatorService,
58 private notifier: Notifier,
59 private configService: ConfigService,
60 private customPage: CustomPageService,
61 private serverService: ServerService,
62 private editConfigurationService: EditConfigurationService
63 ) {
64 super()
65 }
66
67 ngOnInit () {
68 this.serverConfig = this.serverService.getHTMLConfig()
69
70 const formGroupData: { [key in keyof ComponentCustomConfig ]: any } = {
71 instance: {
72 name: INSTANCE_NAME_VALIDATOR,
73 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
74 description: null,
75
76 isNSFW: false,
77 defaultNSFWPolicy: null,
78
79 terms: null,
80 codeOfConduct: null,
81
82 creationReason: null,
83 moderationInformation: null,
84 administrator: null,
85 maintenanceLifetime: null,
86 businessModel: null,
87
88 hardwareInformation: null,
89
90 categories: null,
91 languages: null,
92
93 defaultClientRoute: null,
94
95 customizations: {
96 javascript: null,
97 css: null
98 }
99 },
100 theme: {
101 default: null
102 },
103 services: {
104 twitter: {
105 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
106 whitelisted: null
107 }
108 },
109 client: {
110 videos: {
111 miniature: {
112 preferAuthorDisplayName: null
113 }
114 },
115 menu: {
116 login: {
117 redirectOnSingleExternalAuth: null
118 }
119 }
120 },
121 cache: {
122 previews: {
123 size: CACHE_PREVIEWS_SIZE_VALIDATOR
124 },
125 captions: {
126 size: CACHE_CAPTIONS_SIZE_VALIDATOR
127 },
128 torrents: {
129 size: CACHE_CAPTIONS_SIZE_VALIDATOR
130 }
131 },
132 signup: {
133 enabled: null,
134 limit: SIGNUP_LIMIT_VALIDATOR,
135 requiresEmailVerification: null,
136 minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR
137 },
138 import: {
139 videos: {
140 concurrency: CONCURRENCY_VALIDATOR,
141 http: {
142 enabled: null
143 },
144 torrent: {
145 enabled: null
146 }
147 }
148 },
149 trending: {
150 videos: {
151 algorithms: {
152 enabled: null,
153 default: null
154 }
155 }
156 },
157 admin: {
158 email: ADMIN_EMAIL_VALIDATOR
159 },
160 contactForm: {
161 enabled: null
162 },
163 user: {
164 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
165 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
166 },
167 videoChannels: {
168 maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR
169 },
170 transcoding: {
171 enabled: null,
172 threads: TRANSCODING_THREADS_VALIDATOR,
173 allowAdditionalExtensions: null,
174 allowAudioFiles: null,
175 profile: null,
176 concurrency: CONCURRENCY_VALIDATOR,
177 resolutions: {},
178 alwaysTranscodeOriginalResolution: null,
179 hls: {
180 enabled: null
181 },
182 webtorrent: {
183 enabled: null
184 }
185 },
186 live: {
187 enabled: null,
188
189 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
190 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
191 maxUserLives: MAX_USER_LIVES_VALIDATOR,
192 allowReplay: null,
193 latencySetting: {
194 enabled: null
195 },
196
197 transcoding: {
198 enabled: null,
199 threads: TRANSCODING_THREADS_VALIDATOR,
200 profile: null,
201 resolutions: {},
202 alwaysTranscodeOriginalResolution: null
203 }
204 },
205 videoStudio: {
206 enabled: null
207 },
208 autoBlacklist: {
209 videos: {
210 ofUsers: {
211 enabled: null
212 }
213 }
214 },
215 followers: {
216 instance: {
217 enabled: null,
218 manualApproval: null
219 }
220 },
221 followings: {
222 instance: {
223 autoFollowBack: {
224 enabled: null
225 },
226 autoFollowIndex: {
227 enabled: null,
228 indexUrl: INDEX_URL_VALIDATOR
229 }
230 }
231 },
232 broadcastMessage: {
233 enabled: null,
234 level: null,
235 dismissable: null,
236 message: null
237 },
238 search: {
239 remoteUri: {
240 users: null,
241 anonymous: null
242 },
243 searchIndex: {
244 enabled: null,
245 url: SEARCH_INDEX_URL_VALIDATOR,
246 disableLocalSearch: null,
247 isDefaultSearch: null
248 }
249 },
250
251 instanceCustomHomepage: {
252 content: null
253 }
254 }
255
256 const defaultValues = {
257 transcoding: {
258 resolutions: {}
259 },
260 live: {
261 transcoding: {
262 resolutions: {}
263 }
264 }
265 }
266
267 for (const resolution of this.editConfigurationService.getVODResolutions()) {
268 defaultValues.transcoding.resolutions[resolution.id] = 'false'
269 formGroupData.transcoding.resolutions[resolution.id] = null
270 }
271
272 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
273 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
274 formGroupData.live.transcoding.resolutions[resolution.id] = null
275 }
276
277 this.buildForm(formGroupData)
278
279 if (this.route.snapshot.fragment) {
280 this.onNavChange(this.route.snapshot.fragment)
281 }
282
283 this.loadConfigAndUpdateForm()
284 this.loadCategoriesAndLanguages()
285
286 if (!this.isUpdateAllowed()) {
287 this.form.disable()
288 }
289 }
290
291 formValidated () {
292 const value: ComponentCustomConfig = this.form.getRawValue()
293
294 forkJoin([
295 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
296 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
297 ])
298 .subscribe({
299 next: ([ resConfig ]) => {
300 const instanceCustomHomepage = {
301 content: value.instanceCustomHomepage.content
302 }
303
304 this.customConfig = { ...resConfig, instanceCustomHomepage }
305
306 // Reload general configuration
307 this.serverService.resetConfig()
308 .subscribe(config => {
309 this.serverConfig = config
310 })
311
312 this.updateForm()
313
314 this.notifier.success($localize`Configuration updated.`)
315 },
316
317 error: err => this.notifier.error(err.message)
318 })
319 }
320
321 isUpdateAllowed () {
322 return this.serverConfig.webadmin.configuration.edition.allowed === true
323 }
324
325 hasConsistentOptions () {
326 if (this.hasLiveAllowReplayConsistentOptions()) return true
327
328 return false
329 }
330
331 hasLiveAllowReplayConsistentOptions () {
332 if (
333 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
334 this.editConfigurationService.isLiveEnabled(this.form) &&
335 this.form.value['live']['allowReplay'] === true
336 ) {
337 return false
338 }
339
340 return true
341 }
342
343 onNavChange (newActiveNav: string) {
344 this.activeNav = newActiveNav
345
346 this.router.navigate([], { fragment: this.activeNav })
347 }
348
349 grabAllErrors (errorObjectArg?: any) {
350 const errorObject = errorObjectArg || this.formErrors
351
352 let acc: string[] = []
353
354 for (const key of Object.keys(errorObject)) {
355 const value = errorObject[key]
356 if (!value) continue
357
358 if (typeof value === 'string') {
359 acc.push(value)
360 } else {
361 acc = acc.concat(this.grabAllErrors(value))
362 }
363 }
364
365 return acc
366 }
367
368 private updateForm () {
369 this.form.patchValue(this.customConfig)
370 }
371
372 private loadConfigAndUpdateForm () {
373 forkJoin([
374 this.configService.getCustomConfig(),
375 this.customPage.getInstanceHomepage()
376 ]).subscribe({
377 next: ([ config, homepage ]) => {
378 this.customConfig = { ...config, instanceCustomHomepage: homepage }
379
380 this.updateForm()
381 // Force form validation
382 this.forceCheck()
383 },
384
385 error: err => this.notifier.error(err.message)
386 })
387 }
388
389 private loadCategoriesAndLanguages () {
390 forkJoin([
391 this.serverService.getVideoLanguages(),
392 this.serverService.getVideoCategories()
393 ]).subscribe({
394 next: ([ languages, categories ]) => {
395 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
396 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
397 },
398
399 error: err => this.notifier.error(err.message)
400 })
401 }
402 }