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