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 { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
29 import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
31 const pluginRouter = express.Router()
33 pluginRouter.get('/available',
35 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
36 listAvailablePluginsValidator,
38 availablePluginsSortValidator,
41 asyncMiddleware(listAvailablePlugins)
46 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
52 asyncMiddleware(listPlugins)
55 pluginRouter.get('/:npmName/registered-settings',
57 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
58 asyncMiddleware(existingPluginValidator),
59 getPluginRegisteredSettings
62 pluginRouter.get('/:npmName/public-settings',
63 asyncMiddleware(existingPluginValidator),
64 getPublicPluginSettings
67 pluginRouter.put('/:npmName/settings',
69 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
70 updatePluginSettingsValidator,
71 asyncMiddleware(existingPluginValidator),
72 asyncMiddleware(updatePluginSettings)
75 pluginRouter.get('/:npmName',
77 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
78 asyncMiddleware(existingPluginValidator),
82 pluginRouter.post('/install',
84 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
85 installOrUpdatePluginValidator,
86 asyncMiddleware(installPlugin)
89 pluginRouter.post('/update',
91 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
92 installOrUpdatePluginValidator,
93 asyncMiddleware(updatePlugin)
96 pluginRouter.post('/uninstall',
98 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
99 uninstallPluginValidator,
100 asyncMiddleware(uninstallPlugin)
103 // ---------------------------------------------------------------------------
109 // ---------------------------------------------------------------------------
111 async function listPlugins (req: express.Request, res: express.Response) {
112 const pluginType = req.query.pluginType
113 const uninstalled = req.query.uninstalled
115 const resultList = await PluginModel.listForApi({
118 start: req.query.start,
119 count: req.query.count,
123 return res.json(getFormattedObjects(resultList.data, resultList.total))
126 function getPlugin (req: express.Request, res: express.Response) {
127 const plugin = res.locals.plugin
129 return res.json(plugin.toFormattedJSON())
132 async function installPlugin (req: express.Request, res: express.Response) {
133 const body: InstallOrUpdatePlugin = req.body
135 const fromDisk = !!body.path
136 const toInstall = body.npmName || body.path
138 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
140 return res.json(plugin.toFormattedJSON())
142 logger.warn('Cannot install plugin %s.', toInstall, { err })
143 return res.sendStatus(400)
147 async function updatePlugin (req: express.Request, res: express.Response) {
148 const body: InstallOrUpdatePlugin = req.body
150 const fromDisk = !!body.path
151 const toUpdate = body.npmName || body.path
153 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
155 return res.json(plugin.toFormattedJSON())
157 logger.warn('Cannot update plugin %s.', toUpdate, { err })
158 return res.sendStatus(400)
162 async function uninstallPlugin (req: express.Request, res: express.Response) {
163 const body: ManagePlugin = req.body
165 await PluginManager.Instance.uninstall(body.npmName)
167 return res.sendStatus(204)
170 function getPublicPluginSettings (req: express.Request, res: express.Response) {
171 const plugin = res.locals.plugin
172 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
173 const publicSettings = plugin.getPublicSettings(registeredSettings)
175 const json: PublicServerSetting = { publicSettings }
177 return res.json(json)
180 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
181 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
183 const json: RegisteredServerSettings = { registeredSettings }
185 return res.json(json)
188 async function updatePluginSettings (req: express.Request, res: express.Response) {
189 const plugin = res.locals.plugin
191 plugin.settings = req.body.settings
194 await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
196 return res.sendStatus(204)
199 async function listAvailablePlugins (req: express.Request, res: express.Response) {
200 const query: PeertubePluginIndexList = req.query
202 const resultList = await listAvailablePluginsFromIndex(query)
205 return res.status(503)
206 .json({ error: 'Plugin index unavailable. Please retry later' })
210 return res.json(resultList)