]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Better changelog for a better world
[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 videoChannelSynchronization: {
149 enabled: null
150 }
151 },
152 trending: {
153 videos: {
154 algorithms: {
155 enabled: null,
156 default: null
157 }
158 }
159 },
160 admin: {
161 email: ADMIN_EMAIL_VALIDATOR
162 },
163 contactForm: {
164 enabled: null
165 },
166 user: {
167 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
168 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
169 },
170 videoChannels: {
171 maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR
172 },
173 transcoding: {
174 enabled: null,
175 threads: TRANSCODING_THREADS_VALIDATOR,
176 allowAdditionalExtensions: null,
177 allowAudioFiles: null,
178 profile: null,
179 concurrency: CONCURRENCY_VALIDATOR,
180 resolutions: {},
181 alwaysTranscodeOriginalResolution: null,
182 hls: {
183 enabled: null
184 },
185 webtorrent: {
186 enabled: null
187 }
188 },
189 live: {
190 enabled: null,
191
192 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
193 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
194 maxUserLives: MAX_USER_LIVES_VALIDATOR,
195 allowReplay: null,
196 latencySetting: {
197 enabled: null
198 },
199
200 transcoding: {
201 enabled: null,
202 threads: TRANSCODING_THREADS_VALIDATOR,
203 profile: null,
204 resolutions: {},
205 alwaysTranscodeOriginalResolution: null
206 }
207 },
208 videoStudio: {
209 enabled: null
210 },
211 autoBlacklist: {
212 videos: {
213 ofUsers: {
214 enabled: null
215 }
216 }
217 },
218 followers: {
219 instance: {
220 enabled: null,
221 manualApproval: null
222 }
223 },
224 followings: {
225 instance: {
226 autoFollowBack: {
227 enabled: null
228 },
229 autoFollowIndex: {
230 enabled: null,
231 indexUrl: INDEX_URL_VALIDATOR
232 }
233 }
234 },
235 broadcastMessage: {
236 enabled: null,
237 level: null,
238 dismissable: null,
239 message: null
240 },
241 search: {
242 remoteUri: {
243 users: null,
244 anonymous: null
245 },
246 searchIndex: {
247 enabled: null,
248 url: SEARCH_INDEX_URL_VALIDATOR,
249 disableLocalSearch: null,
250 isDefaultSearch: null
251 }
252 },
253
254 instanceCustomHomepage: {
255 content: null
256 }
257 }
258
259 const defaultValues = {
260 transcoding: {
261 resolutions: {}
262 },
263 live: {
264 transcoding: {
265 resolutions: {}
266 }
267 }
268 }
269
270 for (const resolution of this.editConfigurationService.getVODResolutions()) {
271 defaultValues.transcoding.resolutions[resolution.id] = 'false'
272 formGroupData.transcoding.resolutions[resolution.id] = null
273 }
274
275 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
276 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
277 formGroupData.live.transcoding.resolutions[resolution.id] = null
278 }
279
280 this.buildForm(formGroupData)
281
282 if (this.route.snapshot.fragment) {
283 this.onNavChange(this.route.snapshot.fragment)
284 }
285
286 this.loadConfigAndUpdateForm()
287 this.loadCategoriesAndLanguages()
288
289 if (!this.isUpdateAllowed()) {
290 this.form.disable()
291 }
292 }
293
294 formValidated () {
295 const value: ComponentCustomConfig = this.form.getRawValue()
296
297 forkJoin([
298 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
299 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
300 ])
301 .subscribe({
302 next: ([ resConfig ]) => {
303 const instanceCustomHomepage = {
304 content: value.instanceCustomHomepage.content
305 }
306
307 this.customConfig = { ...resConfig, instanceCustomHomepage }
308
309 // Reload general configuration
310 this.serverService.resetConfig()
311 .subscribe(config => {
312 this.serverConfig = config
313 })
314
315 this.updateForm()
316
317 this.notifier.success($localize`Configuration updated.`)
318 },
319
320 error: err => this.notifier.error(err.message)
321 })
322 }
323
324 isUpdateAllowed () {
325 return this.serverConfig.webadmin.configuration.edition.allowed === true
326 }
327
328 hasConsistentOptions () {
329 if (this.hasLiveAllowReplayConsistentOptions()) return true
330
331 return false
332 }
333
334 hasLiveAllowReplayConsistentOptions () {
335 if (
336 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
337 this.editConfigurationService.isLiveEnabled(this.form) &&
338 this.form.value['live']['allowReplay'] === true
339 ) {
340 return false
341 }
342
343 return true
344 }
345
346 onNavChange (newActiveNav: string) {
347 this.activeNav = newActiveNav
348
349 this.router.navigate([], { fragment: this.activeNav })
350 }
351
352 grabAllErrors (errorObjectArg?: any) {
353 const errorObject = errorObjectArg || this.formErrors
354
355 let acc: string[] = []
356
357 for (const key of Object.keys(errorObject)) {
358 const value = errorObject[key]
359 if (!value) continue
360
361 if (typeof value === 'string') {
362 acc.push(value)
363 } else {
364 acc = acc.concat(this.grabAllErrors(value))
365 }
366 }
367
368 return acc
369 }
370
371 private updateForm () {
372 this.form.patchValue(this.customConfig)
373 }
374
375 private loadConfigAndUpdateForm () {
376 forkJoin([
377 this.configService.getCustomConfig(),
378 this.customPage.getInstanceHomepage()
379 ]).subscribe({
380 next: ([ config, homepage ]) => {
381 this.customConfig = { ...config, instanceCustomHomepage: homepage }
382
383 this.updateForm()
384 // Force form validation
385 this.forceCheck()
386 },
387
388 error: err => this.notifier.error(err.message)
389 })
390 }
391
392 private loadCategoriesAndLanguages () {
393 forkJoin([
394 this.serverService.getVideoLanguages(),
395 this.serverService.getVideoCategories()
396 ]).subscribe({
397 next: ([ languages, categories ]) => {
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
402 error: err => this.notifier.error(err.message)
403 })
404 }
405 }