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