]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/plugins.ts
WIP plugins: update plugin
[github/Chocobozzz/PeerTube.git] / server / controllers / api / plugins.ts
index 89cc67f5444d7f549c47c41fe2eb9594519891f9..14675fdf30452e25681a8e5da69cc5f5c680505e 100644 (file)
@@ -12,15 +12,16 @@ import { pluginsSortValidator } from '../../middlewares/validators'
 import { PluginModel } from '../../models/server/plugin'
 import { UserRight } from '../../../shared/models/users'
 import {
-  enabledPluginValidator,
-  installPluginValidator,
+  existingPluginValidator,
+  installOrUpdatePluginValidator,
   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'
 
 const pluginRouter = express.Router()
 
@@ -35,28 +36,42 @@ pluginRouter.get('/',
   asyncMiddleware(listPlugins)
 )
 
-pluginRouter.get('/:pluginName/settings',
+pluginRouter.get('/:npmName',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
-  asyncMiddleware(enabledPluginValidator),
-  asyncMiddleware(listPluginSettings)
+  asyncMiddleware(existingPluginValidator),
+  getPlugin
 )
 
-pluginRouter.put('/:pluginName/settings',
+pluginRouter.get('/:npmName/registered-settings',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_PLUGINS),
+  asyncMiddleware(existingPluginValidator),
+  getPluginRegisteredSettings
+)
+
+pluginRouter.put('/:npmName/settings',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
   updatePluginSettingsValidator,
-  asyncMiddleware(enabledPluginValidator),
+  asyncMiddleware(existingPluginValidator),
   asyncMiddleware(updatePluginSettings)
 )
 
 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),
@@ -85,12 +100,40 @@ async function listPlugins (req: express.Request, res: express.Response) {
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
+function getPlugin (req: express.Request, res: express.Response) {
+  const plugin = res.locals.plugin
+
+  return res.json(plugin.toFormattedJSON())
+}
+
 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) {
@@ -101,10 +144,8 @@ async function uninstallPlugin (req: express.Request, res: express.Response) {
   return res.sendStatus(204)
 }
 
-async function listPluginSettings (req: express.Request, res: express.Response) {
-  const plugin = res.locals.plugin
-
-  const settings = await PluginManager.Instance.getSettings(plugin.name)
+function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
+  const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
 
   return res.json({
     settings