]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/plugins.ts
Add ability to search available plugins
[github/Chocobozzz/PeerTube.git] / server / controllers / api / plugins.ts
index f17e8cab9cc0fdf815431bcb11240456de7cd675..114cc49b6bd34ed6eb305a47884a32b73abd5418 100644 (file)
@@ -8,22 +8,37 @@ 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'
 
 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),
@@ -46,7 +61,7 @@ pluginRouter.get('/:npmName/registered-settings',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
   asyncMiddleware(existingPluginValidator),
-  asyncMiddleware(getPluginRegisteredSettings)
+  getPluginRegisteredSettings
 )
 
 pluginRouter.put('/:npmName/settings',
@@ -60,10 +75,17 @@ pluginRouter.put('/:npmName/settings',
 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 +102,10 @@ export {
 // ---------------------------------------------------------------------------
 
 async function listPlugins (req: express.Request, res: express.Response) {
-  const type = req.query.type
+  const pluginType = req.query.pluginType
 
   const resultList = await PluginModel.listForApi({
-    type,
+    pluginType,
     start: req.query.start,
     count: req.query.count,
     sort: req.query.sort
@@ -99,11 +121,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,10 +158,8 @@ async function uninstallPlugin (req: express.Request, res: express.Response) {
   return res.sendStatus(204)
 }
 
-async function getPluginRegisteredSettings (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
@@ -132,3 +174,11 @@ async function updatePluginSettings (req: express.Request, res: express.Response
 
   return res.sendStatus(204)
 }
+
+async function listAvailablePlugins (req: express.Request, res: express.Response) {
+  const query: PeertubePluginIndexList = req.query
+
+  const resultList = await listAvailablePluginsFromIndex(query)
+
+  return res.json(resultList)
+}