]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/plugins.ts
8e59f27cf33b98f3761f021eb0be56ac98b5eef6
[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 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 installPluginValidator,
65 asyncMiddleware(installPlugin)
66 )
67
68 pluginRouter.post('/uninstall',
69 authenticate,
70 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
71 uninstallPluginValidator,
72 asyncMiddleware(uninstallPlugin)
73 )
74
75 // ---------------------------------------------------------------------------
76
77 export {
78 pluginRouter
79 }
80
81 // ---------------------------------------------------------------------------
82
83 async function listPlugins (req: express.Request, res: express.Response) {
84 const type = req.query.type
85
86 const resultList = await PluginModel.listForApi({
87 type,
88 start: req.query.start,
89 count: req.query.count,
90 sort: req.query.sort
91 })
92
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
94 }
95
96 function getPlugin (req: express.Request, res: express.Response) {
97 const plugin = res.locals.plugin
98
99 return res.json(plugin.toFormattedJSON())
100 }
101
102 async function installPlugin (req: express.Request, res: express.Response) {
103 const body: InstallPlugin = req.body
104
105 const fromDisk = !!body.path
106 const toInstall = body.npmName || body.path
107 try {
108 await PluginManager.Instance.install(toInstall, undefined, fromDisk)
109 } catch (err) {
110 logger.warn('Cannot install plugin %s.', toInstall, { err })
111 return res.sendStatus(400)
112 }
113
114 return res.sendStatus(204)
115 }
116
117 async function uninstallPlugin (req: express.Request, res: express.Response) {
118 const body: ManagePlugin = req.body
119
120 await PluginManager.Instance.uninstall(body.npmName)
121
122 return res.sendStatus(204)
123 }
124
125 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
126 const plugin = res.locals.plugin
127
128 const settings = PluginManager.Instance.getSettings(plugin.name)
129
130 return res.json({
131 settings
132 })
133 }
134
135 async function updatePluginSettings (req: express.Request, res: express.Response) {
136 const plugin = res.locals.plugin
137
138 plugin.settings = req.body.settings
139 await plugin.save()
140
141 return res.sendStatus(204)
142 }