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