]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Use new doc website links
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
CommitLineData
5f46d28c 1
4e55c132
C
2import { forkJoin } from 'rxjs'
3import { SelectOptionsItem } from 'src/types/select-options-item.model'
5f46d28c 4import { Component, OnInit } from '@angular/core'
53e4e201 5import { ActivatedRoute, Router } from '@angular/router'
67ed6552
C
6import { ConfigService } from '@app/+admin/config/shared/config.service'
7import { Notifier } from '@app/core'
8import { ServerService } from '@app/core/server/server.service'
52c4976f 9import {
7ed1edbb
C
10 ADMIN_EMAIL_VALIDATOR,
11 CACHE_CAPTIONS_SIZE_VALIDATOR,
12 CACHE_PREVIEWS_SIZE_VALIDATOR,
9129b769 13 CONCURRENCY_VALIDATOR,
7ed1edbb
C
14 INDEX_URL_VALIDATOR,
15 INSTANCE_NAME_VALIDATOR,
16 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
4e55c132
C
17 MAX_INSTANCE_LIVES_VALIDATOR,
18 MAX_LIVE_DURATION_VALIDATOR,
19 MAX_USER_LIVES_VALIDATOR,
7ed1edbb
C
20 SEARCH_INDEX_URL_VALIDATOR,
21 SERVICES_TWITTER_USERNAME_VALIDATOR,
22 SIGNUP_LIMIT_VALIDATOR,
23 TRANSCODING_THREADS_VALIDATOR
24} from '@app/shared/form-validators/custom-config-validators'
25import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
21e493d4 26import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
67ed6552 27import { CustomConfig, ServerConfig } from '@shared/models'
5f46d28c 28import { EditConfigurationService } from './edit-configuration.service'
fd206f0b
C
29
30@Component({
31 selector: 'my-edit-custom-config',
32 templateUrl: './edit-custom-config.component.html',
33 styleUrls: [ './edit-custom-config.component.scss' ]
34})
5f46d28c
C
35export class EditCustomConfigComponent extends FormReactive implements OnInit {
36 activeNav: string
45e0d669 37
bee0abff 38 customConfig: CustomConfig
5f46d28c 39 serverConfig: ServerConfig
80ac2e55 40
52c4976f
C
41 languageItems: SelectOptionsItem[] = []
42 categoryItems: SelectOptionsItem[] = []
ccc00cb2 43
fd206f0b 44 constructor (
53e4e201
C
45 private router: Router,
46 private route: ActivatedRoute,
d18d6478 47 protected formValidatorService: FormValidatorService,
f8b2c1b4 48 private notifier: Notifier,
fd206f0b 49 private configService: ConfigService,
5f46d28c
C
50 private serverService: ServerService,
51 private editConfigurationService: EditConfigurationService
fd206f0b
C
52 ) {
53 super()
fd206f0b
C
54 }
55
d18d6478 56 ngOnInit () {
ba430d75
C
57 this.serverConfig = this.serverService.getTmpConfig()
58 this.serverService.getConfig()
fb719404
C
59 .subscribe(config => {
60 this.serverConfig = config
61 })
ba430d75 62
3866f1a0
C
63 const formGroupData: { [key in keyof CustomConfig ]: any } = {
64 instance: {
7ed1edbb
C
65 name: INSTANCE_NAME_VALIDATOR,
66 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
3866f1a0 67 description: null,
ccc00cb2 68
f8802489 69 isNSFW: false,
3866f1a0 70 defaultNSFWPolicy: null,
ccc00cb2
C
71
72 terms: null,
73 codeOfConduct: null,
8ae03c37
C
74
75 creationReason: null,
ccc00cb2
C
76 moderationInformation: null,
77 administrator: null,
78 maintenanceLifetime: null,
79 businessModel: null,
80
be04c6fd
C
81 hardwareInformation: null,
82
ccc00cb2
C
83 categories: null,
84 languages: null,
85
86 defaultClientRoute: null,
87
3866f1a0
C
88 customizations: {
89 javascript: null,
90 css: null
91 }
92 },
7cd4d2ba
C
93 theme: {
94 default: null
95 },
3866f1a0
C
96 services: {
97 twitter: {
7ed1edbb 98 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
3866f1a0
C
99 whitelisted: null
100 }
101 },
102 cache: {
103 previews: {
7ed1edbb 104 size: CACHE_PREVIEWS_SIZE_VALIDATOR
3866f1a0
C
105 },
106 captions: {
7ed1edbb 107 size: CACHE_CAPTIONS_SIZE_VALIDATOR
3866f1a0
C
108 }
109 },
110 signup: {
111 enabled: null,
7ed1edbb 112 limit: SIGNUP_LIMIT_VALIDATOR,
3866f1a0
C
113 requiresEmailVerification: null
114 },
115 import: {
116 videos: {
9129b769 117 concurrency: CONCURRENCY_VALIDATOR,
3866f1a0
C
118 http: {
119 enabled: null
120 },
121 torrent: {
122 enabled: null
123 }
124 }
125 },
ba5d4a84
RK
126 trending: {
127 videos: {
128 algorithms: {
129 enabled: null,
130 default: null
131 }
132 }
133 },
3866f1a0 134 admin: {
7ed1edbb 135 email: ADMIN_EMAIL_VALIDATOR
3866f1a0
C
136 },
137 contactForm: {
138 enabled: null
139 },
140 user: {
7ed1edbb
C
141 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
142 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
3866f1a0
C
143 },
144 transcoding: {
145 enabled: null,
7ed1edbb 146 threads: TRANSCODING_THREADS_VALIDATOR,
3866f1a0 147 allowAdditionalExtensions: null,
536598cf 148 allowAudioFiles: null,
80ac2e55 149 profile: null,
9129b769 150 concurrency: CONCURRENCY_VALIDATOR,
5d9e4eaa
C
151 resolutions: {},
152 hls: {
153 enabled: null
5a71acd2
C
154 },
155 webtorrent: {
156 enabled: null
5d9e4eaa 157 }
7ccddd7b 158 },
c6c0fa6c
C
159 live: {
160 enabled: null,
161
4e55c132
C
162 maxDuration: MAX_LIVE_DURATION_VALIDATOR,
163 maxInstanceLives: MAX_INSTANCE_LIVES_VALIDATOR,
164 maxUserLives: MAX_USER_LIVES_VALIDATOR,
fb719404
C
165 allowReplay: null,
166
c6c0fa6c
C
167 transcoding: {
168 enabled: null,
169 threads: TRANSCODING_THREADS_VALIDATOR,
80ac2e55 170 profile: null,
c6c0fa6c
C
171 resolutions: {}
172 }
173 },
7ccddd7b
JM
174 autoBlacklist: {
175 videos: {
176 ofUsers: {
177 enabled: null
178 }
179 }
0dc64777
C
180 },
181 followers: {
182 instance: {
183 enabled: null,
184 manualApproval: null
185 }
e1b49ee5
C
186 },
187 followings: {
188 instance: {
189 autoFollowBack: {
190 enabled: null
191 },
192 autoFollowIndex: {
193 enabled: null,
7ed1edbb 194 indexUrl: INDEX_URL_VALIDATOR
e1b49ee5
C
195 }
196 }
72c33e71
C
197 },
198 broadcastMessage: {
199 enabled: null,
200 level: null,
201 dismissable: null,
202 message: null
5fb2e288
C
203 },
204 search: {
205 remoteUri: {
206 users: null,
207 anonymous: null
208 },
209 searchIndex: {
210 enabled: null,
7ed1edbb 211 url: SEARCH_INDEX_URL_VALIDATOR,
5fb2e288
C
212 disableLocalSearch: null,
213 isDefaultSearch: null
214 }
3866f1a0 215 }
fd206f0b
C
216 }
217
3866f1a0
C
218 const defaultValues = {
219 transcoding: {
220 resolutions: {}
c6c0fa6c
C
221 },
222 live: {
223 transcoding: {
224 resolutions: {}
225 }
3866f1a0
C
226 }
227 }
c6c0fa6c 228
5f46d28c 229 for (const resolution of this.editConfigurationService.getVODResolutions()) {
00aa1f0d
C
230 defaultValues.transcoding.resolutions[resolution.id] = 'false'
231 formGroupData.transcoding.resolutions[resolution.id] = null
fd206f0b
C
232 }
233
5f46d28c 234 for (const resolution of this.editConfigurationService.getLiveResolutions()) {
c6c0fa6c
C
235 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
236 formGroupData.live.transcoding.resolutions[resolution.id] = null
237 }
238
d18d6478 239 this.buildForm(formGroupData)
16a173bb 240
5f46d28c
C
241 if (this.route.snapshot.fragment) {
242 this.onNavChange(this.route.snapshot.fragment)
45e0d669 243 }
5fb2e288 244
5f46d28c
C
245 this.loadConfigAndUpdateForm()
246 this.loadCategoriesAndLanguages()
3da68f0a
RK
247 }
248
1f30a185 249 async formValidated () {
210856a7
C
250 const value: CustomConfig = this.form.getRawValue()
251
210856a7 252 this.configService.updateCustomConfig(value)
fd206f0b
C
253 .subscribe(
254 res => {
255 this.customConfig = res
256
257 // Reload general configuration
ba430d75 258 this.serverService.resetConfig()
fd206f0b
C
259
260 this.updateForm()
66b16caf 261
66357162 262 this.notifier.success($localize`Configuration updated.`)
fd206f0b
C
263 },
264
f8b2c1b4 265 err => this.notifier.error(err.message)
fd206f0b
C
266 )
267 }
268
fb719404
C
269 hasConsistentOptions () {
270 if (this.hasLiveAllowReplayConsistentOptions()) return true
271
272 return false
273 }
274
275 hasLiveAllowReplayConsistentOptions () {
5f46d28c
C
276 if (
277 this.editConfigurationService.isTranscodingEnabled(this.form) === false &&
278 this.editConfigurationService.isLiveEnabled(this.form) &&
279 this.form.value['live']['allowReplay'] === true
280 ) {
fb719404
C
281 return false
282 }
283
284 return true
285 }
286
53e4e201
C
287 onNavChange (newActiveNav: string) {
288 this.activeNav = newActiveNav
289
290 this.router.navigate([], { fragment: this.activeNav })
291 }
292
45ba09fe
C
293 grabAllErrors (errorObjectArg?: any) {
294 const errorObject = errorObjectArg || this.formErrors
295
296 let acc: string[] = []
297
298 for (const key of Object.keys(errorObject)) {
299 const value = errorObject[key]
300 if (!value) continue
301
302 if (typeof value === 'string') {
303 acc.push(value)
304 } else {
305 acc = acc.concat(this.grabAllErrors(value))
306 }
307 }
308
309 return acc
310 }
311
fd206f0b 312 private updateForm () {
3866f1a0 313 this.form.patchValue(this.customConfig)
fd206f0b 314 }
04cda1d7 315
5f46d28c
C
316 private loadConfigAndUpdateForm () {
317 this.configService.getCustomConfig()
318 .subscribe(config => {
04cda1d7
C
319 this.customConfig = config
320
04cda1d7
C
321 this.updateForm()
322 // Force form validation
323 this.forceCheck()
324 },
325
326 err => this.notifier.error(err.message)
327 )
328 }
329
5f46d28c
C
330 private loadCategoriesAndLanguages () {
331 forkJoin([
332 this.serverService.getVideoLanguages(),
333 this.serverService.getVideoCategories()
334 ]).subscribe(
335 ([ languages, categories ]) => {
336 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
337 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
338 },
16a173bb 339
5f46d28c
C
340 err => this.notifier.error(err.message)
341 )
16a173bb 342 }
fd206f0b 343}