X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fconfig.ts;h=85f3ad3d942d114c04aaab5c7790f3e5385e7167;hb=ebefc902f59be6c5542c19ff706e310d9dddf75f;hp=27c416a12bc75c995ca83394056c0e6198518275;hpb=a41b9da1a9ce49df82ea10c82de4c2fbc6d1b189;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 27c416a12..85f3ad3d9 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -1,22 +1,23 @@ +import { Hooks } from '@server/lib/plugins/hooks' import * as express from 'express' +import { remove, writeJSON } from 'fs-extra' import { snakeCase } from 'lodash' -import { ServerConfig, UserRight } from '../../../shared' +import validator from 'validator' +import { RegisteredExternalAuthConfig, RegisteredIdAndPassAuthConfig, ServerConfig, UserRight } from '../../../shared' import { About } from '../../../shared/models/server/about.model' import { CustomConfig } from '../../../shared/models/server/custom-config.model' +import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' +import { objectConverter } from '../../helpers/core-utils' import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' -import { CONSTRAINTS_FIELDS } from '../../initializers/constants' +import { getServerCommit } from '../../helpers/utils' +import { CONFIG, isEmailEnabled, reloadConfig } from '../../initializers/config' +import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION } from '../../initializers/constants' +import { ClientHtml } from '../../lib/client-html' +import { PluginManager } from '../../lib/plugins/plugin-manager' +import { getThemeOrDefault } from '../../lib/plugins/theme-utils' import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' import { customConfigUpdateValidator } from '../../middlewares/validators/config' -import { ClientHtml } from '../../lib/client-html' -import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger' -import { remove, writeJSON } from 'fs-extra' -import { getServerCommit } from '../../helpers/utils' -import { Emailer } from '../../lib/emailer' -import { isNumeric } from 'validator' -import { objectConverter } from '../../helpers/core-utils' -import { CONFIG, reloadConfig } from '../../initializers/config' -const packageJSON = require('../../../../package.json') const configRouter = express.Router() const auditLogger = auditLoggerFactory('config') @@ -29,12 +30,12 @@ configRouter.get('/', configRouter.get('/custom', authenticate, ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), - asyncMiddleware(getCustomConfig) + getCustomConfig ) configRouter.put('/custom', authenticate, ensureUserHasRight(UserRight.MANAGE_CONFIGURATION), - asyncMiddleware(customConfigUpdateValidator), + customConfigUpdateValidator, asyncMiddleware(updateCustomConfig) ) configRouter.delete('/custom', @@ -44,16 +45,21 @@ configRouter.delete('/custom', ) let serverCommit: string + async function getConfig (req: express.Request, res: express.Response) { - const allowed = await isSignupAllowed() + const { allowed } = await Hooks.wrapPromiseFun( + isSignupAllowed, + { + ip: req.ip + }, + 'filter:api.user.signup.allowed.result' + ) + const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) + const defaultTheme = getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) if (serverCommit === undefined) serverCommit = await getServerCommit() - const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) - .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true) - .map(r => parseInt(r, 10)) - const json: ServerConfig = { instance: { name: CONFIG.INSTANCE.NAME, @@ -66,13 +72,28 @@ async function getConfig (req: express.Request, res: express.Response) { css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS } }, + search: { + remoteUri: { + users: CONFIG.SEARCH.REMOTE_URI.USERS, + anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS + } + }, + plugin: { + registered: getRegisteredPlugins(), + registeredExternalAuths: getExternalAuthsPlugins(), + registeredIdAndPassAuths: getIdAndPassAuthPlugins() + }, + theme: { + registered: getRegisteredThemes(), + default: defaultTheme + }, email: { - enabled: Emailer.isEnabled() + enabled: isEmailEnabled() }, contactForm: { enabled: CONFIG.CONTACT_FORM.ENABLED }, - serverVersion: packageJSON.version, + serverVersion: PEERTUBE_VERSION, serverCommit, signup: { allowed, @@ -83,7 +104,10 @@ async function getConfig (req: express.Request, res: express.Response) { hls: { enabled: CONFIG.TRANSCODING.HLS.ENABLED }, - enabledResolutions + webtorrent: { + enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED + }, + enabledResolutions: getEnabledResolutions() }, import: { videos: { @@ -140,6 +164,14 @@ async function getConfig (req: express.Request, res: express.Response) { }, tracker: { enabled: CONFIG.TRACKER.ENABLED + }, + + followings: { + instance: { + autoFollowIndex: { + indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL + } + } } } @@ -152,14 +184,26 @@ function getAbout (req: express.Request, res: express.Response) { name: CONFIG.INSTANCE.NAME, shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, description: CONFIG.INSTANCE.DESCRIPTION, - terms: CONFIG.INSTANCE.TERMS + terms: CONFIG.INSTANCE.TERMS, + codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT, + + hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION, + + creationReason: CONFIG.INSTANCE.CREATION_REASON, + moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION, + administrator: CONFIG.INSTANCE.ADMINISTRATOR, + maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME, + businessModel: CONFIG.INSTANCE.BUSINESS_MODEL, + + languages: CONFIG.INSTANCE.LANGUAGES, + categories: CONFIG.INSTANCE.CATEGORIES } } return res.json(about).end() } -async function getCustomConfig (req: express.Request, res: express.Response) { +function getCustomConfig (req: express.Request, res: express.Response) { const data = customConfig() return res.json(data).end() @@ -200,10 +244,76 @@ async function updateCustomConfig (req: express.Request, res: express.Response) return res.json(data).end() } +function getRegisteredThemes () { + return PluginManager.Instance.getRegisteredThemes() + .map(t => ({ + name: t.name, + version: t.version, + description: t.description, + css: t.css, + clientScripts: t.clientScripts + })) +} + +function getEnabledResolutions () { + return Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) + .filter(key => CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.RESOLUTIONS[key] === true) + .map(r => parseInt(r, 10)) +} + +function getRegisteredPlugins () { + return PluginManager.Instance.getRegisteredPlugins() + .map(p => ({ + name: p.name, + version: p.version, + description: p.description, + clientScripts: p.clientScripts + })) +} + +function getIdAndPassAuthPlugins () { + const result: RegisteredIdAndPassAuthConfig[] = [] + + for (const p of PluginManager.Instance.getIdAndPassAuths()) { + for (const auth of p.idAndPassAuths) { + result.push({ + npmName: p.npmName, + name: p.name, + version: p.version, + authName: auth.authName, + weight: auth.getWeight() + }) + } + } + + return result +} + +function getExternalAuthsPlugins () { + const result: RegisteredExternalAuthConfig[] = [] + + for (const p of PluginManager.Instance.getExternalAuths()) { + for (const auth of p.externalAuths) { + result.push({ + npmName: p.npmName, + name: p.name, + version: p.version, + authName: auth.authName, + authDisplayName: auth.authDisplayName + }) + } + } + + return result +} + // --------------------------------------------------------------------------- export { - configRouter + configRouter, + getEnabledResolutions, + getRegisteredPlugins, + getRegisteredThemes } // --------------------------------------------------------------------------- @@ -215,6 +325,18 @@ function customConfig (): CustomConfig { shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, description: CONFIG.INSTANCE.DESCRIPTION, terms: CONFIG.INSTANCE.TERMS, + codeOfConduct: CONFIG.INSTANCE.CODE_OF_CONDUCT, + + creationReason: CONFIG.INSTANCE.CREATION_REASON, + moderationInformation: CONFIG.INSTANCE.MODERATION_INFORMATION, + administrator: CONFIG.INSTANCE.ADMINISTRATOR, + maintenanceLifetime: CONFIG.INSTANCE.MAINTENANCE_LIFETIME, + businessModel: CONFIG.INSTANCE.BUSINESS_MODEL, + hardwareInformation: CONFIG.INSTANCE.HARDWARE_INFORMATION, + + languages: CONFIG.INSTANCE.LANGUAGES, + categories: CONFIG.INSTANCE.CATEGORIES, + isNSFW: CONFIG.INSTANCE.IS_NSFW, defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE, defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, @@ -223,6 +345,9 @@ function customConfig (): CustomConfig { javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT } }, + theme: { + default: CONFIG.THEME.DEFAULT + }, services: { twitter: { username: CONFIG.SERVICES.TWITTER.USERNAME, @@ -258,11 +383,16 @@ function customConfig (): CustomConfig { allowAudioFiles: CONFIG.TRANSCODING.ALLOW_AUDIO_FILES, threads: CONFIG.TRANSCODING.THREADS, resolutions: { - '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ], - '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ], - '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ], - '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ], - '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ] + '0p': CONFIG.TRANSCODING.RESOLUTIONS['0p'], + '240p': CONFIG.TRANSCODING.RESOLUTIONS['240p'], + '360p': CONFIG.TRANSCODING.RESOLUTIONS['360p'], + '480p': CONFIG.TRANSCODING.RESOLUTIONS['480p'], + '720p': CONFIG.TRANSCODING.RESOLUTIONS['720p'], + '1080p': CONFIG.TRANSCODING.RESOLUTIONS['1080p'], + '2160p': CONFIG.TRANSCODING.RESOLUTIONS['2160p'] + }, + webtorrent: { + enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED }, hls: { enabled: CONFIG.TRANSCODING.HLS.ENABLED @@ -290,6 +420,18 @@ function customConfig (): CustomConfig { enabled: CONFIG.FOLLOWERS.INSTANCE.ENABLED, manualApproval: CONFIG.FOLLOWERS.INSTANCE.MANUAL_APPROVAL } + }, + followings: { + instance: { + autoFollowBack: { + enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED + }, + + autoFollowIndex: { + enabled: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.ENABLED, + indexUrl: CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_INDEX.INDEX_URL + } + } } } } @@ -298,12 +440,13 @@ function convertCustomConfigBody (body: CustomConfig) { function keyConverter (k: string) { // Transcoding resolutions exception if (/^\d{3,4}p$/.exec(k)) return k + if (k === '0p') return k return snakeCase(k) } function valueConverter (v: any) { - if (isNumeric(v + '')) return parseInt('' + v, 10) + if (validator.isNumeric(v + '')) return parseInt('' + v, 10) return v }