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'
30 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
32 const pluginRouter = express.Router()
34 pluginRouter.get('/available',
36 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
37 listAvailablePluginsValidator,
39 availablePluginsSortValidator,
42 asyncMiddleware(listAvailablePlugins)
47 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
53 asyncMiddleware(listPlugins)
56 pluginRouter.get('/:npmName/registered-settings',
58 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
59 asyncMiddleware(existingPluginValidator),
60 getPluginRegisteredSettings
63 pluginRouter.get('/:npmName/public-settings',
64 asyncMiddleware(existingPluginValidator),
65 getPublicPluginSettings
68 pluginRouter.put('/:npmName/settings',
70 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
71 updatePluginSettingsValidator,
72 asyncMiddleware(existingPluginValidator),
73 asyncMiddleware(updatePluginSettings)
76 pluginRouter.get('/:npmName',
78 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
79 asyncMiddleware(existingPluginValidator),
83 pluginRouter.post('/install',
85 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
86 installOrUpdatePluginValidator,
87 asyncMiddleware(installPlugin)
90 pluginRouter.post('/update',
92 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
93 installOrUpdatePluginValidator,
94 asyncMiddleware(updatePlugin)
97 pluginRouter.post('/uninstall',
99 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
100 uninstallPluginValidator,
101 asyncMiddleware(uninstallPlugin)
104 // ---------------------------------------------------------------------------
110 // ---------------------------------------------------------------------------
112 async function listPlugins (req: express.Request, res: express.Response) {
113 const pluginType = req.query.pluginType
114 const uninstalled = req.query.uninstalled
116 const resultList = await PluginModel.listForApi({
119 start: req.query.start,
120 count: req.query.count,
124 return res.json(getFormattedObjects(resultList.data, resultList.total))
127 function getPlugin (req: express.Request, res: express.Response) {
128 const plugin = res.locals.plugin
130 return res.json(plugin.toFormattedJSON())
133 async function installPlugin (req: express.Request, res: express.Response) {
134 const body: InstallOrUpdatePlugin = req.body
136 const fromDisk = !!body.path
137 const toInstall = body.npmName || body.path
139 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
141 return res.json(plugin.toFormattedJSON())
143 logger.warn('Cannot install plugin %s.', toInstall, { err })
144 return res.sendStatus(HttpStatusCode.BAD_REQUEST_400)
148 async function updatePlugin (req: express.Request, res: express.Response) {
149 const body: InstallOrUpdatePlugin = req.body
151 const fromDisk = !!body.path
152 const toUpdate = body.npmName || body.path
154 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
156 return res.json(plugin.toFormattedJSON())
158 logger.warn('Cannot update plugin %s.', toUpdate, { err })
159 return res.sendStatus(HttpStatusCode.BAD_REQUEST_400)
163 async function uninstallPlugin (req: express.Request, res: express.Response) {
164 const body: ManagePlugin = req.body
166 await PluginManager.Instance.uninstall(body.npmName)
168 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
171 function getPublicPluginSettings (req: express.Request, res: express.Response) {
172 const plugin = res.locals.plugin
173 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
174 const publicSettings = plugin.getPublicSettings(registeredSettings)
176 const json: PublicServerSetting = { publicSettings }
178 return res.json(json)
181 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
182 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
184 const json: RegisteredServerSettings = { registeredSettings }
186 return res.json(json)
189 async function updatePluginSettings (req: express.Request, res: express.Response) {
190 const plugin = res.locals.plugin
192 plugin.settings = req.body.settings
195 await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
197 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
200 async function listAvailablePlugins (req: express.Request, res: express.Response) {
201 const query: PeertubePluginIndexList = req.query
203 const resultList = await listAvailablePluginsFromIndex(query)
206 return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503)
207 .json({ error: 'Plugin index unavailable. Please retry later' })
211 return res.json(resultList)