]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/plugins.ts
WIP plugins: update plugin
[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 installOrUpdatePluginValidator,
17 listPluginsValidator,
18 uninstallPluginValidator,
19 updatePluginSettingsValidator
20 } from '../../middlewares/validators/plugins'
21 import { PluginManager } from '../../lib/plugins/plugin-manager'
22 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
23 import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
24 import { logger } from '../../helpers/logger'
25
26 const pluginRouter = express.Router()
27
28 pluginRouter.get('/',
29 authenticate,
30 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
31 listPluginsValidator,
32 paginationValidator,
33 pluginsSortValidator,
34 setDefaultSort,
35 setDefaultPagination,
36 asyncMiddleware(listPlugins)
37 )
38
39 pluginRouter.get('/:npmName',
40 authenticate,
41 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
42 asyncMiddleware(existingPluginValidator),
43 getPlugin
44 )
45
46 pluginRouter.get('/:npmName/registered-settings',
47 authenticate,
48 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
49 asyncMiddleware(existingPluginValidator),
50 getPluginRegisteredSettings
51 )
52
53 pluginRouter.put('/:npmName/settings',
54 authenticate,
55 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
56 updatePluginSettingsValidator,
57 asyncMiddleware(existingPluginValidator),
58 asyncMiddleware(updatePluginSettings)
59 )
60
61 pluginRouter.post('/install',
62 authenticate,
63 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
64 installOrUpdatePluginValidator,
65 asyncMiddleware(installPlugin)
66 )
67
68 pluginRouter.post('/update',
69 authenticate,
70 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
71 installOrUpdatePluginValidator,
72 asyncMiddleware(updatePlugin)
73 )
74
75 pluginRouter.post('/uninstall',
76 authenticate,
77 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
78 uninstallPluginValidator,
79 asyncMiddleware(uninstallPlugin)
80 )
81
82 // ---------------------------------------------------------------------------
83
84 export {
85 pluginRouter
86 }
87
88 // ---------------------------------------------------------------------------
89
90 async function listPlugins (req: express.Request, res: express.Response) {
91 const type = req.query.type
92
93 const resultList = await PluginModel.listForApi({
94 type,
95 start: req.query.start,
96 count: req.query.count,
97 sort: req.query.sort
98 })
99
100 return res.json(getFormattedObjects(resultList.data, resultList.total))
101 }
102
103 function getPlugin (req: express.Request, res: express.Response) {
104 const plugin = res.locals.plugin
105
106 return res.json(plugin.toFormattedJSON())
107 }
108
109 async function installPlugin (req: express.Request, res: express.Response) {
110 const body: InstallOrUpdatePlugin = req.body
111
112 const fromDisk = !!body.path
113 const toInstall = body.npmName || body.path
114 try {
115 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
116
117 return res.json(plugin.toFormattedJSON())
118 } catch (err) {
119 logger.warn('Cannot install plugin %s.', toInstall, { err })
120 return res.sendStatus(400)
121 }
122 }
123
124 async function updatePlugin (req: express.Request, res: express.Response) {
125 const body: InstallOrUpdatePlugin = req.body
126
127 const fromDisk = !!body.path
128 const toUpdate = body.npmName || body.path
129 try {
130 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
131
132 return res.json(plugin.toFormattedJSON())
133 } catch (err) {
134 logger.warn('Cannot update plugin %s.', toUpdate, { err })
135 return res.sendStatus(400)
136 }
137 }
138
139 async function uninstallPlugin (req: express.Request, res: express.Response) {
140 const body: ManagePlugin = req.body
141
142 await PluginManager.Instance.uninstall(body.npmName)
143
144 return res.sendStatus(204)
145 }
146
147 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
148 const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
149
150 return res.json({
151 settings
152 })
153 }
154
155 async function updatePluginSettings (req: express.Request, res: express.Response) {
156 const plugin = res.locals.plugin
157
158 plugin.settings = req.body.settings
159 await plugin.save()
160
161 return res.sendStatus(204)
162 }