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