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