]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/plugins.ts
WIP plugins: add plugin settings/uninstall in client
[github/Chocobozzz/PeerTube.git] / server / controllers / api / plugins.ts
1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
3 import {
4 asyncMiddleware,
5 authenticate,
6 ensureUserHasRight,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort
10 } from '../../middlewares'
11 import { pluginsSortValidator } from '../../middlewares/validators'
12 import { PluginModel } from '../../models/server/plugin'
13 import { UserRight } from '../../../shared/models/users'
14 import {
15 existingPluginValidator,
16 installPluginValidator,
17 listPluginsValidator,
18 uninstallPluginValidator,
19 updatePluginSettingsValidator
20 } from '../../middlewares/validators/plugins'
21 import { PluginManager } from '../../lib/plugins/plugin-manager'
22 import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
23 import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
24
25 const pluginRouter = express.Router()
26
27 pluginRouter.get('/',
28 authenticate,
29 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
30 listPluginsValidator,
31 paginationValidator,
32 pluginsSortValidator,
33 setDefaultSort,
34 setDefaultPagination,
35 asyncMiddleware(listPlugins)
36 )
37
38 pluginRouter.get('/:npmName',
39 authenticate,
40 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
41 asyncMiddleware(existingPluginValidator),
42 getPlugin
43 )
44
45 pluginRouter.get('/:npmName/registered-settings',
46 authenticate,
47 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
48 asyncMiddleware(existingPluginValidator),
49 asyncMiddleware(getPluginRegisteredSettings)
50 )
51
52 pluginRouter.put('/:npmName/settings',
53 authenticate,
54 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
55 updatePluginSettingsValidator,
56 asyncMiddleware(existingPluginValidator),
57 asyncMiddleware(updatePluginSettings)
58 )
59
60 pluginRouter.post('/install',
61 authenticate,
62 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
63 installPluginValidator,
64 asyncMiddleware(installPlugin)
65 )
66
67 pluginRouter.post('/uninstall',
68 authenticate,
69 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
70 uninstallPluginValidator,
71 asyncMiddleware(uninstallPlugin)
72 )
73
74 // ---------------------------------------------------------------------------
75
76 export {
77 pluginRouter
78 }
79
80 // ---------------------------------------------------------------------------
81
82 async function listPlugins (req: express.Request, res: express.Response) {
83 const type = req.query.type
84
85 const resultList = await PluginModel.listForApi({
86 type,
87 start: req.query.start,
88 count: req.query.count,
89 sort: req.query.sort
90 })
91
92 return res.json(getFormattedObjects(resultList.data, resultList.total))
93 }
94
95 function getPlugin (req: express.Request, res: express.Response) {
96 const plugin = res.locals.plugin
97
98 return res.json(plugin.toFormattedJSON())
99 }
100
101 async function installPlugin (req: express.Request, res: express.Response) {
102 const body: InstallPlugin = req.body
103
104 await PluginManager.Instance.install(body.npmName)
105
106 return res.sendStatus(204)
107 }
108
109 async function uninstallPlugin (req: express.Request, res: express.Response) {
110 const body: ManagePlugin = req.body
111
112 await PluginManager.Instance.uninstall(body.npmName)
113
114 return res.sendStatus(204)
115 }
116
117 async function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
118 const plugin = res.locals.plugin
119
120 const settings = await PluginManager.Instance.getSettings(plugin.name)
121
122 return res.json({
123 settings
124 })
125 }
126
127 async function updatePluginSettings (req: express.Request, res: express.Response) {
128 const plugin = res.locals.plugin
129
130 plugin.settings = req.body.settings
131 await plugin.save()
132
133 return res.sendStatus(204)
134 }