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