X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fplugins.ts;h=f8a0d19ca519d550591709b5aa905b99db150d71;hb=8872828d59a5152e27734711ae30ebe86e84f570;hp=f17e8cab9cc0fdf815431bcb11240456de7cd675;hpb=dba85a1e9e9f603ba52e1ea42deaf3fdd799b1d8;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts index f17e8cab9..f8a0d19ca 100644 --- a/server/controllers/api/plugins.ts +++ b/server/controllers/api/plugins.ts @@ -8,22 +8,39 @@ import { setDefaultPagination, setDefaultSort } from '../../middlewares' -import { pluginsSortValidator } from '../../middlewares/validators' +import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators' import { PluginModel } from '../../models/server/plugin' import { UserRight } from '../../../shared/models/users' import { existingPluginValidator, - installPluginValidator, + installOrUpdatePluginValidator, + listAvailablePluginsValidator, listPluginsValidator, uninstallPluginValidator, updatePluginSettingsValidator } from '../../middlewares/validators/plugins' import { PluginManager } from '../../lib/plugins/plugin-manager' -import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model' +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' +import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model' +import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting' const pluginRouter = express.Router() +pluginRouter.get('/available', + authenticate, + ensureUserHasRight(UserRight.MANAGE_PLUGINS), + listAvailablePluginsValidator, + paginationValidator, + availablePluginsSortValidator, + setDefaultSort, + setDefaultPagination, + asyncMiddleware(listAvailablePlugins) +) + pluginRouter.get('/', authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), @@ -35,18 +52,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), - asyncMiddleware(getPluginRegisteredSettings) + getPublicPluginSettings ) pluginRouter.put('/:npmName/settings', @@ -57,13 +72,27 @@ 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), - installPluginValidator, + installOrUpdatePluginValidator, asyncMiddleware(installPlugin) ) +pluginRouter.post('/update', + authenticate, + ensureUserHasRight(UserRight.MANAGE_PLUGINS), + installOrUpdatePluginValidator, + asyncMiddleware(updatePlugin) +) + pluginRouter.post('/uninstall', authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), @@ -80,10 +109,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 @@ -99,11 +130,33 @@ function getPlugin (req: express.Request, res: express.Response) { } async function installPlugin (req: express.Request, res: express.Response) { - const body: InstallPlugin = req.body + const body: InstallOrUpdatePlugin = req.body + + const fromDisk = !!body.path + const toInstall = body.npmName || body.path + try { + const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk) + + return res.json(plugin.toFormattedJSON()) + } catch (err) { + logger.warn('Cannot install plugin %s.', toInstall, { err }) + return res.sendStatus(400) + } +} - await PluginManager.Instance.install(body.npmName) +async function updatePlugin (req: express.Request, res: express.Response) { + const body: InstallOrUpdatePlugin = req.body - return res.sendStatus(204) + const fromDisk = !!body.path + const toUpdate = body.npmName || body.path + try { + const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk) + + return res.json(plugin.toFormattedJSON()) + } catch (err) { + logger.warn('Cannot update plugin %s.', toUpdate, { err }) + return res.sendStatus(400) + } } async function uninstallPlugin (req: express.Request, res: express.Response) { @@ -114,14 +167,22 @@ async function uninstallPlugin (req: express.Request, res: express.Response) { return res.sendStatus(204) } -async function getPluginRegisteredSettings (req: express.Request, res: express.Response) { +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 settings = await PluginManager.Instance.getSettings(plugin.name) + const json: PublicServerSetting = { publicSettings } - return res.json({ - settings - }) + return res.json(json) +} + +function getPluginRegisteredSettings (req: express.Request, res: express.Response) { + const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) + + const json: RegisteredServerSettings = { registeredSettings } + + return res.json(json) } async function updatePluginSettings (req: express.Request, res: express.Response) { @@ -130,5 +191,21 @@ async function updatePluginSettings (req: express.Request, res: express.Response plugin.settings = req.body.settings await plugin.save() + await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings) + return res.sendStatus(204) } + +async function listAvailablePlugins (req: express.Request, res: express.Response) { + const query: PeertubePluginIndexList = req.query + + const resultList = await listAvailablePluginsFromIndex(query) + + if (!resultList) { + return res.status(503) + .json({ error: 'Plugin index unavailable. Please retry later' }) + .end() + } + + return res.json(resultList) +}