]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { ConfigService } from '@app/+admin/config/shared/config.service'
3 import { ServerService } from '@app/core/server/server.service'
4 import { CustomConfigValidatorsService, FormReactive, UserValidatorsService } from '@app/shared'
5 import { Notifier } from '@app/core'
6 import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9 import { SelectItem } from 'primeng/api'
10 import { forkJoin } from 'rxjs'
11 import { ServerConfig } from '@shared/models'
12
13 @Component({
14 selector: 'my-edit-custom-config',
15 templateUrl: './edit-custom-config.component.html',
16 styleUrls: [ './edit-custom-config.component.scss' ]
17 })
18 export class EditCustomConfigComponent extends FormReactive implements OnInit {
19 customConfig: CustomConfig
20
21 resolutions: { id: string, label: string }[] = []
22 transcodingThreadOptions: { label: string, value: number }[] = []
23
24 languageItems: SelectItem[] = []
25 categoryItems: SelectItem[] = []
26
27 private serverConfig: ServerConfig
28
29 constructor (
30 protected formValidatorService: FormValidatorService,
31 private customConfigValidatorsService: CustomConfigValidatorsService,
32 private userValidatorsService: UserValidatorsService,
33 private notifier: Notifier,
34 private configService: ConfigService,
35 private serverService: ServerService,
36 private i18n: I18n
37 ) {
38 super()
39
40 this.resolutions = [
41 {
42 id: '0p',
43 label: this.i18n('Audio-only')
44 },
45 {
46 id: '240p',
47 label: this.i18n('240p')
48 },
49 {
50 id: '360p',
51 label: this.i18n('360p')
52 },
53 {
54 id: '480p',
55 label: this.i18n('480p')
56 },
57 {
58 id: '720p',
59 label: this.i18n('720p')
60 },
61 {
62 id: '1080p',
63 label: this.i18n('1080p')
64 },
65 {
66 id: '2160p',
67 label: this.i18n('2160p')
68 }
69 ]
70
71 this.transcodingThreadOptions = [
72 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
73 { value: 1, label: '1' },
74 { value: 2, label: '2' },
75 { value: 4, label: '4' },
76 { value: 8, label: '8' }
77 ]
78 }
79
80 get videoQuotaOptions () {
81 return this.configService.videoQuotaOptions
82 }
83
84 get videoQuotaDailyOptions () {
85 return this.configService.videoQuotaDailyOptions
86 }
87
88 get availableThemes () {
89 return this.serverConfig.theme.registered
90 .map(t => t.name)
91 }
92
93 getResolutionKey (resolution: string) {
94 return 'transcoding.resolutions.' + resolution
95 }
96
97 ngOnInit () {
98 this.serverConfig = this.serverService.getTmpConfig()
99 this.serverService.getConfig()
100 .subscribe(config => this.serverConfig = config)
101
102 const formGroupData: { [key in keyof CustomConfig ]: any } = {
103 instance: {
104 name: this.customConfigValidatorsService.INSTANCE_NAME,
105 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
106 description: null,
107
108 isNSFW: false,
109 defaultNSFWPolicy: null,
110
111 terms: null,
112 codeOfConduct: null,
113
114 creationReason: null,
115 moderationInformation: null,
116 administrator: null,
117 maintenanceLifetime: null,
118 businessModel: null,
119
120 hardwareInformation: null,
121
122 categories: null,
123 languages: null,
124
125 defaultClientRoute: null,
126
127 customizations: {
128 javascript: null,
129 css: null
130 }
131 },
132 theme: {
133 default: null
134 },
135 services: {
136 twitter: {
137 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
138 whitelisted: null
139 }
140 },
141 cache: {
142 previews: {
143 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
144 },
145 captions: {
146 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
147 }
148 },
149 signup: {
150 enabled: null,
151 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
152 requiresEmailVerification: null
153 },
154 import: {
155 videos: {
156 http: {
157 enabled: null
158 },
159 torrent: {
160 enabled: null
161 }
162 }
163 },
164 admin: {
165 email: this.customConfigValidatorsService.ADMIN_EMAIL
166 },
167 contactForm: {
168 enabled: null
169 },
170 user: {
171 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
172 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
173 },
174 transcoding: {
175 enabled: null,
176 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
177 allowAdditionalExtensions: null,
178 allowAudioFiles: null,
179 resolutions: {},
180 hls: {
181 enabled: null
182 },
183 webtorrent: {
184 enabled: null
185 }
186 },
187 autoBlacklist: {
188 videos: {
189 ofUsers: {
190 enabled: null
191 }
192 }
193 },
194 followers: {
195 instance: {
196 enabled: null,
197 manualApproval: null
198 }
199 },
200 followings: {
201 instance: {
202 autoFollowBack: {
203 enabled: null
204 },
205 autoFollowIndex: {
206 enabled: null,
207 indexUrl: this.customConfigValidatorsService.INDEX_URL
208 }
209 }
210 }
211 }
212
213 const defaultValues = {
214 transcoding: {
215 resolutions: {}
216 }
217 }
218 for (const resolution of this.resolutions) {
219 defaultValues.transcoding.resolutions[resolution.id] = 'false'
220 formGroupData.transcoding.resolutions[resolution.id] = null
221 }
222
223 this.buildForm(formGroupData)
224
225 forkJoin([
226 this.configService.getCustomConfig(),
227 this.serverService.getVideoLanguages(),
228 this.serverService.getVideoCategories()
229 ]).subscribe(
230 ([ config, languages, categories ]) => {
231 this.customConfig = config
232
233 this.languageItems = languages.map(l => ({ label: l.label, value: l.id }))
234 this.categoryItems = categories.map(l => ({ label: l.label, value: l.id }))
235
236 this.updateForm()
237 // Force form validation
238 this.forceCheck()
239 },
240
241 err => this.notifier.error(err.message)
242 )
243 }
244
245 isTranscodingEnabled () {
246 return this.form.value['transcoding']['enabled'] === true
247 }
248
249 isSignupEnabled () {
250 return this.form.value['signup']['enabled'] === true
251 }
252
253 async formValidated () {
254 this.configService.updateCustomConfig(this.form.value)
255 .pipe(
256 )
257 .subscribe(
258 res => {
259 this.customConfig = res
260
261 // Reload general configuration
262 this.serverService.resetConfig()
263
264 this.updateForm()
265
266 this.notifier.success(this.i18n('Configuration updated.'))
267 },
268
269 err => this.notifier.error(err.message)
270 )
271 }
272
273 getSelectedLanguageLabel () {
274 return this.i18n('{{\'{0} languages selected')
275 }
276
277 getDefaultLanguageLabel () {
278 return this.i18n('No language')
279 }
280
281 getSelectedCategoryLabel () {
282 return this.i18n('{{\'{0} categories selected')
283 }
284
285 getDefaultCategoryLabel () {
286 return this.i18n('No category')
287 }
288
289 private updateForm () {
290 this.form.patchValue(this.customConfig)
291 }
292 }