]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
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,
24 TRANSCODING_THREADS_VALIDATOR
25} from '@app/shared/form-validators/custom-config-validators'
26import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
21e493d4 27import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
2539932e
C
28import { CustomPageService } from '@app/shared/shared-main/custom-page'
29import { CustomConfig, CustomPage, ServerConfig } from '@shared/models'
5f46d28c 30import { EditConfigurationService } from './edit-configuration.service'
fd206f0b 31
2539932e
C
32type ComponentCustomConfig = CustomConfig & {
33 instanceCustomHomepage: CustomPage
34}
35
fd206f0b
C
36@Component({
37 selector: 'my-edit-custom-config',
38 templateUrl: './edit-custom-config.component.html',
39 styleUrls: [ './edit-custom-config.component.scss' ]
40})
5f46d28c
C
41export class EditCustomConfigComponent extends FormReactive implements OnInit {
42 activeNav: string
45e0d669 43
2539932e 44 customConfig: ComponentCustomConfig
5f46d28c 45 serverConfig: ServerConfig
80ac2e55 46
2539932e
C
47 homepage: CustomPage
48
52c4976f
C
49 languageItems: SelectOptionsItem[] = []
50 categoryItems: SelectOptionsItem[] = []
ccc00cb2 51
fd206f0b 52 constructor (
53e4e201
C
53 private router: Router,
54 private route: ActivatedRoute,
d18d6478 55 protected formValidatorService: FormValidatorService,
f8b2c1b4 56 private notifier: Notifier,
fd206f0b 57 private configService: ConfigService,
2539932e 58 private customPage: CustomPageService,
5f46d28c
C
59 private serverService: ServerService,
60 private editConfigurationService: EditConfigurationService
fd206f0b
C
61 ) {
62 super()
fd206f0b
C
63 }
64
d18d6478 65 ngOnInit () {
ba430d75
C
66 this.serverConfig = this.serverService.getTmpConfig()
67 this.serverService.getConfig()
2539932e 68 .subscribe(config => this.serverConfig = config)
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 },
109 cache: {
110 previews: {
7ed1edbb 111 size: CACHE_PREVIEWS_SIZE_VALIDATOR
3866f1a0
C
112 },
113 captions: {
7ed1edbb 114 size: CACHE_CAPTIONS_SIZE_VALIDATOR
b3d5cb92
C
115 },
116 torrents: {
117 size: CACHE_CAPTIONS_SIZE_VALIDATOR
3866f1a0
C
118 }
119 },
120 signup: {
121 enabled: null,
7ed1edbb 122 limit: SIGNUP_LIMIT_VALIDATOR,
3866f1a0
C
123 requiresEmailVerification: null
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()
3da68f0a
RK
261 }
262
1f30a185 263 async formValidated () {
2539932e 264 const value: ComponentCustomConfig = this.form.getRawValue()
210856a7 265
2539932e
C
266 forkJoin([
267 this.configService.updateCustomConfig(omit(value, 'instanceCustomHomepage')),
268 this.customPage.updateInstanceHomepage(value.instanceCustomHomepage.content)
269 ])
fd206f0b 270 .subscribe(
2539932e
C
271 ([ resConfig ]) => {
272 const instanceCustomHomepage = {
273 content: value.instanceCustomHomepage.content
274 }
275
276 this.customConfig = { ...resConfig, instanceCustomHomepage }
fd206f0b
C
277
278 // Reload general configuration
ba430d75 279 this.serverService.resetConfig()
2539932e 280 .subscribe(config => this.serverConfig = config)
fd206f0b
C
281
282 this.updateForm()
66b16caf 283
66357162 284 this.notifier.success($localize`Configuration updated.`)
fd206f0b
C
285 },
286
f8b2c1b4 287 err => this.notifier.error(err.message)
fd206f0b
C
288 )
289 }
290
fb719404
C
291 hasConsistentOptions () {
292 if (this.hasLiveAllowReplayConsistentOptions()) return true
293
294 return false
295 }
296
297 hasLiveAllowReplayConsistentOptions () {
5f46d28c
C
298 if (
299 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
300 this.editConfigurationService.isLiveEnabled(this.form) &&
301 this.form.value['live']['allowReplay'] === true
302 ) {
fb719404
C
303 return false
304 }
305
306 return true
307 }
308
53e4e201
C
309 onNavChange (newActiveNav: string) {
310 this.activeNav = newActiveNav
311
312 this.router.navigate([], { fragment: this.activeNav })
313 }
314
45ba09fe
C
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
fd206f0b 334 private updateForm () {
3866f1a0 335 this.form.patchValue(this.customConfig)
fd206f0b 336 }
04cda1d7 337
5f46d28c 338 private loadConfigAndUpdateForm () {
2539932e
C
339 forkJoin([
340 this.configService.getCustomConfig(),
341 this.customPage.getInstanceHomepage()
342 ])
343 .subscribe(([ config, homepage ]) => {
344 this.customConfig = { ...config, instanceCustomHomepage: homepage }
04cda1d7 345
04cda1d7
C
346 this.updateForm()
347 // Force form validation
348 this.forceCheck()
349 },
350
351 err => this.notifier.error(err.message)
352 )
353 }
354
5f46d28c
C
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 },
16a173bb 364
5f46d28c
C
365 err => this.notifier.error(err.message)
366 )
16a173bb 367 }
fd206f0b 368}