]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/plugins.ts
Merge branch 'release/2.2.0' into develop
[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'
9ae88819 28import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
ba211e73 29import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
ad91e700
C
30
31const pluginRouter = express.Router()
32
6702a1b2
C
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
ad91e700
C
44pluginRouter.get('/',
45 authenticate,
46 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
47 listPluginsValidator,
48 paginationValidator,
49 pluginsSortValidator,
50 setDefaultSort,
51 setDefaultPagination,
52 asyncMiddleware(listPlugins)
53)
54
ba211e73 55pluginRouter.get('/:npmName/registered-settings',
ad91e700
C
56 authenticate,
57 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
dba85a1e 58 asyncMiddleware(existingPluginValidator),
ba211e73 59 getPluginRegisteredSettings
ad91e700
C
60)
61
ba211e73 62pluginRouter.get('/:npmName/public-settings',
dba85a1e 63 asyncMiddleware(existingPluginValidator),
ba211e73 64 getPublicPluginSettings
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
ba211e73
C
75pluginRouter.get('/:npmName',
76 authenticate,
77 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
78 asyncMiddleware(existingPluginValidator),
79 getPlugin
80)
81
ad91e700
C
82pluginRouter.post('/install',
83 authenticate,
84 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
b5f919ac 85 installOrUpdatePluginValidator,
ad91e700
C
86 asyncMiddleware(installPlugin)
87)
88
b5f919ac
C
89pluginRouter.post('/update',
90 authenticate,
91 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
92 installOrUpdatePluginValidator,
93 asyncMiddleware(updatePlugin)
94)
95
ad91e700
C
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) {
6702a1b2 112 const pluginType = req.query.pluginType
09071200 113 const uninstalled = req.query.uninstalled
ad91e700
C
114
115 const resultList = await PluginModel.listForApi({
6702a1b2 116 pluginType,
09071200 117 uninstalled,
ad91e700
C
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
dba85a1e
C
126function getPlugin (req: express.Request, res: express.Response) {
127 const plugin = res.locals.plugin
128
129 return res.json(plugin.toFormattedJSON())
130}
131
ad91e700 132async function installPlugin (req: express.Request, res: express.Response) {
b5f919ac 133 const body: InstallOrUpdatePlugin = req.body
ad91e700 134
8d2be0ed
C
135 const fromDisk = !!body.path
136 const toInstall = body.npmName || body.path
137 try {
b5f919ac
C
138 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
139
140 return res.json(plugin.toFormattedJSON())
8d2be0ed
C
141 } catch (err) {
142 logger.warn('Cannot install plugin %s.', toInstall, { err })
143 return res.sendStatus(400)
144 }
b5f919ac 145}
ad91e700 146
b5f919ac
C
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 }
ad91e700
C
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
ba211e73
C
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
8d2be0ed 180function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
ba211e73 181 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
ad91e700 182
ba211e73 183 const json: RegisteredServerSettings = { registeredSettings }
09071200
C
184
185 return res.json(json)
ad91e700
C
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
a5896799
C
194 await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
195
ad91e700
C
196 return res.sendStatus(204)
197}
6702a1b2
C
198
199async function listAvailablePlugins (req: express.Request, res: express.Response) {
200 const query: PeertubePluginIndexList = req.query
201
202 const resultList = await listAvailablePluginsFromIndex(query)
203
e0ce715a
C
204 if (!resultList) {
205 return res.status(503)
206 .json({ error: 'Plugin index unavailable. Please retry later' })
207 .end()
208 }
209
6702a1b2
C
210 return res.json(resultList)
211}