]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Move form modules in the form shared module
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
CommitLineData
ccc00cb2
C
1import { SelectItem } from 'primeng/api'
2import { forkJoin } from 'rxjs'
45e0d669 3import { ViewportScroller } from '@angular/common'
67ed6552
C
4import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core'
5import { ConfigService } from '@app/+admin/config/shared/config.service'
6import { Notifier } from '@app/core'
7import { ServerService } from '@app/core/server/server.service'
8import { CustomConfigValidatorsService, FormReactive, FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms'
45c6bcf3 9import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
67ed6552
C
10import { I18n } from '@ngx-translate/i18n-polyfill'
11import { CustomConfig, ServerConfig } from '@shared/models'
fd206f0b
C
12
13@Component({
14 selector: 'my-edit-custom-config',
15 templateUrl: './edit-custom-config.component.html',
16 styleUrls: [ './edit-custom-config.component.scss' ]
17})
45e0d669 18export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
45c6bcf3
C
19 // FIXME: use built-in router
20 @ViewChild('nav') nav: NgbNav
45e0d669
RK
21
22 initDone = false
bee0abff 23 customConfig: CustomConfig
bee0abff 24
46db9430 25 resolutions: { id: string, label: string, description?: string }[] = []
3827c3b3 26 transcodingThreadOptions: { label: string, value: number }[] = []
fd206f0b 27
ccc00cb2
C
28 languageItems: SelectItem[] = []
29 categoryItems: SelectItem[] = []
30
ba430d75
C
31 private serverConfig: ServerConfig
32
fd206f0b 33 constructor (
45e0d669 34 private viewportScroller: ViewportScroller,
d18d6478 35 protected formValidatorService: FormValidatorService,
e309822b
C
36 private customConfigValidatorsService: CustomConfigValidatorsService,
37 private userValidatorsService: UserValidatorsService,
f8b2c1b4 38 private notifier: Notifier,
fd206f0b 39 private configService: ConfigService,
1f30a185 40 private serverService: ServerService,
b1d40cff 41 private i18n: I18n
fd206f0b
C
42 ) {
43 super()
3827c3b3
C
44
45 this.resolutions = [
2fa9c40e 46 {
5c7d6508 47 id: '0p',
46db9430 48 label: this.i18n('Audio-only'),
6a07a058 49 description: this.i18n('A <code>.mp4</code> that keeps the original audio track, with no video')
5c7d6508 50 },
00aa1f0d
C
51 {
52 id: '240p',
53 label: this.i18n('240p')
54 },
55 {
56 id: '360p',
57 label: this.i18n('360p')
58 },
59 {
60 id: '480p',
61 label: this.i18n('480p')
62 },
63 {
64 id: '720p',
65 label: this.i18n('720p')
66 },
67 {
68 id: '1080p',
69 label: this.i18n('1080p')
70 },
71 {
72 id: '2160p',
73 label: this.i18n('2160p')
74 }
3827c3b3
C
75 ]
76
77 this.transcodingThreadOptions = [
78 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
79 { value: 1, label: '1' },
80 { value: 2, label: '2' },
81 { value: 4, label: '4' },
82 { value: 8, label: '8' }
83 ]
fd206f0b
C
84 }
85
41a676db 86 get videoQuotaOptions () {
3827c3b3 87 return this.configService.videoQuotaOptions
41a676db
C
88 }
89
90 get videoQuotaDailyOptions () {
3827c3b3 91 return this.configService.videoQuotaDailyOptions
41a676db
C
92 }
93
7cd4d2ba 94 get availableThemes () {
ba430d75 95 return this.serverConfig.theme.registered
ffb321be 96 .map(t => t.name)
7cd4d2ba
C
97 }
98
fd206f0b 99 getResolutionKey (resolution: string) {
3866f1a0 100 return 'transcoding.resolutions.' + resolution
fd206f0b
C
101 }
102
d18d6478 103 ngOnInit () {
ba430d75
C
104 this.serverConfig = this.serverService.getTmpConfig()
105 this.serverService.getConfig()
106 .subscribe(config => this.serverConfig = config)
107
3866f1a0
C
108 const formGroupData: { [key in keyof CustomConfig ]: any } = {
109 instance: {
110 name: this.customConfigValidatorsService.INSTANCE_NAME,
111 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
112 description: null,
ccc00cb2 113
f8802489 114 isNSFW: false,
3866f1a0 115 defaultNSFWPolicy: null,
ccc00cb2
C
116
117 terms: null,
118 codeOfConduct: null,
8ae03c37
C
119
120 creationReason: null,
ccc00cb2
C
121 moderationInformation: null,
122 administrator: null,
123 maintenanceLifetime: null,
124 businessModel: null,
125
be04c6fd
C
126 hardwareInformation: null,
127
ccc00cb2
C
128 categories: null,
129 languages: null,
130
131 defaultClientRoute: null,
132
3866f1a0
C
133 customizations: {
134 javascript: null,
135 css: null
136 }
137 },
7cd4d2ba
C
138 theme: {
139 default: null
140 },
3866f1a0
C
141 services: {
142 twitter: {
143 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
144 whitelisted: null
145 }
146 },
147 cache: {
148 previews: {
149 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
150 },
151 captions: {
152 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
153 }
154 },
155 signup: {
156 enabled: null,
157 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
158 requiresEmailVerification: null
159 },
160 import: {
161 videos: {
162 http: {
163 enabled: null
164 },
165 torrent: {
166 enabled: null
167 }
168 }
169 },
170 admin: {
171 email: this.customConfigValidatorsService.ADMIN_EMAIL
172 },
173 contactForm: {
174 enabled: null
175 },
176 user: {
177 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
178 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
179 },
180 transcoding: {
181 enabled: null,
182 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
183 allowAdditionalExtensions: null,
536598cf 184 allowAudioFiles: null,
5d9e4eaa
C
185 resolutions: {},
186 hls: {
187 enabled: null
5a71acd2
C
188 },
189 webtorrent: {
190 enabled: null
5d9e4eaa 191 }
7ccddd7b
JM
192 },
193 autoBlacklist: {
194 videos: {
195 ofUsers: {
196 enabled: null
197 }
198 }
0dc64777
C
199 },
200 followers: {
201 instance: {
202 enabled: null,
203 manualApproval: null
204 }
e1b49ee5
C
205 },
206 followings: {
207 instance: {
208 autoFollowBack: {
209 enabled: null
210 },
211 autoFollowIndex: {
212 enabled: null,
213 indexUrl: this.customConfigValidatorsService.INDEX_URL
214 }
215 }
72c33e71
C
216 },
217 broadcastMessage: {
218 enabled: null,
219 level: null,
220 dismissable: null,
221 message: null
5fb2e288
C
222 },
223 search: {
224 remoteUri: {
225 users: null,
226 anonymous: null
227 },
228 searchIndex: {
229 enabled: null,
230 url: this.customConfigValidatorsService.SEARCH_INDEX_URL,
231 disableLocalSearch: null,
232 isDefaultSearch: null
233 }
3866f1a0 234 }
fd206f0b
C
235 }
236
3866f1a0
C
237 const defaultValues = {
238 transcoding: {
239 resolutions: {}
240 }
241 }
fd206f0b 242 for (const resolution of this.resolutions) {
00aa1f0d
C
243 defaultValues.transcoding.resolutions[resolution.id] = 'false'
244 formGroupData.transcoding.resolutions[resolution.id] = null
fd206f0b
C
245 }
246
d18d6478 247 this.buildForm(formGroupData)
04cda1d7
C
248 this.loadForm()
249 this.checkTranscodingFields()
fd206f0b
C
250 }
251
45e0d669
RK
252 ngAfterViewChecked () {
253 if (!this.initDone) {
254 this.initDone = true
255 this.gotoAnchor()
256 }
257 }
258
fd206f0b 259 isTranscodingEnabled () {
3866f1a0 260 return this.form.value['transcoding']['enabled'] === true
fd206f0b
C
261 }
262
263 isSignupEnabled () {
3866f1a0 264 return this.form.value['signup']['enabled'] === true
fd206f0b
C
265 }
266
5fb2e288
C
267 isSearchIndexEnabled () {
268 return this.form.value['search']['searchIndex']['enabled'] === true
269 }
270
4ee6a8b1
C
271 isAutoFollowIndexEnabled () {
272 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
273 }
274
1f30a185 275 async formValidated () {
04cda1d7 276 this.configService.updateCustomConfig(this.form.getRawValue())
fd206f0b
C
277 .subscribe(
278 res => {
279 this.customConfig = res
280
281 // Reload general configuration
ba430d75 282 this.serverService.resetConfig()
fd206f0b
C
283
284 this.updateForm()
66b16caf 285
f8b2c1b4 286 this.notifier.success(this.i18n('Configuration updated.'))
fd206f0b
C
287 },
288
f8b2c1b4 289 err => this.notifier.error(err.message)
fd206f0b
C
290 )
291 }
292
ccc00cb2
C
293 getSelectedLanguageLabel () {
294 return this.i18n('{{\'{0} languages selected')
295 }
296
297 getDefaultLanguageLabel () {
298 return this.i18n('No language')
299 }
300
301 getSelectedCategoryLabel () {
302 return this.i18n('{{\'{0} categories selected')
303 }
304
305 getDefaultCategoryLabel () {
306 return this.i18n('No category')
307 }
308
45e0d669 309 gotoAnchor () {
45c6bcf3 310 const hashToNav = {
45e0d669
RK
311 'customizations': 'advanced-configuration'
312 }
313 const hash = window.location.hash.replace('#', '')
314
45c6bcf3
C
315 if (hash && Object.keys(hashToNav).includes(hash)) {
316 this.nav.select(hashToNav[hash])
45e0d669
RK
317 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
318 }
319 }
320
fd206f0b 321 private updateForm () {
3866f1a0 322 this.form.patchValue(this.customConfig)
fd206f0b 323 }
04cda1d7
C
324
325 private loadForm () {
326 forkJoin([
327 this.configService.getCustomConfig(),
328 this.serverService.getVideoLanguages(),
329 this.serverService.getVideoCategories()
330 ]).subscribe(
331 ([ config, languages, categories ]) => {
332 this.customConfig = config
333
334 this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
335 this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
336
337 this.updateForm()
338 // Force form validation
339 this.forceCheck()
340 },
341
342 err => this.notifier.error(err.message)
343 )
344 }
345
346 private checkTranscodingFields () {
347 const hlsControl = this.form.get('transcoding.hls.enabled')
348 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
349
350 webtorrentControl.valueChanges
351 .subscribe(newValue => {
352 if (newValue === false && !hlsControl.disabled) {
353 hlsControl.disable()
354 }
355
356 if (newValue === true && !hlsControl.enabled) {
357 hlsControl.enable()
358 }
359 })
360
361 hlsControl.valueChanges
362 .subscribe(newValue => {
363 if (newValue === false && !webtorrentControl.disabled) {
364 webtorrentControl.disable()
365 }
366
367 if (newValue === true && !webtorrentControl.enabled) {
368 webtorrentControl.enable()
369 }
370 })
371 }
fd206f0b 372}