]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
WIP plugins: load theme on client 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 { 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: { id: string, label: string }[] = []
19 transcodingThreadOptions: { label: string, value: number }[] = []
20
21 constructor (
22 protected formValidatorService: FormValidatorService,
23 private customConfigValidatorsService: CustomConfigValidatorsService,
24 private userValidatorsService: UserValidatorsService,
25 private notifier: Notifier,
26 private configService: ConfigService,
27 private serverService: ServerService,
28 private i18n: I18n
29 ) {
30 super()
31
32 this.resolutions = [
33 {
34 id: '240p',
35 label: this.i18n('240p')
36 },
37 {
38 id: '360p',
39 label: this.i18n('360p')
40 },
41 {
42 id: '480p',
43 label: this.i18n('480p')
44 },
45 {
46 id: '720p',
47 label: this.i18n('720p')
48 },
49 {
50 id: '1080p',
51 label: this.i18n('1080p')
52 },
53 {
54 id: '2160p',
55 label: this.i18n('2160p')
56 }
57 ]
58
59 this.transcodingThreadOptions = [
60 { value: 0, label: this.i18n('Auto (via ffmpeg)') },
61 { value: 1, label: '1' },
62 { value: 2, label: '2' },
63 { value: 4, label: '4' },
64 { value: 8, label: '8' }
65 ]
66 }
67
68 get videoQuotaOptions () {
69 return this.configService.videoQuotaOptions
70 }
71
72 get videoQuotaDailyOptions () {
73 return this.configService.videoQuotaDailyOptions
74 }
75
76 get availableThemes () {
77 return this.serverService.getConfig().theme.registered
78 .map(t => t.name)
79 }
80
81 getResolutionKey (resolution: string) {
82 return 'transcoding.resolutions.' + resolution
83 }
84
85 ngOnInit () {
86 const formGroupData: { [key in keyof CustomConfig ]: any } = {
87 instance: {
88 name: this.customConfigValidatorsService.INSTANCE_NAME,
89 shortDescription: this.customConfigValidatorsService.INSTANCE_SHORT_DESCRIPTION,
90 description: null,
91 terms: null,
92 defaultClientRoute: null,
93 isNSFW: false,
94 defaultNSFWPolicy: null,
95 customizations: {
96 javascript: null,
97 css: null
98 }
99 },
100 theme: {
101 default: null
102 },
103 services: {
104 twitter: {
105 username: this.customConfigValidatorsService.SERVICES_TWITTER_USERNAME,
106 whitelisted: null
107 }
108 },
109 cache: {
110 previews: {
111 size: this.customConfigValidatorsService.CACHE_PREVIEWS_SIZE
112 },
113 captions: {
114 size: this.customConfigValidatorsService.CACHE_CAPTIONS_SIZE
115 }
116 },
117 signup: {
118 enabled: null,
119 limit: this.customConfigValidatorsService.SIGNUP_LIMIT,
120 requiresEmailVerification: null
121 },
122 import: {
123 videos: {
124 http: {
125 enabled: null
126 },
127 torrent: {
128 enabled: null
129 }
130 }
131 },
132 admin: {
133 email: this.customConfigValidatorsService.ADMIN_EMAIL
134 },
135 contactForm: {
136 enabled: null
137 },
138 user: {
139 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
140 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
141 },
142 transcoding: {
143 enabled: null,
144 threads: this.customConfigValidatorsService.TRANSCODING_THREADS,
145 allowAdditionalExtensions: null,
146 allowAudioFiles: null,
147 resolutions: {}
148 },
149 autoBlacklist: {
150 videos: {
151 ofUsers: {
152 enabled: null
153 }
154 }
155 },
156 followers: {
157 instance: {
158 enabled: null,
159 manualApproval: null
160 }
161 }
162 }
163
164 const defaultValues = {
165 transcoding: {
166 resolutions: {}
167 }
168 }
169 for (const resolution of this.resolutions) {
170 defaultValues.transcoding.resolutions[resolution.id] = 'false'
171 formGroupData.transcoding.resolutions[resolution.id] = null
172 }
173
174 this.buildForm(formGroupData)
175
176 this.configService.getCustomConfig()
177 .subscribe(
178 res => {
179 this.customConfig = res
180
181 this.updateForm()
182 // Force form validation
183 this.forceCheck()
184 },
185
186 err => this.notifier.error(err.message)
187 )
188 }
189
190 isTranscodingEnabled () {
191 return this.form.value['transcoding']['enabled'] === true
192 }
193
194 isSignupEnabled () {
195 return this.form.value['signup']['enabled'] === true
196 }
197
198 async formValidated () {
199 this.configService.updateCustomConfig(this.form.value)
200 .subscribe(
201 res => {
202 this.customConfig = res
203
204 // Reload general configuration
205 this.serverService.loadConfig()
206
207 this.updateForm()
208
209 this.notifier.success(this.i18n('Configuration updated.'))
210 },
211
212 err => this.notifier.error(err.message)
213 )
214 }
215
216 private updateForm () {
217 this.form.patchValue(this.customConfig)
218 }
219
220 }