]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Channel sync (#5135)
[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,
7ed1edbb
C
21 SEARCH_INDEX_URL_VALIDATOR,
22 SERVICES_TWITTER_USERNAME_VALIDATOR,
23 SIGNUP_LIMIT_VALIDATOR,
1f256e7d 24 SIGNUP_MINIMUM_AGE_VALIDATOR,
754b6f5f
FC
25 TRANSCODING_THREADS_VALIDATOR,
26 MAX_VIDEO_CHANNELS_PER_USER_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'
21e493d4 29import { FormReactive, FormValidatorService } 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 (
53e4e201
C
55 private router: Router,
56 private route: ActivatedRoute,
d18d6478 57 protected formValidatorService: FormValidatorService,
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,
1f256e7d
P
135 requiresEmailVerification: null,
136 minimumAge: SIGNUP_MINIMUM_AGE_VALIDATOR
3866f1a0
C
137 },
138 import: {
139 videos: {
9129b769 140 concurrency: CONCURRENCY_VALIDATOR,
3866f1a0
C
141 http: {
142 enabled: null
143 },
144 torrent: {
145 enabled: null
146 }
2a491182
F
147 },
148 videoChannelSynchronization: {
149 enabled: null
3866f1a0
C
150 }
151 },
ba5d4a84
RK
152 trending: {
153 videos: {
154 algorithms: {
155 enabled: null,
156 default: null
157 }
158 }
159 },
3866f1a0 160 admin: {
7ed1edbb 161 email: ADMIN_EMAIL_VALIDATOR
3866f1a0
C
162 },
163 contactForm: {
164 enabled: null
165 },
166 user: {
7ed1edbb
C
167 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
168 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
3866f1a0 169 },
754b6f5f
FC
170 videoChannels: {
171 maxPerUser: MAX_VIDEO_CHANNELS_PER_USER_VALIDATOR
172 },
3866f1a0
C
173 transcoding: {
174 enabled: null,
7ed1edbb 175 threads: TRANSCODING_THREADS_VALIDATOR,
3866f1a0 176 allowAdditionalExtensions: null,
536598cf 177 allowAudioFiles: null,
80ac2e55 178 profile: null,
9129b769 179 concurrency: CONCURRENCY_VALIDATOR,
5d9e4eaa 180 resolutions: {},
84cae54e 181 alwaysTranscodeOriginalResolution: null,
5d9e4eaa
C
182 hls: {
183 enabled: null
5a71acd2
C
184 },
185 webtorrent: {
186 enabled: null
5d9e4eaa 187 }
7ccddd7b 188 },
c6c0fa6c
C
189 live: {
190 enabled: null,
191
4e55c132
C
192 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
193 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
194 maxUserLives: MAX_USER_LIVES_VALIDATOR,
fb719404 195 allowReplay: null,
f443a746
C
196 latencySetting: {
197 enabled: null
198 },
fb719404 199
c6c0fa6c
C
200 transcoding: {
201 enabled: null,
202 threads: TRANSCODING_THREADS_VALIDATOR,
80ac2e55 203 profile: null,
84cae54e
C
204 resolutions: {},
205 alwaysTranscodeOriginalResolution: null
c6c0fa6c
C
206 }
207 },
92e66e04 208 videoStudio: {
c729caf6
C
209 enabled: null
210 },
7ccddd7b
JM
211 autoBlacklist: {
212 videos: {
213 ofUsers: {
214 enabled: null
215 }
216 }
0dc64777
C
217 },
218 followers: {
219 instance: {
220 enabled: null,
221 manualApproval: null
222 }
e1b49ee5
C
223 },
224 followings: {
225 instance: {
226 autoFollowBack: {
227 enabled: null
228 },
229 autoFollowIndex: {
230 enabled: null,
7ed1edbb 231 indexUrl: INDEX_URL_VALIDATOR
e1b49ee5
C
232 }
233 }
72c33e71
C
234 },
235 broadcastMessage: {
236 enabled: null,
237 level: null,
238 dismissable: null,
239 message: null
5fb2e288
C
240 },
241 search: {
242 remoteUri: {
243 users: null,
244 anonymous: null
245 },
246 searchIndex: {
247 enabled: null,
7ed1edbb 248 url: SEARCH_INDEX_URL_VALIDATOR,
5fb2e288
C
249 disableLocalSearch: null,
250 isDefaultSearch: null
251 }
2539932e
C
252 },
253
254 instanceCustomHomepage: {
255 content: null
3866f1a0 256 }
fd206f0b
C
257 }
258
3866f1a0
C
259 const defaultValues = {
260 transcoding: {
261 resolutions: {}
c6c0fa6c
C
262 },
263 live: {
264 transcoding: {
265 resolutions: {}
266 }
3866f1a0
C
267 }
268 }
c6c0fa6c 269
5f46d28c 270 for (const resolution of this.editConfigurationService.getVODResolutions()) {
00aa1f0d
C
271 defaultValues.transcoding.resolutions[resolution.id] = 'false'
272 formGroupData.transcoding.resolutions[resolution.id] = null
fd206f0b
C
273 }
274
5f46d28c 275 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
c6c0fa6c
C
276 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
277 formGroupData.live.transcoding.resolutions[resolution.id] = null
278 }
279
d18d6478 280 this.buildForm(formGroupData)
16a173bb 281
5f46d28c
C
282 if (this.route.snapshot.fragment) {
283 this.onNavChange(this.route.snapshot.fragment)
45e0d669 284 }
5fb2e288 285
5f46d28c
C
286 this.loadConfigAndUpdateForm()
287 this.loadCategoriesAndLanguages()
cf0c8ee5
C
288
289 if (!this.isUpdateAllowed()) {
8d8a037e
JB
290 this.form.disable()
291 }
3da68f0a
RK
292 }
293
98ab5dc8 294 formValidated () {
2539932e 295 const value: ComponentCustomConfig = this.form.getRawValue()
210856a7 296
2539932e
C
297 forkJoin([
298 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
299 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
300 ])
1378c0d3
C
301 .subscribe({
302 next: ([ resConfig ]) => {
2539932e
C
303 const instanceCustomHomepage = {
304 content: value.instanceCustomHomepage.content
305 }
306
307 this.customConfig = { ...resConfig, instanceCustomHomepage }
fd206f0b
C
308
309 // Reload general configuration
ba430d75 310 this.serverService.resetConfig()
9df52d66
C
311 .subscribe(config => {
312 this.serverConfig = config
313 })
fd206f0b
C
314
315 this.updateForm()
66b16caf 316
66357162 317 this.notifier.success($localize`Configuration updated.`)
fd206f0b
C
318 },
319
1378c0d3
C
320 error: err => this.notifier.error(err.message)
321 })
fd206f0b
C
322 }
323
cf0c8ee5
C
324 isUpdateAllowed () {
325 return this.serverConfig.webadmin.configuration.edition.allowed === true
326 }
327
fb719404
C
328 hasConsistentOptions () {
329 if (this.hasLiveAllowReplayConsistentOptions()) return true
330
331 return false
332 }
333
334 hasLiveAllowReplayConsistentOptions () {
5f46d28c
C
335 if (
336 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
337 this.editConfigurationService.isLiveEnabled(this.form) &&
338 this.form.value['live']['allowReplay'] === true
339 ) {
fb719404
C
340 return false
341 }
342
343 return true
344 }
345
53e4e201
C
346 onNavChange (newActiveNav: string) {
347 this.activeNav = newActiveNav
348
349 this.router.navigate([], { fragment: this.activeNav })
350 }
351
45ba09fe
C
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
fd206f0b 371 private updateForm () {
3866f1a0 372 this.form.patchValue(this.customConfig)
fd206f0b 373 }
04cda1d7 374
5f46d28c 375 private loadConfigAndUpdateForm () {
2539932e
C
376 forkJoin([
377 this.configService.getCustomConfig(),
378 this.customPage.getInstanceHomepage()
1378c0d3
C
379 ]).subscribe({
380 next: ([ config, homepage ]) => {
2539932e 381 this.customConfig = { ...config, instanceCustomHomepage: homepage }
04cda1d7 382
04cda1d7
C
383 this.updateForm()
384 // Force form validation
385 this.forceCheck()
386 },
387
1378c0d3
C
388 error: err => this.notifier.error(err.message)
389 })
04cda1d7
C
390 }
391
5f46d28c
C
392 private loadCategoriesAndLanguages () {
393 forkJoin([
394 this.serverService.getVideoLanguages(),
395 this.serverService.getVideoCategories()
1378c0d3
C
396 ]).subscribe({
397 next: ([ languages, categories ]) => {
5f46d28c
C
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 },
16a173bb 401
1378c0d3
C
402 error: err => this.notifier.error(err.message)
403 })
16a173bb 404 }
fd206f0b 405}