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