6b7562fd3162439b5a15d122d84fb43bcf85120b
[oweals/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 { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
29 import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
30
31 const pluginRouter = express.Router()
32
33 pluginRouter.get('/available',
34   authenticate,
35   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
36   listAvailablePluginsValidator,
37   paginationValidator,
38   availablePluginsSortValidator,
39   setDefaultSort,
40   setDefaultPagination,
41   asyncMiddleware(listAvailablePlugins)
42 )
43
44 pluginRouter.get('/',
45   authenticate,
46   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
47   listPluginsValidator,
48   paginationValidator,
49   pluginsSortValidator,
50   setDefaultSort,
51   setDefaultPagination,
52   asyncMiddleware(listPlugins)
53 )
54
55 pluginRouter.get('/:npmName/registered-settings',
56   authenticate,
57   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
58   asyncMiddleware(existingPluginValidator),
59   getPluginRegisteredSettings
60 )
61
62 pluginRouter.get('/:npmName/public-settings',
63   asyncMiddleware(existingPluginValidator),
64   getPublicPluginSettings
65 )
66
67 pluginRouter.put('/:npmName/settings',
68   authenticate,
69   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
70   updatePluginSettingsValidator,
71   asyncMiddleware(existingPluginValidator),
72   asyncMiddleware(updatePluginSettings)
73 )
74
75 pluginRouter.get('/:npmName',
76   authenticate,
77   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
78   asyncMiddleware(existingPluginValidator),
79   getPlugin
80 )
81
82 pluginRouter.post('/install',
83   authenticate,
84   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
85   installOrUpdatePluginValidator,
86   asyncMiddleware(installPlugin)
87 )
88
89 pluginRouter.post('/update',
90   authenticate,
91   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
92   installOrUpdatePluginValidator,
93   asyncMiddleware(updatePlugin)
94 )
95
96 pluginRouter.post('/uninstall',
97   authenticate,
98   ensureUserHasRight(UserRight.MANAGE_PLUGINS),
99   uninstallPluginValidator,
100   asyncMiddleware(uninstallPlugin)
101 )
102
103 // ---------------------------------------------------------------------------
104
105 export {
106   pluginRouter
107 }
108
109 // ---------------------------------------------------------------------------
110
111 async 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
126 function getPlugin (req: express.Request, res: express.Response) {
127   const plugin = res.locals.plugin
128
129   return res.json(plugin.toFormattedJSON())
130 }
131
132 async 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
147 async 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
162 async 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
170 function 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
180 function 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
188 async 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   return res.sendStatus(204)
195 }
196
197 async function listAvailablePlugins (req: express.Request, res: express.Response) {
198   const query: PeertubePluginIndexList = req.query
199
200   const resultList = await listAvailablePluginsFromIndex(query)
201
202   if (!resultList) {
203     return res.status(503)
204       .json({ error: 'Plugin index unavailable. Please retry later' })
205       .end()
206   }
207
208   return res.json(resultList)
209 }