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) {
22 return this.updateExistingSubConfig({
39 enableLive (options: {
43 return this.updateExistingSubConfig({
47 allowReplay: options.allowReplay ?? true,
49 enabled: options.transcoding ?? true,
50 resolutions: ConfigCommand.getCustomConfigResolutions(true)
57 disableTranscoding () {
58 return this.updateExistingSubConfig({
70 enableTranscoding (webtorrent = true, hls = true) {
71 return this.updateExistingSubConfig({
76 allowAudioFiles: true,
77 allowAdditionalExtensions: true,
79 resolutions: ConfigCommand.getCustomConfigResolutions(true),
92 enableMinimumTranscoding (webtorrent = true, hls = true) {
93 return this.updateExistingSubConfig({
98 ...ConfigCommand.getCustomConfigResolutions(false),
115 return this.updateExistingSubConfig({
124 getConfig (options: OverrideCommandOptions = {}) {
125 const path = '/api/v1/config'
127 return this.getRequestBody<ServerConfig>({
131 implicitToken: false,
132 defaultExpectedStatus: HttpStatusCode.OK_200
136 async getIndexHTMLConfig (options: OverrideCommandOptions = {}) {
137 const text = await this.getRequestText({
141 implicitToken: false,
142 defaultExpectedStatus: HttpStatusCode.OK_200
145 const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>')
147 // We parse the string twice, first to extract the string and then to extract the JSON
148 return JSON.parse(JSON.parse(match[1])) as ServerConfig
151 getAbout (options: OverrideCommandOptions = {}) {
152 const path = '/api/v1/config/about'
154 return this.getRequestBody<About>({
158 implicitToken: false,
159 defaultExpectedStatus: HttpStatusCode.OK_200
163 getCustomConfig (options: OverrideCommandOptions = {}) {
164 const path = '/api/v1/config/custom'
166 return this.getRequestBody<CustomConfig>({
171 defaultExpectedStatus: HttpStatusCode.OK_200
175 updateCustomConfig (options: OverrideCommandOptions & {
176 newCustomConfig: CustomConfig
178 const path = '/api/v1/config/custom'
180 return this.putBodyRequest({
184 fields: options.newCustomConfig,
186 defaultExpectedStatus: HttpStatusCode.OK_200
190 deleteCustomConfig (options: OverrideCommandOptions = {}) {
191 const path = '/api/v1/config/custom'
193 return this.deleteRequest({
198 defaultExpectedStatus: HttpStatusCode.OK_200
202 async updateExistingSubConfig (options: OverrideCommandOptions & {
203 newConfig: DeepPartial<CustomConfig>
205 const existing = await this.getCustomConfig({ ...options, expectedStatus: HttpStatusCode.OK_200 })
207 return this.updateCustomConfig({ ...options, newCustomConfig: merge({}, existing, options.newConfig) })
210 updateCustomSubConfig (options: OverrideCommandOptions & {
211 newConfig: DeepPartial<CustomConfig>
213 const newCustomConfig: CustomConfig = {
215 name: 'PeerTube updated',
216 shortDescription: 'my short description',
217 description: 'my super description',
218 terms: 'my super terms',
219 codeOfConduct: 'my super coc',
221 creationReason: 'my super creation reason',
222 moderationInformation: 'my super moderation information',
223 administrator: 'Kuja',
224 maintenanceLifetime: 'forever',
225 businessModel: 'my super business model',
226 hardwareInformation: '2vCore 3GB RAM',
228 languages: [ 'en', 'es' ],
229 categories: [ 1, 2 ],
232 defaultNSFWPolicy: 'blur',
234 defaultClientRoute: '/videos/recently-added',
237 javascript: 'alert("coucou")',
238 css: 'body { background-color: red; }'
246 username: '@MySuperUsername',
253 preferAuthorDisplayName: false
258 redirectOnSingleExternalAuth: false
276 requiresEmailVerification: false,
280 email: 'superadmin1@example.com'
287 videoQuotaDaily: 318742
294 allowAdditionalExtensions: true,
295 allowAudioFiles: true,
324 maxInstanceLives: -1,
359 enabled: [ 'hot', 'most-viewed', 'most-liked' ],
374 manualApproval: false
383 indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
401 url: 'https://search.joinpeertube.org',
402 disableLocalSearch: true,
403 isDefaultSearch: true
408 merge(newCustomConfig, options.newConfig)
410 return this.updateCustomConfig({ ...options, newCustomConfig })