]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/plugins.ts
Merge branch 'release/2.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / plugins.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { getFormattedObjects } from '../../helpers/utils'
3import {
4 asyncMiddleware,
5 authenticate,
6 ensureUserHasRight,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort
10} from '../../middlewares'
11import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators'
12import { PluginModel } from '../../models/server/plugin'
13import { UserRight } from '../../../shared/models/users'
14import {
15 existingPluginValidator,
16 installOrUpdatePluginValidator,
17 listAvailablePluginsValidator,
18 listPluginsValidator,
19 uninstallPluginValidator,
20 updatePluginSettingsValidator
21} from '../../middlewares/validators/plugins'
22import { PluginManager } from '../../lib/plugins/plugin-manager'
23import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
24import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
25import { logger } from '../../helpers/logger'
26import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index'
27import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
28import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
29import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
30
31const pluginRouter = express.Router()
32
33pluginRouter.get('/available',
34 authenticate,
35 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
36 listAvailablePluginsValidator,
37 paginationValidator,
38 availablePluginsSortValidator,
39 setDefaultSort,
40 setDefaultPagination,
41 asyncMiddleware(listAvailablePlugins)
42)
43
44pluginRouter.get('/',
45 authenticate,
46 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
47 listPluginsValidator,
48 paginationValidator,
49 pluginsSortValidator,
50 setDefaultSort,
51 setDefaultPagination,
52 asyncMiddleware(listPlugins)
53)
54
55pluginRouter.get('/:npmName/registered-settings',
56 authenticate,
57 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
58 asyncMiddleware(existingPluginValidator),
59 getPluginRegisteredSettings
60)
61
62pluginRouter.get('/:npmName/public-settings',
63 asyncMiddleware(existingPluginValidator),
64 getPublicPluginSettings
65)
66
67pluginRouter.put('/:npmName/settings',
68 authenticate,
69 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
70 updatePluginSettingsValidator,
71 asyncMiddleware(existingPluginValidator),
72 asyncMiddleware(updatePluginSettings)
73)
74
75pluginRouter.get('/:npmName',
76 authenticate,
77 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
78 asyncMiddleware(existingPluginValidator),
79 getPlugin
80)
81
82pluginRouter.post('/install',
83 authenticate,
84 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
85 installOrUpdatePluginValidator,
86 asyncMiddleware(installPlugin)
87)
88
89pluginRouter.post('/update',
90 authenticate,
91 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
92 installOrUpdatePluginValidator,
93 asyncMiddleware(updatePlugin)
94)
95
96pluginRouter.post('/uninstall',
97 authenticate,
98 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
99 uninstallPluginValidator,
100 asyncMiddleware(uninstallPlugin)
101)
102
103// ---------------------------------------------------------------------------
104
105export {
106 pluginRouter
107}
108
109// ---------------------------------------------------------------------------
110
111async function listPlugins (req: express.Request, res: express.Response) {
112 const pluginType = req.query.pluginType
113 const uninstalled = req.query.uninstalled
114
115 const resultList = await PluginModel.listForApi({
116 pluginType,
117 uninstalled,
118 start: req.query.start,
119 count: req.query.count,
120 sort: req.query.sort
121 })
122
123 return res.json(getFormattedObjects(resultList.data, resultList.total))
124}
125
126function getPlugin (req: express.Request, res: express.Response) {
127 const plugin = res.locals.plugin
128
129 return res.json(plugin.toFormattedJSON())
130}
131
132async function installPlugin (req: express.Request, res: express.Response) {
133 const body: InstallOrUpdatePlugin = req.body
134
135 const fromDisk = !!body.path
136 const toInstall = body.npmName || body.path
137 try {
138 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
139
140 return res.json(plugin.toFormattedJSON())
141 } catch (err) {
142 logger.warn('Cannot install plugin %s.', toInstall, { err })
143 return res.sendStatus(400)
144 }
145}
146
147async function updatePlugin (req: express.Request, res: express.Response) {
148 const body: InstallOrUpdatePlugin = req.body
149
150 const fromDisk = !!body.path
151 const toUpdate = body.npmName || body.path
152 try {
153 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
154
155 return res.json(plugin.toFormattedJSON())
156 } catch (err) {
157 logger.warn('Cannot update plugin %s.', toUpdate, { err })
158 return res.sendStatus(400)
159 }
160}
161
162async function uninstallPlugin (req: express.Request, res: express.Response) {
163 const body: ManagePlugin = req.body
164
165 await PluginManager.Instance.uninstall(body.npmName)
166
167 return res.sendStatus(204)
168}
169
170function getPublicPluginSettings (req: express.Request, res: express.Response) {
171 const plugin = res.locals.plugin
172 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
173 const publicSettings = plugin.getPublicSettings(registeredSettings)
174
175 const json: PublicServerSetting = { publicSettings }
176
177 return res.json(json)
178}
179
180function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
181 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
182
183 const json: RegisteredServerSettings = { registeredSettings }
184
185 return res.json(json)
186}
187
188async function updatePluginSettings (req: express.Request, res: express.Response) {
189 const plugin = res.locals.plugin
190
191 plugin.settings = req.body.settings
192 await plugin.save()
193
194 await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
195
196 return res.sendStatus(204)
197}
198
199async function listAvailablePlugins (req: express.Request, res: express.Response) {
200 const query: PeertubePluginIndexList = req.query
201
202 const resultList = await listAvailablePluginsFromIndex(query)
203
204 if (!resultList) {
205 return res.status(503)
206 .json({ error: 'Plugin index unavailable. Please retry later' })
207 .end()
208 }
209
210 return res.json(resultList)
211}