X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fplugins.ts;h=e18eed332765e7905b1efbcfc146dccb0c81a803;hb=5ec3cbdf22fc88ebe57f370fc0bc0e3df7453458;hp=bb410e8917672586ebaf019d314e67ecee317ae5;hpb=e0ce715a1ded6e84c877004dae3e354c8716fb06;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts index bb410e891..e18eed332 100644 --- a/server/controllers/api/plugins.ts +++ b/server/controllers/api/plugins.ts @@ -1,16 +1,18 @@ import * as express from 'express' -import { getFormattedObjects } from '../../helpers/utils' +import { logger } from '@server/helpers/logger' +import { getFormattedObjects } from '@server/helpers/utils' +import { listAvailablePluginsFromIndex } from '@server/lib/plugins/plugin-index' +import { PluginManager } from '@server/lib/plugins/plugin-manager' import { asyncMiddleware, authenticate, + availablePluginsSortValidator, ensureUserHasRight, paginationValidator, + pluginsSortValidator, setDefaultPagination, setDefaultSort -} from '../../middlewares' -import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators' -import { PluginModel } from '../../models/server/plugin' -import { UserRight } from '../../../shared/models/users' +} from '@server/middlewares' import { existingPluginValidator, installOrUpdatePluginValidator, @@ -18,13 +20,17 @@ import { listPluginsValidator, uninstallPluginValidator, updatePluginSettingsValidator -} from '../../middlewares/validators/plugins' -import { PluginManager } from '../../lib/plugins/plugin-manager' -import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model' -import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model' -import { logger } from '../../helpers/logger' -import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index' -import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model' +} from '@server/middlewares/validators/plugins' +import { PluginModel } from '@server/models/server/plugin' +import { HttpStatusCode } from '@shared/core-utils' +import { + InstallOrUpdatePlugin, + ManagePlugin, + PeertubePluginIndexList, + PublicServerSetting, + RegisteredServerSettings, + UserRight +} from '@shared/models' const pluginRouter = express.Router() @@ -50,18 +56,16 @@ pluginRouter.get('/', asyncMiddleware(listPlugins) ) -pluginRouter.get('/:npmName', +pluginRouter.get('/:npmName/registered-settings', authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), asyncMiddleware(existingPluginValidator), - getPlugin + getPluginRegisteredSettings ) -pluginRouter.get('/:npmName/registered-settings', - authenticate, - ensureUserHasRight(UserRight.MANAGE_PLUGINS), +pluginRouter.get('/:npmName/public-settings', asyncMiddleware(existingPluginValidator), - getPluginRegisteredSettings + getPublicPluginSettings ) pluginRouter.put('/:npmName/settings', @@ -72,6 +76,13 @@ pluginRouter.put('/:npmName/settings', asyncMiddleware(updatePluginSettings) ) +pluginRouter.get('/:npmName', + authenticate, + ensureUserHasRight(UserRight.MANAGE_PLUGINS), + asyncMiddleware(existingPluginValidator), + getPlugin +) + pluginRouter.post('/install', authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), @@ -103,9 +114,11 @@ export { async function listPlugins (req: express.Request, res: express.Response) { const pluginType = req.query.pluginType + const uninstalled = req.query.uninstalled const resultList = await PluginModel.listForApi({ pluginType, + uninstalled, start: req.query.start, count: req.query.count, sort: req.query.sort @@ -131,7 +144,7 @@ async function installPlugin (req: express.Request, res: express.Response) { return res.json(plugin.toFormattedJSON()) } catch (err) { logger.warn('Cannot install plugin %s.', toInstall, { err }) - return res.sendStatus(400) + return res.sendStatus(HttpStatusCode.BAD_REQUEST_400) } } @@ -141,12 +154,12 @@ async function updatePlugin (req: express.Request, res: express.Response) { const fromDisk = !!body.path const toUpdate = body.npmName || body.path try { - const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk) + const plugin = await PluginManager.Instance.update(toUpdate, fromDisk) return res.json(plugin.toFormattedJSON()) } catch (err) { logger.warn('Cannot update plugin %s.', toUpdate, { err }) - return res.sendStatus(400) + return res.sendStatus(HttpStatusCode.BAD_REQUEST_400) } } @@ -155,15 +168,25 @@ async function uninstallPlugin (req: express.Request, res: express.Response) { await PluginManager.Instance.uninstall(body.npmName) - return res.sendStatus(204) + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +} + +function getPublicPluginSettings (req: express.Request, res: express.Response) { + const plugin = res.locals.plugin + const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) + const publicSettings = plugin.getPublicSettings(registeredSettings) + + const json: PublicServerSetting = { publicSettings } + + return res.json(json) } function getPluginRegisteredSettings (req: express.Request, res: express.Response) { - const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) + const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) - return res.json({ - settings - }) + const json: RegisteredServerSettings = { registeredSettings } + + return res.json(json) } async function updatePluginSettings (req: express.Request, res: express.Response) { @@ -172,7 +195,9 @@ async function updatePluginSettings (req: express.Request, res: express.Response plugin.settings = req.body.settings await plugin.save() - return res.sendStatus(204) + await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings) + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) } async function listAvailablePlugins (req: express.Request, res: express.Response) { @@ -181,9 +206,8 @@ async function listAvailablePlugins (req: express.Request, res: express.Response const resultList = await listAvailablePluginsFromIndex(query) if (!resultList) { - return res.status(503) + return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503) .json({ error: 'Plugin index unavailable. Please retry later' }) - .end() } return res.json(resultList)