1 import { Hooks } from '@server/lib/plugins/hooks'
2 import * as express from 'express'
3 import { remove, writeJSON } from 'fs-extra'
4 import { snakeCase } from 'lodash'
5 import validator from 'validator'
6 import { RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig, UserRight } from '../../../shared'
7 import { About } from '../../../shared/models/server/about.model'
8 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
9 import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
10 import { objectConverter } from '../../helpers/core-utils'
11 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
12 import { getServerCommit } from '../../helpers/utils'
13 import { CONFIG, isEmailEnabled, reloadConfig } from '../../initializers/config'
14 import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '../../initializers/constants'
15 import { ClientHtml } from '../../lib/client-html'
16 import { PluginManager } from '../../lib/plugins/plugin-manager'
17 import { getThemeOrDefault } from '../../lib/plugins/theme-utils'
18 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
19 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
21 const configRouter = express.Router()
23 const auditLogger = auditLoggerFactory('config')
25 configRouter.get('/about', getAbout)
27 asyncMiddleware(getConfig)
30 configRouter.get('/custom',
32 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
35 configRouter.put('/custom',
37 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
38 customConfigUpdateValidator,
39 asyncMiddleware(updateCustomConfig)
41 configRouter.delete('/custom',
43 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
44 asyncMiddleware(deleteCustomConfig)
47 let serverCommit: string
49 async function getConfig (req: express.Request, res: express.Response) {
50 const { allowed } = await Hooks.wrapPromiseFun(
55 'filter:api.user.signup.allowed.result'
58 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
59 const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
61 if (serverCommit === undefined) serverCommit = await getServerCommit()
63 const json: ServerConfig = {
65 name: CONFIG.INSTANCE.NAME,
66 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
67 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
68 isNSFW: CONFIG.INSTANCE.IS_NSFW,
69 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
71 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
72 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
77 users: CONFIG.SEARCH.REMOTE_URI.USERS,
78 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
82 registered: getRegisteredPlugins(),
83 registeredExternalAuths: getExternalAuthsPlugins(),
84 registeredIdAndPassAuths: getIdAndPassAuthPlugins()
87 registered: getRegisteredThemes(),
91 enabled: isEmailEnabled()
94 enabled: CONFIG.CONTACT_FORM.ENABLED
96 serverVersion: PEERTUBE_VERSION,
101 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
105 enabled: CONFIG.TRANSCODING.HLS.ENABLED
108 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
110 enabledResolutions: getEnabledResolutions()
115 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
118 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
125 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
132 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
134 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
139 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
141 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
145 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
151 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
153 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
157 videoQuota: CONFIG.USER.VIDEO_QUOTA,
158 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
162 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
166 enabled: CONFIG.TRACKER.ENABLED
172 indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
178 return res.json(json)
181 function getAbout (req: express.Request, res: express.Response) {
182 const about: About = {
184 name: CONFIG.INSTANCE.NAME,
185 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
186 description: CONFIG.INSTANCE.DESCRIPTION,
187 terms: CONFIG.INSTANCE.TERMS,
188 codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT,
190 hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION,
192 creationReason: CONFIG.INSTANCE.CREATION_REASON,
193 moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION,
194 administrator: CONFIG.INSTANCE.ADMINISTRATOR,
195 maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME,
196 businessModel: CONFIG.INSTANCE.BUSINESS_MODEL,
198 languages: CONFIG.INSTANCE.LANGUAGES,
199 categories: CONFIG.INSTANCE.CATEGORIES
203 return res.json(about).end()
206 function getCustomConfig (req: express.Request, res: express.Response) {
207 const data = customConfig()
209 return res.json(data).end()
212 async function deleteCustomConfig (req: express.Request, res: express.Response) {
213 await remove(CONFIG.CUSTOM_FILE)
215 auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
218 ClientHtml.invalidCache()
220 const data = customConfig()
222 return res.json(data).end()
225 async function updateCustomConfig (req: express.Request, res: express.Response) {
226 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
228 // camelCase to snake_case key + Force number conversion
229 const toUpdateJSON = convertCustomConfigBody(req.body)
231 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
234 ClientHtml.invalidCache()
236 const data = customConfig()
239 getAuditIdFromRes(res),
240 new CustomConfigAuditView(data),
241 oldCustomConfigAuditKeys
244 return res.json(data).end()
247 function getRegisteredThemes () {
248 return PluginManager.Instance.getRegisteredThemes()
252 description: t.description,
254 clientScripts: t.clientScripts
258 function getEnabledResolutions () {
259 return Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
260 .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
261 .map(r => parseInt(r, 10))
264 function getRegisteredPlugins () {
265 return PluginManager.Instance.getRegisteredPlugins()
269 description: p.description,
270 clientScripts: p.clientScripts
274 function getIdAndPassAuthPlugins () {
275 const result: RegisteredIdAndPassAuthConfig[] = []
277 for (const p of PluginManager.Instance.getIdAndPassAuths()) {
278 for (const auth of p.idAndPassAuths) {
281 authName: auth.authName,
282 weight: auth.getWeight()
290 function getExternalAuthsPlugins () {
291 const result: RegisteredExternalAuthConfig[] = []
293 for (const p of PluginManager.Instance.getExternalAuths()) {
294 for (const auth of p.externalAuths) {
297 authName: auth.authName,
298 authDisplayName: auth.authDisplayName
306 // ---------------------------------------------------------------------------
310 getEnabledResolutions,
311 getRegisteredPlugins,
315 // ---------------------------------------------------------------------------
317 function customConfig (): CustomConfig {
320 name: CONFIG.INSTANCE.NAME,
321 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
322 description: CONFIG.INSTANCE.DESCRIPTION,
323 terms: CONFIG.INSTANCE.TERMS,
324 codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT,
326 creationReason: CONFIG.INSTANCE.CREATION_REASON,
327 moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION,
328 administrator: CONFIG.INSTANCE.ADMINISTRATOR,
329 maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME,
330 businessModel: CONFIG.INSTANCE.BUSINESS_MODEL,
331 hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION,
333 languages: CONFIG.INSTANCE.LANGUAGES,
334 categories: CONFIG.INSTANCE.CATEGORIES,
336 isNSFW: CONFIG.INSTANCE.IS_NSFW,
337 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
338 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
340 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
341 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
345 default: CONFIG.THEME.DEFAULT
349 username: CONFIG.SERVICES.TWITTER.USERNAME,
350 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
355 size: CONFIG.CACHE.PREVIEWS.SIZE
358 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
362 enabled: CONFIG.SIGNUP.ENABLED,
363 limit: CONFIG.SIGNUP.LIMIT,
364 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
367 email: CONFIG.ADMIN.EMAIL
370 enabled: CONFIG.CONTACT_FORM.ENABLED
373 videoQuota: CONFIG.USER.VIDEO_QUOTA,
374 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
377 enabled: CONFIG.TRANSCODING.ENABLED,
378 allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS,
379 allowAudioFiles: CONFIG.TRANSCODING.ALLOW_AUDIO_FILES,
380 threads: CONFIG.TRANSCODING.THREADS,
382 '0p': CONFIG.TRANSCODING.RESOLUTIONS['0p'],
383 '240p': CONFIG.TRANSCODING.RESOLUTIONS['240p'],
384 '360p': CONFIG.TRANSCODING.RESOLUTIONS['360p'],
385 '480p': CONFIG.TRANSCODING.RESOLUTIONS['480p'],
386 '720p': CONFIG.TRANSCODING.RESOLUTIONS['720p'],
387 '1080p': CONFIG.TRANSCODING.RESOLUTIONS['1080p'],
388 '2160p': CONFIG.TRANSCODING.RESOLUTIONS['2160p']
391 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
394 enabled: CONFIG.TRANSCODING.HLS.ENABLED
400 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
403 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
410 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
416 enabled: CONFIG.FOLLOWERS.INSTANCE.ENABLED,
417 manualApproval: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL
423 enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED
427 enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED,
428 indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL
435 function convertCustomConfigBody (body: CustomConfig) {
436 function keyConverter (k: string) {
437 // Transcoding resolutions exception
438 if (/^\d{3,4}p$/.exec(k)) return k
439 if (k === '0p') return k
444 function valueConverter (v: any) {
445 if (validator.isNumeric(v + '')) return parseInt('' + v, 10)
450 return objectConverter(body, keyConverter, valueConverter)