]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/plugins.ts
Add plugin API tests
[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 { availablePluginsSortValidator, 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 listAvailablePluginsValidator,
18 listPluginsValidator,
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 { RegisteredSettings } from '../../../shared/models/plugins/register-setting.model'
29
30 const pluginRouter = express.Router()
31
32 pluginRouter.get('/available',
33 authenticate,
34 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
35 listAvailablePluginsValidator,
36 paginationValidator,
37 availablePluginsSortValidator,
38 setDefaultSort,
39 setDefaultPagination,
40 asyncMiddleware(listAvailablePlugins)
41 )
42
43 pluginRouter.get('/',
44 authenticate,
45 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
46 listPluginsValidator,
47 paginationValidator,
48 pluginsSortValidator,
49 setDefaultSort,
50 setDefaultPagination,
51 asyncMiddleware(listPlugins)
52 )
53
54 pluginRouter.get('/:npmName',
55 authenticate,
56 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
57 asyncMiddleware(existingPluginValidator),
58 getPlugin
59 )
60
61 pluginRouter.get('/:npmName/registered-settings',
62 authenticate,
63 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
64 asyncMiddleware(existingPluginValidator),
65 getPluginRegisteredSettings
66 )
67
68 pluginRouter.put('/:npmName/settings',
69 authenticate,
70 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
71 updatePluginSettingsValidator,
72 asyncMiddleware(existingPluginValidator),
73 asyncMiddleware(updatePluginSettings)
74 )
75
76 pluginRouter.post('/install',
77 authenticate,
78 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
79 installOrUpdatePluginValidator,
80 asyncMiddleware(installPlugin)
81 )
82
83 pluginRouter.post('/update',
84 authenticate,
85 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
86 installOrUpdatePluginValidator,
87 asyncMiddleware(updatePlugin)
88 )
89
90 pluginRouter.post('/uninstall',
91 authenticate,
92 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
93 uninstallPluginValidator,
94 asyncMiddleware(uninstallPlugin)
95 )
96
97 // ---------------------------------------------------------------------------
98
99 export {
100 pluginRouter
101 }
102
103 // ---------------------------------------------------------------------------
104
105 async function listPlugins (req: express.Request, res: express.Response) {
106 const pluginType = req.query.pluginType
107 const uninstalled = req.query.uninstalled
108
109 const resultList = await PluginModel.listForApi({
110 pluginType,
111 uninstalled,
112 start: req.query.start,
113 count: req.query.count,
114 sort: req.query.sort
115 })
116
117 return res.json(getFormattedObjects(resultList.data, resultList.total))
118 }
119
120 function getPlugin (req: express.Request, res: express.Response) {
121 const plugin = res.locals.plugin
122
123 return res.json(plugin.toFormattedJSON())
124 }
125
126 async function installPlugin (req: express.Request, res: express.Response) {
127 const body: InstallOrUpdatePlugin = req.body
128
129 const fromDisk = !!body.path
130 const toInstall = body.npmName || body.path
131 try {
132 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
133
134 return res.json(plugin.toFormattedJSON())
135 } catch (err) {
136 logger.warn('Cannot install plugin %s.', toInstall, { err })
137 return res.sendStatus(400)
138 }
139 }
140
141 async function updatePlugin (req: express.Request, res: express.Response) {
142 const body: InstallOrUpdatePlugin = req.body
143
144 const fromDisk = !!body.path
145 const toUpdate = body.npmName || body.path
146 try {
147 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
148
149 return res.json(plugin.toFormattedJSON())
150 } catch (err) {
151 logger.warn('Cannot update plugin %s.', toUpdate, { err })
152 return res.sendStatus(400)
153 }
154 }
155
156 async function uninstallPlugin (req: express.Request, res: express.Response) {
157 const body: ManagePlugin = req.body
158
159 await PluginManager.Instance.uninstall(body.npmName)
160
161 return res.sendStatus(204)
162 }
163
164 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
165 const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
166
167 const json: RegisteredSettings = { settings }
168
169 return res.json(json)
170 }
171
172 async function updatePluginSettings (req: express.Request, res: express.Response) {
173 const plugin = res.locals.plugin
174
175 plugin.settings = req.body.settings
176 await plugin.save()
177
178 return res.sendStatus(204)
179 }
180
181 async function listAvailablePlugins (req: express.Request, res: express.Response) {
182 const query: PeertubePluginIndexList = req.query
183
184 const resultList = await listAvailablePluginsFromIndex(query)
185
186 if (!resultList) {
187 return res.status(503)
188 .json({ error: 'Plugin index unavailable. Please retry later' })
189 .end()
190 }
191
192 return res.json(resultList)
193 }