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),
114 getConfig (options: OverrideCommandOptions = {}) {
115 const path = '/api/v1/config'
117 return this.getRequestBody<ServerConfig>({
121 implicitToken: false,
122 defaultExpectedStatus: HttpStatusCode.OK_200
126 async getIndexHTMLConfig (options: OverrideCommandOptions = {}) {
127 const text = await this.getRequestText({
131 implicitToken: false,
132 defaultExpectedStatus: HttpStatusCode.OK_200
135 const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>')
137 // We parse the string twice, first to extract the string and then to extract the JSON
138 return JSON.parse(JSON.parse(match[1])) as ServerConfig
141 getAbout (options: OverrideCommandOptions = {}) {
142 const path = '/api/v1/config/about'
144 return this.getRequestBody<About>({
148 implicitToken: false,
149 defaultExpectedStatus: HttpStatusCode.OK_200
153 getCustomConfig (options: OverrideCommandOptions = {}) {
154 const path = '/api/v1/config/custom'
156 return this.getRequestBody<CustomConfig>({
161 defaultExpectedStatus: HttpStatusCode.OK_200
165 updateCustomConfig (options: OverrideCommandOptions & {
166 newCustomConfig: CustomConfig
168 const path = '/api/v1/config/custom'
170 return this.putBodyRequest({
174 fields: options.newCustomConfig,
176 defaultExpectedStatus: HttpStatusCode.OK_200
180 deleteCustomConfig (options: OverrideCommandOptions = {}) {
181 const path = '/api/v1/config/custom'
183 return this.deleteRequest({
188 defaultExpectedStatus: HttpStatusCode.OK_200
192 async updateExistingSubConfig (options: OverrideCommandOptions & {
193 newConfig: DeepPartial<CustomConfig>
195 const existing = await this.getCustomConfig({ ...options, expectedStatus: HttpStatusCode.OK_200 })
197 return this.updateCustomConfig({ ...options, newCustomConfig: merge({}, existing, options.newConfig) })
200 updateCustomSubConfig (options: OverrideCommandOptions & {
201 newConfig: DeepPartial<CustomConfig>
203 const newCustomConfig: CustomConfig = {
205 name: 'PeerTube updated',
206 shortDescription: 'my short description',
207 description: 'my super description',
208 terms: 'my super terms',
209 codeOfConduct: 'my super coc',
211 creationReason: 'my super creation reason',
212 moderationInformation: 'my super moderation information',
213 administrator: 'Kuja',
214 maintenanceLifetime: 'forever',
215 businessModel: 'my super business model',
216 hardwareInformation: '2vCore 3GB RAM',
218 languages: [ 'en', 'es' ],
219 categories: [ 1, 2 ],
222 defaultNSFWPolicy: 'blur',
224 defaultClientRoute: '/videos/recently-added',
227 javascript: 'alert("coucou")',
228 css: 'body { background-color: red; }'
236 username: '@MySuperUsername',
243 preferAuthorDisplayName: false
248 redirectOnSingleExternalAuth: false
266 requiresEmailVerification: false,
270 email: 'superadmin1@example.com'
277 videoQuotaDaily: 318742
284 allowAdditionalExtensions: true,
285 allowAudioFiles: true,
314 maxInstanceLives: -1,
349 enabled: [ 'best', 'hot', 'most-viewed', 'most-liked' ],
364 manualApproval: false
373 indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
391 url: 'https://search.joinpeertube.org',
392 disableLocalSearch: true,
393 isDefaultSearch: true
398 merge(newCustomConfig, options.newConfig)
400 return this.updateCustomConfig({ ...options, newCustomConfig })