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: {
42 resolutions?: 'min' | 'max' // Default max
44 const { allowReplay, transcoding, resolutions = 'max' } = options
46 return this.updateExistingSubConfig({
50 allowReplay: allowReplay ?? true,
52 enabled: transcoding ?? true,
53 resolutions: ConfigCommand.getCustomConfigResolutions(resolutions === 'max')
60 disableTranscoding () {
61 return this.updateExistingSubConfig({
73 enableTranscoding (webtorrent = true, hls = true) {
74 return this.updateExistingSubConfig({
79 allowAudioFiles: true,
80 allowAdditionalExtensions: true,
82 resolutions: ConfigCommand.getCustomConfigResolutions(true),
95 enableMinimumTranscoding (webtorrent = true, hls = true) {
96 return this.updateExistingSubConfig({
101 ...ConfigCommand.getCustomConfigResolutions(false),
118 return this.updateExistingSubConfig({
127 getConfig (options: OverrideCommandOptions = {}) {
128 const path = '/api/v1/config'
130 return this.getRequestBody<ServerConfig>({
134 implicitToken: false,
135 defaultExpectedStatus: HttpStatusCode.OK_200
139 async getIndexHTMLConfig (options: OverrideCommandOptions = {}) {
140 const text = await this.getRequestText({
144 implicitToken: false,
145 defaultExpectedStatus: HttpStatusCode.OK_200
148 const match = text.match('<script type="application/javascript">window.PeerTubeServerConfig = (".+?")</script>')
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
154 getAbout (options: OverrideCommandOptions = {}) {
155 const path = '/api/v1/config/about'
157 return this.getRequestBody<About>({
161 implicitToken: false,
162 defaultExpectedStatus: HttpStatusCode.OK_200
166 getCustomConfig (options: OverrideCommandOptions = {}) {
167 const path = '/api/v1/config/custom'
169 return this.getRequestBody<CustomConfig>({
174 defaultExpectedStatus: HttpStatusCode.OK_200
178 updateCustomConfig (options: OverrideCommandOptions & {
179 newCustomConfig: CustomConfig
181 const path = '/api/v1/config/custom'
183 return this.putBodyRequest({
187 fields: options.newCustomConfig,
189 defaultExpectedStatus: HttpStatusCode.OK_200
193 deleteCustomConfig (options: OverrideCommandOptions = {}) {
194 const path = '/api/v1/config/custom'
196 return this.deleteRequest({
201 defaultExpectedStatus: HttpStatusCode.OK_200
205 async updateExistingSubConfig (options: OverrideCommandOptions & {
206 newConfig: DeepPartial<CustomConfig>
208 const existing = await this.getCustomConfig({ ...options, expectedStatus: HttpStatusCode.OK_200 })
210 return this.updateCustomConfig({ ...options, newCustomConfig: merge({}, existing, options.newConfig) })
213 updateCustomSubConfig (options: OverrideCommandOptions & {
214 newConfig: DeepPartial<CustomConfig>
216 const newCustomConfig: CustomConfig = {
218 name: 'PeerTube updated',
219 shortDescription: 'my short description',
220 description: 'my super description',
221 terms: 'my super terms',
222 codeOfConduct: 'my super coc',
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',
231 languages: [ 'en', 'es' ],
232 categories: [ 1, 2 ],
235 defaultNSFWPolicy: 'blur',
237 defaultClientRoute: '/videos/recently-added',
240 javascript: 'alert("coucou")',
241 css: 'body { background-color: red; }'
249 username: '@MySuperUsername',
256 preferAuthorDisplayName: false
261 redirectOnSingleExternalAuth: false
279 requiresEmailVerification: false,
283 email: 'superadmin1@example.com'
290 videoQuotaDaily: 318742
297 allowAdditionalExtensions: true,
298 allowAudioFiles: true,
327 maxInstanceLives: -1,
362 enabled: [ 'hot', 'most-viewed', 'most-liked' ],
377 manualApproval: false
386 indexUrl: 'https://instances.joinpeertube.org/api/v1/instances/hosts',
404 url: 'https://search.joinpeertube.org',
405 disableLocalSearch: true,
406 isDefaultSearch: true
411 merge(newCustomConfig, options.newConfig)
413 return this.updateCustomConfig({ ...options, newCustomConfig })