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