1 import * as express from 'express'
2 import { logger } from '@server/helpers/logger'
3 import { getFormattedObjects } from '@server/helpers/utils'
4 import { listAvailablePluginsFromIndex } from '@server/lib/plugins/plugin-index'
5 import { PluginManager } from '@server/lib/plugins/plugin-manager'
9 availablePluginsSortValidator,
15 } from '@server/middlewares'
17 existingPluginValidator,
18 installOrUpdatePluginValidator,
19 listAvailablePluginsValidator,
21 uninstallPluginValidator,
22 updatePluginSettingsValidator
23 } from '@server/middlewares/validators/plugins'
24 import { PluginModel } from '@server/models/server/plugin'
25 import { HttpStatusCode } from '@shared/core-utils'
27 InstallOrUpdatePlugin,
29 PeertubePluginIndexList,
31 RegisteredServerSettings,
33 } from '@shared/models'
35 const pluginRouter = express.Router()
37 pluginRouter.get('/available',
39 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
40 listAvailablePluginsValidator,
42 availablePluginsSortValidator,
45 asyncMiddleware(listAvailablePlugins)
50 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
56 asyncMiddleware(listPlugins)
59 pluginRouter.get('/:npmName/registered-settings',
61 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
62 asyncMiddleware(existingPluginValidator),
63 getPluginRegisteredSettings
66 pluginRouter.get('/:npmName/public-settings',
67 asyncMiddleware(existingPluginValidator),
68 getPublicPluginSettings
71 pluginRouter.put('/:npmName/settings',
73 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
74 updatePluginSettingsValidator,
75 asyncMiddleware(existingPluginValidator),
76 asyncMiddleware(updatePluginSettings)
79 pluginRouter.get('/:npmName',
81 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
82 asyncMiddleware(existingPluginValidator),
86 pluginRouter.post('/install',
88 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
89 installOrUpdatePluginValidator,
90 asyncMiddleware(installPlugin)
93 pluginRouter.post('/update',
95 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
96 installOrUpdatePluginValidator,
97 asyncMiddleware(updatePlugin)
100 pluginRouter.post('/uninstall',
102 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
103 uninstallPluginValidator,
104 asyncMiddleware(uninstallPlugin)
107 // ---------------------------------------------------------------------------
113 // ---------------------------------------------------------------------------
115 async function listPlugins (req: express.Request, res: express.Response) {
116 const pluginType = req.query.pluginType
117 const uninstalled = req.query.uninstalled
119 const resultList = await PluginModel.listForApi({
122 start: req.query.start,
123 count: req.query.count,
127 return res.json(getFormattedObjects(resultList.data, resultList.total))
130 function getPlugin (req: express.Request, res: express.Response) {
131 const plugin = res.locals.plugin
133 return res.json(plugin.toFormattedJSON())
136 async function installPlugin (req: express.Request, res: express.Response) {
137 const body: InstallOrUpdatePlugin = req.body
139 const fromDisk = !!body.path
140 const toInstall = body.npmName || body.path
142 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
144 return res.json(plugin.toFormattedJSON())
146 logger.warn('Cannot install plugin %s.', toInstall, { err })
147 return res.sendStatus(HttpStatusCode.BAD_REQUEST_400)
151 async function updatePlugin (req: express.Request, res: express.Response) {
152 const body: InstallOrUpdatePlugin = req.body
154 const fromDisk = !!body.path
155 const toUpdate = body.npmName || body.path
157 const plugin = await PluginManager.Instance.update(toUpdate, fromDisk)
159 return res.json(plugin.toFormattedJSON())
161 logger.warn('Cannot update plugin %s.', toUpdate, { err })
162 return res.sendStatus(HttpStatusCode.BAD_REQUEST_400)
166 async function uninstallPlugin (req: express.Request, res: express.Response) {
167 const body: ManagePlugin = req.body
169 await PluginManager.Instance.uninstall(body.npmName)
171 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
174 function getPublicPluginSettings (req: express.Request, res: express.Response) {
175 const plugin = res.locals.plugin
176 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
177 const publicSettings = plugin.getPublicSettings(registeredSettings)
179 const json: PublicServerSetting = { publicSettings }
181 return res.json(json)
184 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
185 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
187 const json: RegisteredServerSettings = { registeredSettings }
189 return res.json(json)
192 async function updatePluginSettings (req: express.Request, res: express.Response) {
193 const plugin = res.locals.plugin
195 plugin.settings = req.body.settings
198 await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
200 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
203 async function listAvailablePlugins (req: express.Request, res: express.Response) {
204 const query: PeertubePluginIndexList = req.query
206 const resultList = await listAvailablePluginsFromIndex(query)
209 return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503)
210 .json({ error: 'Plugin index unavailable. Please retry later' })
213 return res.json(resultList)