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