]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/plugins.ts
CLI: plugins install command accept a --plugin-version parameter. (#4599)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / plugins.ts
index 1c0b5edb1fdd02df278e33b696ca353a6298630e..de9e055dc0fe4b9bf7db4b7c0a5484237a44ebc9 100644 (file)
@@ -1,16 +1,19 @@
-import * as express from 'express'
-import { getFormattedObjects } from '../../helpers/utils'
+import express from 'express'
+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 { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators'
-import { PluginModel } from '../../models/server/plugin'
-import { UserRight } from '../../../shared/models/users'
+} from '@server/middlewares'
 import {
   existingPluginValidator,
   installOrUpdatePluginValidator,
@@ -18,20 +21,22 @@ 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'
-import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
-import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
-import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
+} from '@server/middlewares/validators/plugins'
+import { PluginModel } from '@server/models/server/plugin'
+import {
+  HttpStatusCode,
+  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,
@@ -43,6 +48,7 @@ pluginRouter.get('/available',
 )
 
 pluginRouter.get('/',
+  openapiOperationDoc({ operationId: 'getPlugins' }),
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
   listPluginsValidator,
@@ -81,6 +87,7 @@ pluginRouter.get('/:npmName',
 )
 
 pluginRouter.post('/install',
+  openapiOperationDoc({ operationId: 'addPlugin' }),
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
   installOrUpdatePluginValidator,
@@ -88,6 +95,7 @@ pluginRouter.post('/install',
 )
 
 pluginRouter.post('/update',
+  openapiOperationDoc({ operationId: 'updatePlugin' }),
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
   installOrUpdatePluginValidator,
@@ -95,6 +103,7 @@ pluginRouter.post('/update',
 )
 
 pluginRouter.post('/uninstall',
+  openapiOperationDoc({ operationId: 'uninstallPlugin' }),
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
   uninstallPluginValidator,
@@ -135,13 +144,18 @@ async function installPlugin (req: express.Request, res: express.Response) {
 
   const fromDisk = !!body.path
   const toInstall = body.npmName || body.path
+
+  const pluginVersion = body.pluginVersion && body.npmName
+    ? body.pluginVersion
+    : undefined
+
   try {
-    const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
+    const plugin = await PluginManager.Instance.install(toInstall, pluginVersion, fromDisk)
 
     return res.json(plugin.toFormattedJSON())
   } catch (err) {
     logger.warn('Cannot install plugin %s.', toInstall, { err })
-    return res.sendStatus(HttpStatusCode.BAD_REQUEST_400)
+    return res.fail({ message: 'Cannot install plugin ' + toInstall })
   }
 }
 
@@ -151,12 +165,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(HttpStatusCode.BAD_REQUEST_400)
+    return res.fail({ message: 'Cannot update plugin ' + toUpdate })
   }
 }
 
@@ -165,7 +179,7 @@ async function uninstallPlugin (req: express.Request, res: express.Response) {
 
   await PluginManager.Instance.uninstall(body.npmName)
 
-  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 function getPublicPluginSettings (req: express.Request, res: express.Response) {
@@ -194,7 +208,7 @@ async function updatePluginSettings (req: express.Request, res: express.Response
 
   await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
 
-  return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function listAvailablePlugins (req: express.Request, res: express.Response) {
@@ -203,9 +217,10 @@ async function listAvailablePlugins (req: express.Request, res: express.Response
   const resultList = await listAvailablePluginsFromIndex(query)
 
   if (!resultList) {
-    return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503)
-      .json({ error: 'Plugin index unavailable. Please retry later' })
-      .end()
+    return res.fail({
+      status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
+      message: 'Plugin index unavailable. Please retry later'
+    })
   }
 
   return res.json(resultList)