]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Implement contact form on server side
[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 { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
9
10 @Component({
11 selector: 'my-edit-custom-config',
12 templateUrl: './edit-custom-config.component.html',
13 styleUrls: [ './edit-custom-config.component.scss' ]
14 })
15 export class EditCustomConfigComponent extends FormReactive implements OnInit {
16 customConfig: CustomConfig
17
18 resolutions: string[] = []
19 transcodingThreadOptions: { label: string, value: number }[] = []
20
21 private oldCustomJavascript: string
22 private oldCustomCSS: string
23
24 constructor (
25 protected formValidatorService: FormValidatorService,
26 private customConfigValidatorsService: CustomConfigValidatorsService,
27 private userValidatorsService: UserValidatorsService,
28 private notifier: Notifier,
29 private configService: ConfigService,
30 private serverService: ServerService,
31 private i18n: I18n
32 ) {
33 super()
34
35 this.resolutions = [
36 this.i18n('240p'),
37 this.i18n('360p'),
38 this.i18n('480p'),
39 this.i18n('720p'),
40 this.i18n('1080p')
41 ]
42
43 this.transcodingThreadOptions = [
44 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
45 { value: 1, label: '1' },
46 { value: 2, label: '2' },
47 { value: 4, label: '4' },
48 { value: 8, label: '8' }
49 ]
50 }
51
52 get videoQuotaOptions () {
53 return this.configService.videoQuotaOptions
54 }
55
56 get videoQuotaDailyOptions () {
57 return this.configService.videoQuotaDailyOptions
58 }
59
60 getResolutionKey (resolution: string) {
61 return 'transcodingResolution' + resolution
62 }
63
64 ngOnInit () {
65 const formGroupData: { [key: string]: any } = {
66 instanceName: this.customConfigValidatorsService.INSTANCE_NAME,
67 instanceShortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
68 instanceDescription: null,
69 instanceTerms: null,
70 instanceDefaultClientRoute: null,
71 instanceDefaultNSFWPolicy: null,
72 servicesTwitterUsername: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
73 servicesTwitterWhitelisted: null,
74 cachePreviewsSize: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE,
75 cacheCaptionsSize: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE,
76 signupEnabled: null,
77 signupLimit: this.customConfigValidatorsService.SIGNUP_LIMIT,
78 signupRequiresEmailVerification: null,
79 importVideosHttpEnabled: null,
80 importVideosTorrentEnabled: null,
81 adminEmail: this.customConfigValidatorsService.ADMIN_EMAIL,
82 userVideoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
83 userVideoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
84 transcodingThreads: this.customConfigValidatorsService.TRANSCODING_THREADS,
85 transcodingAllowAdditionalExtensions: null,
86 transcodingEnabled: null,
87 customizationJavascript: null,
88 customizationCSS: null
89 }
90
91 const defaultValues: BuildFormDefaultValues = {}
92 for (const resolution of this.resolutions) {
93 const key = this.getResolutionKey(resolution)
94 defaultValues[key] = 'false'
95 formGroupData[key] = null
96 }
97
98 this.buildForm(formGroupData)
99
100 this.configService.getCustomConfig()
101 .subscribe(
102 res => {
103 this.customConfig = res
104
105 this.oldCustomCSS = this.customConfig.instance.customizations.css
106 this.oldCustomJavascript = this.customConfig.instance.customizations.javascript
107
108 this.updateForm()
109 // Force form validation
110 this.forceCheck()
111 },
112
113 err => this.notifier.error(err.message)
114 )
115 }
116
117 isTranscodingEnabled () {
118 return this.form.value['transcodingEnabled'] === true
119 }
120
121 isSignupEnabled () {
122 return this.form.value['signupEnabled'] === true
123 }
124
125 async formValidated () {
126 const data: CustomConfig = {
127 instance: {
128 name: this.form.value['instanceName'],
129 shortDescription: this.form.value['instanceShortDescription'],
130 description: this.form.value['instanceDescription'],
131 terms: this.form.value['instanceTerms'],
132 defaultClientRoute: this.form.value['instanceDefaultClientRoute'],
133 defaultNSFWPolicy: this.form.value['instanceDefaultNSFWPolicy'],
134 customizations: {
135 javascript: this.form.value['customizationJavascript'],
136 css: this.form.value['customizationCSS']
137 }
138 },
139 services: {
140 twitter: {
141 username: this.form.value['servicesTwitterUsername'],
142 whitelisted: this.form.value['servicesTwitterWhitelisted']
143 }
144 },
145 cache: {
146 previews: {
147 size: this.form.value['cachePreviewsSize']
148 },
149 captions: {
150 size: this.form.value['cacheCaptionsSize']
151 }
152 },
153 signup: {
154 enabled: this.form.value['signupEnabled'],
155 limit: this.form.value['signupLimit'],
156 requiresEmailVerification: this.form.value['signupRequiresEmailVerification']
157 },
158 admin: {
159 email: this.form.value['adminEmail']
160 },
161 user: {
162 videoQuota: this.form.value['userVideoQuota'],
163 videoQuotaDaily: this.form.value['userVideoQuotaDaily']
164 },
165 transcoding: {
166 enabled: this.form.value['transcodingEnabled'],
167 allowAdditionalExtensions: this.form.value['transcodingAllowAdditionalExtensions'],
168 threads: this.form.value['transcodingThreads'],
169 resolutions: {
170 '240p': this.form.value[this.getResolutionKey('240p')],
171 '360p': this.form.value[this.getResolutionKey('360p')],
172 '480p': this.form.value[this.getResolutionKey('480p')],
173 '720p': this.form.value[this.getResolutionKey('720p')],
174 '1080p': this.form.value[this.getResolutionKey('1080p')]
175 }
176 },
177 import: {
178 videos: {
179 http: {
180 enabled: this.form.value['importVideosHttpEnabled']
181 },
182 torrent: {
183 enabled: this.form.value['importVideosTorrentEnabled']
184 }
185 }
186 }
187 }
188
189 this.configService.updateCustomConfig(data)
190 .subscribe(
191 res => {
192 this.customConfig = res
193
194 // Reload general configuration
195 this.serverService.loadConfig()
196
197 this.updateForm()
198
199 this.notifier.success(this.i18n('Configuration updated.'))
200 },
201
202 err => this.notifier.error(err.message)
203 )
204 }
205
206 private updateForm () {
207 const data: { [key: string]: any } = {
208 instanceName: this.customConfig.instance.name,
209 instanceShortDescription: this.customConfig.instance.shortDescription,
210 instanceDescription: this.customConfig.instance.description,
211 instanceTerms: this.customConfig.instance.terms,
212 instanceDefaultClientRoute: this.customConfig.instance.defaultClientRoute,
213 instanceDefaultNSFWPolicy: this.customConfig.instance.defaultNSFWPolicy,
214 servicesTwitterUsername: this.customConfig.services.twitter.username,
215 servicesTwitterWhitelisted: this.customConfig.services.twitter.whitelisted,
216 cachePreviewsSize: this.customConfig.cache.previews.size,
217 cacheCaptionsSize: this.customConfig.cache.captions.size,
218 signupEnabled: this.customConfig.signup.enabled,
219 signupLimit: this.customConfig.signup.limit,
220 signupRequiresEmailVerification: this.customConfig.signup.requiresEmailVerification,
221 adminEmail: this.customConfig.admin.email,
222 userVideoQuota: this.customConfig.user.videoQuota,
223 userVideoQuotaDaily: this.customConfig.user.videoQuotaDaily,
224 transcodingThreads: this.customConfig.transcoding.threads,
225 transcodingEnabled: this.customConfig.transcoding.enabled,
226 transcodingAllowAdditionalExtensions: this.customConfig.transcoding.allowAdditionalExtensions,
227 customizationJavascript: this.customConfig.instance.customizations.javascript,
228 customizationCSS: this.customConfig.instance.customizations.css,
229 importVideosHttpEnabled: this.customConfig.import.videos.http.enabled,
230 importVideosTorrentEnabled: this.customConfig.import.videos.torrent.enabled
231 }
232
233 for (const resolution of this.resolutions) {
234 const key = this.getResolutionKey(resolution)
235 data[key] = this.customConfig.transcoding.resolutions[resolution]
236 }
237
238 this.form.patchValue(data)
239 }
240
241 }