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