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