aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-10 16:59:53 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commitad91e7006e41f8ee5b8dcefee30f99e8ca44133a (patch)
treed860f20e05b036fa1a96e049c74deffd7f5d2b00 /server/controllers
parentffb321bedca46d6987c7b31dd58e5dea96ea2ea2 (diff)
downloadPeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.tar.gz
PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.tar.zst
PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.zip
WIP plugins: plugin settings on server side
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/index.ts2
-rw-r--r--server/controllers/api/plugins.ts121
2 files changed, 123 insertions, 0 deletions
diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts
index ea2615e28..0876283a2 100644
--- a/server/controllers/api/index.ts
+++ b/server/controllers/api/index.ts
@@ -14,6 +14,7 @@ import { searchRouter } from './search'
14import { overviewsRouter } from './overviews' 14import { overviewsRouter } from './overviews'
15import { videoPlaylistRouter } from './video-playlist' 15import { videoPlaylistRouter } from './video-playlist'
16import { CONFIG } from '../../initializers/config' 16import { CONFIG } from '../../initializers/config'
17import { pluginsRouter } from '../plugins'
17 18
18const apiRouter = express.Router() 19const apiRouter = express.Router()
19 20
@@ -42,6 +43,7 @@ apiRouter.use('/videos', videosRouter)
42apiRouter.use('/jobs', jobsRouter) 43apiRouter.use('/jobs', jobsRouter)
43apiRouter.use('/search', searchRouter) 44apiRouter.use('/search', searchRouter)
44apiRouter.use('/overviews', overviewsRouter) 45apiRouter.use('/overviews', overviewsRouter)
46apiRouter.use('/plugins', pluginsRouter)
45apiRouter.use('/ping', pong) 47apiRouter.use('/ping', pong)
46apiRouter.use('/*', badRequest) 48apiRouter.use('/*', badRequest)
47 49
diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts
new file mode 100644
index 000000000..89cc67f54
--- /dev/null
+++ b/server/controllers/api/plugins.ts
@@ -0,0 +1,121 @@
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 { pluginsSortValidator } from '../../middlewares/validators'
12import { PluginModel } from '../../models/server/plugin'
13import { UserRight } from '../../../shared/models/users'
14import {
15 enabledPluginValidator,
16 installPluginValidator,
17 listPluginsValidator,
18 uninstallPluginValidator,
19 updatePluginSettingsValidator
20} from '../../middlewares/validators/plugins'
21import { PluginManager } from '../../lib/plugins/plugin-manager'
22import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
23import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
24
25const pluginRouter = express.Router()
26
27pluginRouter.get('/',
28 authenticate,
29 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
30 listPluginsValidator,
31 paginationValidator,
32 pluginsSortValidator,
33 setDefaultSort,
34 setDefaultPagination,
35 asyncMiddleware(listPlugins)
36)
37
38pluginRouter.get('/:pluginName/settings',
39 authenticate,
40 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
41 asyncMiddleware(enabledPluginValidator),
42 asyncMiddleware(listPluginSettings)
43)
44
45pluginRouter.put('/:pluginName/settings',
46 authenticate,
47 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
48 updatePluginSettingsValidator,
49 asyncMiddleware(enabledPluginValidator),
50 asyncMiddleware(updatePluginSettings)
51)
52
53pluginRouter.post('/install',
54 authenticate,
55 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
56 installPluginValidator,
57 asyncMiddleware(installPlugin)
58)
59
60pluginRouter.post('/uninstall',
61 authenticate,
62 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
63 uninstallPluginValidator,
64 asyncMiddleware(uninstallPlugin)
65)
66
67// ---------------------------------------------------------------------------
68
69export {
70 pluginRouter
71}
72
73// ---------------------------------------------------------------------------
74
75async function listPlugins (req: express.Request, res: express.Response) {
76 const type = req.query.type
77
78 const resultList = await PluginModel.listForApi({
79 type,
80 start: req.query.start,
81 count: req.query.count,
82 sort: req.query.sort
83 })
84
85 return res.json(getFormattedObjects(resultList.data, resultList.total))
86}
87
88async function installPlugin (req: express.Request, res: express.Response) {
89 const body: InstallPlugin = req.body
90
91 await PluginManager.Instance.install(body.npmName)
92
93 return res.sendStatus(204)
94}
95
96async function uninstallPlugin (req: express.Request, res: express.Response) {
97 const body: ManagePlugin = req.body
98
99 await PluginManager.Instance.uninstall(body.npmName)
100
101 return res.sendStatus(204)
102}
103
104async function listPluginSettings (req: express.Request, res: express.Response) {
105 const plugin = res.locals.plugin
106
107 const settings = await PluginManager.Instance.getSettings(plugin.name)
108
109 return res.json({
110 settings
111 })
112}
113
114async function updatePluginSettings (req: express.Request, res: express.Response) {
115 const plugin = res.locals.plugin
116
117 plugin.settings = req.body.settings
118 await plugin.save()
119
120 return res.sendStatus(204)
121}