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