]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/server/config-command.ts
Add signup approval API tests
[github/Chocobozzz/PeerTube.git] / shared / server-commands / server / config-command.ts
1 import { merge } from 'lodash'
2 import { About, CustomConfig, HttpStatusCode, ServerConfig } from '@shared/models'
3 import { DeepPartial } from '@shared/typescript-utils'
4 import { AbstractCommand, OverrideCommandOptions } from '../shared/abstract-command'
5
6 export class ConfigCommand extends AbstractCommand {
7
8 static getCustomConfigResolutions (enabled: boolean) {
9 return {
10 '144p': enabled,
11 '240p': enabled,
12 '360p': enabled,
13 '480p': enabled,
14 '720p': enabled,
15 '1080p': enabled,
16 '1440p': enabled,
17 '2160p': enabled
18 }
19 }
20
21 // ---------------------------------------------------------------------------
22
23 static getEmailOverrideConfig (emailPort: number) {
24 return {
25 smtp: {
26 hostname: '127.0.0.1',
27 port: emailPort
28 }
29 }
30 }
31
32 // ---------------------------------------------------------------------------
33
34 enableSignup (requiresApproval: boolean) {
35 return this.updateExistingSubConfig({
36 newConfig: {
37 signup: {
38 enabled: true,
39 requiresApproval,
40 limit: -1
41 }
42 }
43 })
44 }
45
46 // ---------------------------------------------------------------------------
47
48 disableImports () {
49 return this.setImportsEnabled(false)
50 }
51
52 enableImports () {
53 return this.setImportsEnabled(true)
54 }
55
56 private setImportsEnabled (enabled: boolean) {
57 return this.updateExistingSubConfig({
58 newConfig: {
59 import: {
60 videos: {
61 http: {
62 enabled
63 },
64
65 torrent: {
66 enabled
67 }
68 }
69 }
70 }
71 })
72 }
73
74 // ---------------------------------------------------------------------------
75
76 enableChannelSync () {
77 return this.setChannelSyncEnabled(true)
78 }
79
80 disableChannelSync () {
81 return this.setChannelSyncEnabled(false)
82 }
83
84 private setChannelSyncEnabled (enabled: boolean) {
85 return this.updateExistingSubConfig({
86 newConfig: {
87 import: {
88 videoChannelSynchronization: {
89 enabled
90 }
91 }
92 }
93 })
94 }
95
96 // ---------------------------------------------------------------------------
97
98 enableLive (options: {
99 allowReplay?: boolean
100 transcoding?: boolean
101 resolutions?: 'min' | 'max' // Default max
102 } = {}) {
103 const { allowReplay, transcoding, resolutions = 'max' } = options
104
105 return this.updateExistingSubConfig({
106 newConfig: {
107 live: {
108 enabled: true,
109 allowReplay: allowReplay ?? true,
110 transcoding: {
111 enabled: transcoding ?? true,
112 resolutions: ConfigCommand.getCustomConfigResolutions(resolutions === 'max')
113 }
114 }
115 }
116 })
117 }
118
119 disableTranscoding () {
120 return this.updateExistingSubConfig({
121 newConfig: {
122 transcoding: {
123 enabled: false
124 },
125 videoStudio: {
126 enabled: false
127 }
128 }
129 })
130 }
131
132 enableTranscoding (webtorrent = true, hls = true) {
133 return this.updateExistingSubConfig({
134 newConfig: {
135 transcoding: {
136 enabled: true,
137
138 allowAudioFiles: true,
139 allowAdditionalExtensions: true,
140
141 resolutions: ConfigCommand.getCustomConfigResolutions(true),
142
143 webtorrent: {
144 enabled: webtorrent
145 },
146 hls: {
147 enabled: hls
148 }
149 }
150 }
151 })
152 }
153
154 enableMinimumTranscoding (webtorrent = true, hls = true) {
155 return this.updateExistingSubConfig({
156 newConfig: {
157 transcoding: {
158 enabled: true,
159 resolutions: {
160 ...ConfigCommand.getCustomConfigResolutions(false),
161
162 '240p': true
163 },
164
165 webtorrent: {
166 enabled: webtorrent
167 },
168 hls: {
169 enabled: hls
170 }
171 }
172 }
173 })
174 }
175
176 // ---------------------------------------------------------------------------
177
178 enableStudio () {
179 return this.updateExistingSubConfig({
180 newConfig: {
181 videoStudio: {
182 enabled: true
183 }
184 }
185 })
186 }
187
188 // ---------------------------------------------------------------------------
189
190 getConfig (options: OverrideCommandOptions = {}) {
191 const path = '/api/v1/config'
192
193 return this.getRequestBody<ServerConfig>({
194 ...options,
195
196 path,
197 implicitToken: false,
198 defaultExpectedStatus: HttpStatusCode.OK_200
199 })
200 }
201
202 async getIndexHTMLConfig (options: OverrideCommandOptions = {}) {
203 const text = await this.getRequestText({
204 ...options,
205
206 path: '/',
207 implicitToken: false,
208 defaultExpectedStatus: HttpStatusCode.OK_200
209 })
210
211 const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>')
212
213 // We parse the string twice, first to extract the string and then to extract the JSON
214 return JSON.parse(JSON.parse(match[1])) as ServerConfig
215 }
216
217 getAbout (options: OverrideCommandOptions = {}) {
218 const path = '/api/v1/config/about'
219
220 return this.getRequestBody<About>({
221 ...options,
222
223 path,
224 implicitToken: false,
225 defaultExpectedStatus: HttpStatusCode.OK_200
226 })
227 }
228
229 getCustomConfig (options: OverrideCommandOptions = {}) {
230 const path = '/api/v1/config/custom'
231
232 return this.getRequestBody<CustomConfig>({
233 ...options,
234
235 path,
236 implicitToken: true,
237 defaultExpectedStatus: HttpStatusCode.OK_200
238 })
239 }
240
241 updateCustomConfig (options: OverrideCommandOptions & {
242 newCustomConfig: CustomConfig
243 }) {
244 const path = '/api/v1/config/custom'
245
246 return this.putBodyRequest({
247 ...options,
248
249 path,
250 fields: options.newCustomConfig,
251 implicitToken: true,
252 defaultExpectedStatus: HttpStatusCode.OK_200
253 })
254 }
255
256 deleteCustomConfig (options: OverrideCommandOptions = {}) {
257 const path = '/api/v1/config/custom'
258
259 return this.deleteRequest({
260 ...options,
261
262 path,
263 implicitToken: true,
264 defaultExpectedStatus: HttpStatusCode.OK_200
265 })
266 }
267
268 async updateExistingSubConfig (options: OverrideCommandOptions & {
269 newConfig: DeepPartial<CustomConfig>
270 }) {
271 const existing = await this.getCustomConfig({ ...options, expectedStatus: HttpStatusCode.OK_200 })
272
273 return this.updateCustomConfig({ ...options, newCustomConfig: merge({}, existing, options.newConfig) })
274 }
275
276 updateCustomSubConfig (options: OverrideCommandOptions & {
277 newConfig: DeepPartial<CustomConfig>
278 }) {
279 const newCustomConfig: CustomConfig = {
280 instance: {
281 name: 'PeerTube updated',
282 shortDescription: 'my short description',
283 description: 'my super description',
284 terms: 'my super terms',
285 codeOfConduct: 'my super coc',
286
287 creationReason: 'my super creation reason',
288 moderationInformation: 'my super moderation information',
289 administrator: 'Kuja',
290 maintenanceLifetime: 'forever',
291 businessModel: 'my super business model',
292 hardwareInformation: '2vCore 3GB RAM',
293
294 languages: [ 'en', 'es' ],
295 categories: [ 1, 2 ],
296
297 isNSFW: true,
298 defaultNSFWPolicy: 'blur',
299
300 defaultClientRoute: '/videos/recently-added',
301
302 customizations: {
303 javascript: 'alert("coucou")',
304 css: 'body { background-color: red; }'
305 }
306 },
307 theme: {
308 default: 'default'
309 },
310 services: {
311 twitter: {
312 username: '@MySuperUsername',
313 whitelisted: true
314 }
315 },
316 client: {
317 videos: {
318 miniature: {
319 preferAuthorDisplayName: false
320 }
321 },
322 menu: {
323 login: {
324 redirectOnSingleExternalAuth: false
325 }
326 }
327 },
328 cache: {
329 previews: {
330 size: 2
331 },
332 captions: {
333 size: 3
334 },
335 torrents: {
336 size: 4
337 }
338 },
339 signup: {
340 enabled: false,
341 limit: 5,
342 requiresApproval: true,
343 requiresEmailVerification: false,
344 minimumAge: 16
345 },
346 admin: {
347 email: 'superadmin1@example.com'
348 },
349 contactForm: {
350 enabled: true
351 },
352 user: {
353 videoQuota: 5242881,
354 videoQuotaDaily: 318742
355 },
356 videoChannels: {
357 maxPerUser: 20
358 },
359 transcoding: {
360 enabled: true,
361 allowAdditionalExtensions: true,
362 allowAudioFiles: true,
363 threads: 1,
364 concurrency: 3,
365 profile: 'default',
366 resolutions: {
367 '0p': false,
368 '144p': false,
369 '240p': false,
370 '360p': true,
371 '480p': true,
372 '720p': false,
373 '1080p': false,
374 '1440p': false,
375 '2160p': false
376 },
377 alwaysTranscodeOriginalResolution: true,
378 webtorrent: {
379 enabled: true
380 },
381 hls: {
382 enabled: false
383 }
384 },
385 live: {
386 enabled: true,
387 allowReplay: false,
388 latencySetting: {
389 enabled: false
390 },
391 maxDuration: -1,
392 maxInstanceLives: -1,
393 maxUserLives: 50,
394 transcoding: {
395 enabled: true,
396 threads: 4,
397 profile: 'default',
398 resolutions: {
399 '144p': true,
400 '240p': true,
401 '360p': true,
402 '480p': true,
403 '720p': true,
404 '1080p': true,
405 '1440p': true,
406 '2160p': true
407 },
408 alwaysTranscodeOriginalResolution: true
409 }
410 },
411 videoStudio: {
412 enabled: false
413 },
414 import: {
415 videos: {
416 concurrency: 3,
417 http: {
418 enabled: false
419 },
420 torrent: {
421 enabled: false
422 }
423 },
424 videoChannelSynchronization: {
425 enabled: false,
426 maxPerUser: 10
427 }
428 },
429 trending: {
430 videos: {
431 algorithms: {
432 enabled: [ 'hot', 'most-viewed', 'most-liked' ],
433 default: 'hot'
434 }
435 }
436 },
437 autoBlacklist: {
438 videos: {
439 ofUsers: {
440 enabled: false
441 }
442 }
443 },
444 followers: {
445 instance: {
446 enabled: true,
447 manualApproval: false
448 }
449 },
450 followings: {
451 instance: {
452 autoFollowBack: {
453 enabled: false
454 },
455 autoFollowIndex: {
456 indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
457 enabled: false
458 }
459 }
460 },
461 broadcastMessage: {
462 enabled: true,
463 level: 'warning',
464 message: 'hello',
465 dismissable: true
466 },
467 search: {
468 remoteUri: {
469 users: true,
470 anonymous: true
471 },
472 searchIndex: {
473 enabled: true,
474 url: 'https://search.joinpeertube.org',
475 disableLocalSearch: true,
476 isDefaultSearch: true
477 }
478 }
479 }
480
481 merge(newCustomConfig, options.newConfig)
482
483 return this.updateCustomConfig({ ...options, newCustomConfig })
484 }
485 }