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