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'
6 export class ConfigCommand extends AbstractCommand {
8 static getCustomConfigResolutions (enabled: boolean) {
21 // ---------------------------------------------------------------------------
23 static getEmailOverrideConfig (emailPort: number) {
26 hostname: '127.0.0.1',
32 // ---------------------------------------------------------------------------
34 enableSignup (requiresApproval: boolean, limit = -1) {
35 return this.updateExistingSubConfig({
46 // ---------------------------------------------------------------------------
49 return this.setImportsEnabled(false)
53 return this.setImportsEnabled(true)
56 private setImportsEnabled (enabled: boolean) {
57 return this.updateExistingSubConfig({
74 // ---------------------------------------------------------------------------
76 enableChannelSync () {
77 return this.setChannelSyncEnabled(true)
80 disableChannelSync () {
81 return this.setChannelSyncEnabled(false)
84 private setChannelSyncEnabled (enabled: boolean) {
85 return this.updateExistingSubConfig({
88 videoChannelSynchronization: {
96 // ---------------------------------------------------------------------------
98 enableLive (options: {
100 transcoding?: boolean
101 resolutions?: 'min' | 'max' // Default max
103 const { allowReplay, transcoding, resolutions = 'max' } = options
105 return this.updateExistingSubConfig({
109 allowReplay: allowReplay ?? true,
111 enabled: transcoding ?? true,
112 resolutions: ConfigCommand.getCustomConfigResolutions(resolutions === 'max')
119 disableTranscoding () {
120 return this.updateExistingSubConfig({
132 enableTranscoding (webtorrent = true, hls = true) {
133 return this.updateExistingSubConfig({
138 allowAudioFiles: true,
139 allowAdditionalExtensions: true,
141 resolutions: ConfigCommand.getCustomConfigResolutions(true),
154 enableMinimumTranscoding (webtorrent = true, hls = true) {
155 return this.updateExistingSubConfig({
160 ...ConfigCommand.getCustomConfigResolutions(false),
176 // ---------------------------------------------------------------------------
179 return this.updateExistingSubConfig({
188 // ---------------------------------------------------------------------------
190 getConfig (options: OverrideCommandOptions = {}) {
191 const path = '/api/v1/config'
193 return this.getRequestBody<ServerConfig>({
197 implicitToken: false,
198 defaultExpectedStatus: HttpStatusCode.OK_200
202 async getIndexHTMLConfig (options: OverrideCommandOptions = {}) {
203 const text = await this.getRequestText({
207 implicitToken: false,
208 defaultExpectedStatus: HttpStatusCode.OK_200
211 const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>')
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
217 getAbout (options: OverrideCommandOptions = {}) {
218 const path = '/api/v1/config/about'
220 return this.getRequestBody<About>({
224 implicitToken: false,
225 defaultExpectedStatus: HttpStatusCode.OK_200
229 getCustomConfig (options: OverrideCommandOptions = {}) {
230 const path = '/api/v1/config/custom'
232 return this.getRequestBody<CustomConfig>({
237 defaultExpectedStatus: HttpStatusCode.OK_200
241 updateCustomConfig (options: OverrideCommandOptions & {
242 newCustomConfig: CustomConfig
244 const path = '/api/v1/config/custom'
246 return this.putBodyRequest({
250 fields: options.newCustomConfig,
252 defaultExpectedStatus: HttpStatusCode.OK_200
256 deleteCustomConfig (options: OverrideCommandOptions = {}) {
257 const path = '/api/v1/config/custom'
259 return this.deleteRequest({
264 defaultExpectedStatus: HttpStatusCode.OK_200
268 async updateExistingSubConfig (options: OverrideCommandOptions & {
269 newConfig: DeepPartial<CustomConfig>
271 const existing = await this.getCustomConfig({ ...options, expectedStatus: HttpStatusCode.OK_200 })
273 return this.updateCustomConfig({ ...options, newCustomConfig: merge({}, existing, options.newConfig) })
276 updateCustomSubConfig (options: OverrideCommandOptions & {
277 newConfig: DeepPartial<CustomConfig>
279 const newCustomConfig: CustomConfig = {
281 name: 'PeerTube updated',
282 shortDescription: 'my short description',
283 description: 'my super description',
284 terms: 'my super terms',
285 codeOfConduct: 'my super coc',
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',
294 languages: [ 'en', 'es' ],
295 categories: [ 1, 2 ],
298 defaultNSFWPolicy: 'blur',
300 defaultClientRoute: '/videos/recently-added',
303 javascript: 'alert("coucou")',
304 css: 'body { background-color: red; }'
312 username: '@MySuperUsername',
319 preferAuthorDisplayName: false
324 redirectOnSingleExternalAuth: false
342 requiresApproval: true,
343 requiresEmailVerification: false,
347 email: 'superadmin1@example.com'
354 videoQuotaDaily: 318742
361 allowAdditionalExtensions: true,
362 allowAudioFiles: true,
377 alwaysTranscodeOriginalResolution: true,
392 maxInstanceLives: -1,
408 alwaysTranscodeOriginalResolution: true
424 videoChannelSynchronization: {
432 enabled: [ 'hot', 'most-viewed', 'most-liked' ],
447 manualApproval: false
456 indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
474 url: 'https://search.joinpeertube.org',
475 disableLocalSearch: true,
476 isDefaultSearch: true
481 merge(newCustomConfig, options.newConfig)
483 return this.updateCustomConfig({ ...options, newCustomConfig })