]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Instance homepage support (#4007)
[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 TRANSCODING_THREADS_VALIDATOR
25 } from '@app/shared/form-validators/custom-config-validators'
26 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
27 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
28 import { CustomPageService } from '@app/shared/shared-main/custom-page'
29 import { CustomConfig, CustomPage, ServerConfig } from '@shared/models'
30 import { EditConfigurationService } from './edit-configuration.service'
31
32 type ComponentCustomConfig = CustomConfig & {
33 instanceCustomHomepage: CustomPage
34 }
35
36 @Component({
37 selector: 'my-edit-custom-config',
38 templateUrl: './edit-custom-config.component.html',
39 styleUrls: [ './edit-custom-config.component.scss' ]
40 })
41 export class EditCustomConfigComponent extends FormReactive implements OnInit {
42 activeNav: string
43
44 customConfig: ComponentCustomConfig
45 serverConfig: ServerConfig
46
47 homepage: CustomPage
48
49 languageItems: SelectOptionsItem[] = []
50 categoryItems: SelectOptionsItem[] = []
51
52 constructor (
53 private router: Router,
54 private route: ActivatedRoute,
55 protected formValidatorService: FormValidatorService,
56 private notifier: Notifier,
57 private configService: ConfigService,
58 private customPage: CustomPageService,
59 private serverService: ServerService,
60 private editConfigurationService: EditConfigurationService
61 ) {
62 super()
63 }
64
65 ngOnInit () {
66 this.serverConfig = this.serverService.getTmpConfig()
67 this.serverService.getConfig()
68 .subscribe(config => this.serverConfig = config)
69
70 const formGroupData: { [key in keyof ComponentCustomConfig ]: any } = {
71 instance: {
72 name: INSTANCE_NAME_VALIDATOR,
73 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
74 description: null,
75
76 isNSFW: false,
77 defaultNSFWPolicy: null,
78
79 terms: null,
80 codeOfConduct: null,
81
82 creationReason: null,
83 moderationInformation: null,
84 administrator: null,
85 maintenanceLifetime: null,
86 businessModel: null,
87
88 hardwareInformation: null,
89
90 categories: null,
91 languages: null,
92
93 defaultClientRoute: null,
94
95 customizations: {
96 javascript: null,
97 css: null
98 }
99 },
100 theme: {
101 default: null
102 },
103 services: {
104 twitter: {
105 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
106 whitelisted: null
107 }
108 },
109 cache: {
110 previews: {
111 size: CACHE_PREVIEWS_SIZE_VALIDATOR
112 },
113 captions: {
114 size: CACHE_CAPTIONS_SIZE_VALIDATOR
115 },
116 torrents: {
117 size: CACHE_CAPTIONS_SIZE_VALIDATOR
118 }
119 },
120 signup: {
121 enabled: null,
122 limit: SIGNUP_LIMIT_VALIDATOR,
123 requiresEmailVerification: null
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
263 async formValidated () {
264 const value: ComponentCustomConfig = this.form.getRawValue()
265
266 forkJoin([
267 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
268 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
269 ])
270 .subscribe(
271 ([ resConfig ]) => {
272 const instanceCustomHomepage = {
273 content: value.instanceCustomHomepage.content
274 }
275
276 this.customConfig = { ...resConfig, instanceCustomHomepage }
277
278 // Reload general configuration
279 this.serverService.resetConfig()
280 .subscribe(config => this.serverConfig = config)
281
282 this.updateForm()
283
284 this.notifier.success($localize`Configuration updated.`)
285 },
286
287 err => this.notifier.error(err.message)
288 )
289 }
290
291 hasConsistentOptions () {
292 if (this.hasLiveAllowReplayConsistentOptions()) return true
293
294 return false
295 }
296
297 hasLiveAllowReplayConsistentOptions () {
298 if (
299 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
300 this.editConfigurationService.isLiveEnabled(this.form) &&
301 this.form.value['live']['allowReplay'] === true
302 ) {
303 return false
304 }
305
306 return true
307 }
308
309 onNavChange (newActiveNav: string) {
310 this.activeNav = newActiveNav
311
312 this.router.navigate([], { fragment: this.activeNav })
313 }
314
315 grabAllErrors (errorObjectArg?: any) {
316 const errorObject = errorObjectArg || this.formErrors
317
318 let acc: string[] = []
319
320 for (const key of Object.keys(errorObject)) {
321 const value = errorObject[key]
322 if (!value) continue
323
324 if (typeof value === 'string') {
325 acc.push(value)
326 } else {
327 acc = acc.concat(this.grabAllErrors(value))
328 }
329 }
330
331 return acc
332 }
333
334 private updateForm () {
335 this.form.patchValue(this.customConfig)
336 }
337
338 private loadConfigAndUpdateForm () {
339 forkJoin([
340 this.configService.getCustomConfig(),
341 this.customPage.getInstanceHomepage()
342 ])
343 .subscribe(([ config, homepage ]) => {
344 this.customConfig = { ...config, instanceCustomHomepage: homepage }
345
346 this.updateForm()
347 // Force form validation
348 this.forceCheck()
349 },
350
351 err => this.notifier.error(err.message)
352 )
353 }
354
355 private loadCategoriesAndLanguages () {
356 forkJoin([
357 this.serverService.getVideoLanguages(),
358 this.serverService.getVideoCategories()
359 ]).subscribe(
360 ([ languages, categories ]) => {
361 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
362 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
363 },
364
365 err => this.notifier.error(err.message)
366 )
367 }
368 }