X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fplugins.ts;h=1e6a02c496f17503f49dad18ac8da0089a95c953;hb=79db409a41bd28fd2773626c9a93b5d326a38bc0;hp=14675fdf30452e25681a8e5da69cc5f5c680505e;hpb=b5f919ac8eb2a1c20e26582fdfd377d687710d8f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts index 14675fdf3..1e6a02c49 100644 --- a/server/controllers/api/plugins.ts +++ b/server/controllers/api/plugins.ts @@ -1,31 +1,54 @@ 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, + openapiOperationDoc, paginationValidator, + pluginsSortValidator, setDefaultPagination, setDefaultSort -} from '../../middlewares' -import { pluginsSortValidator } from '../../middlewares/validators' -import { PluginModel } from '../../models/server/plugin' -import { UserRight } from '../../../shared/models/users' +} from '@server/middlewares' import { existingPluginValidator, installOrUpdatePluginValidator, + listAvailablePluginsValidator, 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' +} 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() +pluginRouter.get('/available', + openapiOperationDoc({ operationId: 'getAvailablePlugins' }), + authenticate, + ensureUserHasRight(UserRight.MANAGE_PLUGINS), + listAvailablePluginsValidator, + paginationValidator, + availablePluginsSortValidator, + setDefaultSort, + setDefaultPagination, + asyncMiddleware(listAvailablePlugins) +) + pluginRouter.get('/', + openapiOperationDoc({ operationId: 'getPlugins' }), authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), listPluginsValidator, @@ -36,18 +59,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', @@ -58,7 +79,15 @@ pluginRouter.put('/:npmName/settings', asyncMiddleware(updatePluginSettings) ) +pluginRouter.get('/:npmName', + authenticate, + ensureUserHasRight(UserRight.MANAGE_PLUGINS), + asyncMiddleware(existingPluginValidator), + getPlugin +) + pluginRouter.post('/install', + openapiOperationDoc({ operationId: 'addPlugin' }), authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), installOrUpdatePluginValidator, @@ -66,6 +95,7 @@ pluginRouter.post('/install', ) pluginRouter.post('/update', + openapiOperationDoc({ operationId: 'updatePlugin' }), authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), installOrUpdatePluginValidator, @@ -73,6 +103,7 @@ pluginRouter.post('/update', ) pluginRouter.post('/uninstall', + openapiOperationDoc({ operationId: 'uninstallPlugin' }), authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), uninstallPluginValidator, @@ -88,10 +119,12 @@ export { // --------------------------------------------------------------------------- async function listPlugins (req: express.Request, res: express.Response) { - const type = req.query.type + const pluginType = req.query.pluginType + const uninstalled = req.query.uninstalled const resultList = await PluginModel.listForApi({ - type, + pluginType, + uninstalled, start: req.query.start, count: req.query.count, sort: req.query.sort @@ -117,7 +150,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.fail({ message: 'Cannot install plugin ' + toInstall }) } } @@ -127,12 +160,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.fail({ message: 'Cannot update plugin ' + toUpdate }) } } @@ -141,15 +174,25 @@ async function uninstallPlugin (req: express.Request, res: express.Response) { await PluginManager.Instance.uninstall(body.npmName) - return res.sendStatus(204) + return res.status(HttpStatusCode.NO_CONTENT_204).end() +} + +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) { @@ -158,5 +201,22 @@ 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.status(HttpStatusCode.NO_CONTENT_204).end() +} + +async function listAvailablePlugins (req: express.Request, res: express.Response) { + const query: PeertubePluginIndexList = req.query + + const resultList = await listAvailablePluginsFromIndex(query) + + if (!resultList) { + return res.fail({ + status: HttpStatusCode.SERVICE_UNAVAILABLE_503, + message: 'Plugin index unavailable. Please retry later' + }) + } + + return res.json(resultList) }