1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
10 } from '../../middlewares'
11 import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators'
12 import { PluginModel } from '../../models/server/plugin'
13 import { UserRight } from '../../../shared/models/users'
15 existingPluginValidator,
16 installOrUpdatePluginValidator,
17 listAvailablePluginsValidator,
19 uninstallPluginValidator,
20 updatePluginSettingsValidator
21 } from '../../middlewares/validators/plugins'
22 import { PluginManager } from '../../lib/plugins/plugin-manager'
23 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
24 import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
25 import { logger } from '../../helpers/logger'
26 import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index'
27 import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
28 import { RegisteredSettings } from '../../../shared/models/plugins/register-setting.model'
30 const pluginRouter = express.Router()
32 pluginRouter.get('/available',
34 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
35 listAvailablePluginsValidator,
37 availablePluginsSortValidator,
40 asyncMiddleware(listAvailablePlugins)
45 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
51 asyncMiddleware(listPlugins)
54 pluginRouter.get('/:npmName',
56 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
57 asyncMiddleware(existingPluginValidator),
61 pluginRouter.get('/:npmName/registered-settings',
63 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
64 asyncMiddleware(existingPluginValidator),
65 getPluginRegisteredSettings
68 pluginRouter.put('/:npmName/settings',
70 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
71 updatePluginSettingsValidator,
72 asyncMiddleware(existingPluginValidator),
73 asyncMiddleware(updatePluginSettings)
76 pluginRouter.post('/install',
78 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
79 installOrUpdatePluginValidator,
80 asyncMiddleware(installPlugin)
83 pluginRouter.post('/update',
85 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
86 installOrUpdatePluginValidator,
87 asyncMiddleware(updatePlugin)
90 pluginRouter.post('/uninstall',
92 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
93 uninstallPluginValidator,
94 asyncMiddleware(uninstallPlugin)
97 // ---------------------------------------------------------------------------
103 // ---------------------------------------------------------------------------
105 async function listPlugins (req: express.Request, res: express.Response) {
106 const pluginType = req.query.pluginType
107 const uninstalled = req.query.uninstalled
109 const resultList = await PluginModel.listForApi({
112 start: req.query.start,
113 count: req.query.count,
117 return res.json(getFormattedObjects(resultList.data, resultList.total))
120 function getPlugin (req: express.Request, res: express.Response) {
121 const plugin = res.locals.plugin
123 return res.json(plugin.toFormattedJSON())
126 async function installPlugin (req: express.Request, res: express.Response) {
127 const body: InstallOrUpdatePlugin = req.body
129 const fromDisk = !!body.path
130 const toInstall = body.npmName || body.path
132 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
134 return res.json(plugin.toFormattedJSON())
136 logger.warn('Cannot install plugin %s.', toInstall, { err })
137 return res.sendStatus(400)
141 async function updatePlugin (req: express.Request, res: express.Response) {
142 const body: InstallOrUpdatePlugin = req.body
144 const fromDisk = !!body.path
145 const toUpdate = body.npmName || body.path
147 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
149 return res.json(plugin.toFormattedJSON())
151 logger.warn('Cannot update plugin %s.', toUpdate, { err })
152 return res.sendStatus(400)
156 async function uninstallPlugin (req: express.Request, res: express.Response) {
157 const body: ManagePlugin = req.body
159 await PluginManager.Instance.uninstall(body.npmName)
161 return res.sendStatus(204)
164 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
165 const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
167 const json: RegisteredSettings = { settings }
169 return res.json(json)
172 async function updatePluginSettings (req: express.Request, res: express.Response) {
173 const plugin = res.locals.plugin
175 plugin.settings = req.body.settings
178 return res.sendStatus(204)
181 async function listAvailablePlugins (req: express.Request, res: express.Response) {
182 const query: PeertubePluginIndexList = req.query
184 const resultList = await listAvailablePluginsFromIndex(query)
187 return res.status(503)
188 .json({ error: 'Plugin index unavailable. Please retry later' })
192 return res.json(resultList)