]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/plugins.ts
Try to fix weird CI test crashes
[github/Chocobozzz/PeerTube.git] / server / controllers / api / plugins.ts
CommitLineData
ad91e700 1import * as express from 'express'
428ccb8b
C
2import { logger } from '@server/helpers/logger'
3import { getFormattedObjects } from '@server/helpers/utils'
4import { listAvailablePluginsFromIndex } from '@server/lib/plugins/plugin-index'
5import { PluginManager } from '@server/lib/plugins/plugin-manager'
ad91e700
C
6import {
7 asyncMiddleware,
8 authenticate,
428ccb8b 9 availablePluginsSortValidator,
ad91e700 10 ensureUserHasRight,
1333ab1f 11 openapiOperationDoc,
ad91e700 12 paginationValidator,
428ccb8b 13 pluginsSortValidator,
ad91e700
C
14 setDefaultPagination,
15 setDefaultSort
428ccb8b 16} from '@server/middlewares'
ad91e700 17import {
dba85a1e 18 existingPluginValidator,
b5f919ac 19 installOrUpdatePluginValidator,
6702a1b2 20 listAvailablePluginsValidator,
ad91e700
C
21 listPluginsValidator,
22 uninstallPluginValidator,
23 updatePluginSettingsValidator
428ccb8b
C
24} from '@server/middlewares/validators/plugins'
25import { PluginModel } from '@server/models/server/plugin'
428ccb8b 26import {
4c7e60bc 27 HttpStatusCode,
428ccb8b
C
28 InstallOrUpdatePlugin,
29 ManagePlugin,
30 PeertubePluginIndexList,
31 PublicServerSetting,
32 RegisteredServerSettings,
33 UserRight
34} from '@shared/models'
ad91e700
C
35
36const pluginRouter = express.Router()
37
6702a1b2 38pluginRouter.get('/available',
1333ab1f 39 openapiOperationDoc({ operationId: 'getAvailablePlugins' }),
6702a1b2
C
40 authenticate,
41 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
42 listAvailablePluginsValidator,
43 paginationValidator,
44 availablePluginsSortValidator,
45 setDefaultSort,
46 setDefaultPagination,
47 asyncMiddleware(listAvailablePlugins)
48)
49
ad91e700 50pluginRouter.get('/',
1333ab1f 51 openapiOperationDoc({ operationId: 'getPlugins' }),
ad91e700
C
52 authenticate,
53 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
54 listPluginsValidator,
55 paginationValidator,
56 pluginsSortValidator,
57 setDefaultSort,
58 setDefaultPagination,
59 asyncMiddleware(listPlugins)
60)
61
ba211e73 62pluginRouter.get('/:npmName/registered-settings',
ad91e700
C
63 authenticate,
64 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
dba85a1e 65 asyncMiddleware(existingPluginValidator),
ba211e73 66 getPluginRegisteredSettings
ad91e700
C
67)
68
ba211e73 69pluginRouter.get('/:npmName/public-settings',
dba85a1e 70 asyncMiddleware(existingPluginValidator),
ba211e73 71 getPublicPluginSettings
dba85a1e
C
72)
73
74pluginRouter.put('/:npmName/settings',
ad91e700
C
75 authenticate,
76 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
77 updatePluginSettingsValidator,
dba85a1e 78 asyncMiddleware(existingPluginValidator),
ad91e700
C
79 asyncMiddleware(updatePluginSettings)
80)
81
ba211e73
C
82pluginRouter.get('/:npmName',
83 authenticate,
84 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
85 asyncMiddleware(existingPluginValidator),
86 getPlugin
87)
88
ad91e700 89pluginRouter.post('/install',
1333ab1f 90 openapiOperationDoc({ operationId: 'addPlugin' }),
ad91e700
C
91 authenticate,
92 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
b5f919ac 93 installOrUpdatePluginValidator,
ad91e700
C
94 asyncMiddleware(installPlugin)
95)
96
b5f919ac 97pluginRouter.post('/update',
1333ab1f 98 openapiOperationDoc({ operationId: 'updatePlugin' }),
b5f919ac
C
99 authenticate,
100 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
101 installOrUpdatePluginValidator,
102 asyncMiddleware(updatePlugin)
103)
104
ad91e700 105pluginRouter.post('/uninstall',
1333ab1f 106 openapiOperationDoc({ operationId: 'uninstallPlugin' }),
ad91e700
C
107 authenticate,
108 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
109 uninstallPluginValidator,
110 asyncMiddleware(uninstallPlugin)
111)
112
113// ---------------------------------------------------------------------------
114
115export {
116 pluginRouter
117}
118
119// ---------------------------------------------------------------------------
120
121async function listPlugins (req: express.Request, res: express.Response) {
6702a1b2 122 const pluginType = req.query.pluginType
09071200 123 const uninstalled = req.query.uninstalled
ad91e700
C
124
125 const resultList = await PluginModel.listForApi({
6702a1b2 126 pluginType,
09071200 127 uninstalled,
ad91e700
C
128 start: req.query.start,
129 count: req.query.count,
130 sort: req.query.sort
131 })
132
133 return res.json(getFormattedObjects(resultList.data, resultList.total))
134}
135
dba85a1e
C
136function getPlugin (req: express.Request, res: express.Response) {
137 const plugin = res.locals.plugin
138
139 return res.json(plugin.toFormattedJSON())
140}
141
ad91e700 142async function installPlugin (req: express.Request, res: express.Response) {
b5f919ac 143 const body: InstallOrUpdatePlugin = req.body
ad91e700 144
8d2be0ed
C
145 const fromDisk = !!body.path
146 const toInstall = body.npmName || body.path
147 try {
b5f919ac
C
148 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
149
150 return res.json(plugin.toFormattedJSON())
8d2be0ed
C
151 } catch (err) {
152 logger.warn('Cannot install plugin %s.', toInstall, { err })
76148b27 153 return res.fail({ message: 'Cannot install plugin ' + toInstall })
8d2be0ed 154 }
b5f919ac 155}
ad91e700 156
b5f919ac
C
157async function updatePlugin (req: express.Request, res: express.Response) {
158 const body: InstallOrUpdatePlugin = req.body
159
160 const fromDisk = !!body.path
161 const toUpdate = body.npmName || body.path
162 try {
8280d0c2 163 const plugin = await PluginManager.Instance.update(toUpdate, fromDisk)
b5f919ac
C
164
165 return res.json(plugin.toFormattedJSON())
166 } catch (err) {
167 logger.warn('Cannot update plugin %s.', toUpdate, { err })
76148b27 168 return res.fail({ message: 'Cannot update plugin ' + toUpdate })
b5f919ac 169 }
ad91e700
C
170}
171
172async function uninstallPlugin (req: express.Request, res: express.Response) {
173 const body: ManagePlugin = req.body
174
175 await PluginManager.Instance.uninstall(body.npmName)
176
76148b27 177 return res.status(HttpStatusCode.NO_CONTENT_204).end()
ad91e700
C
178}
179
ba211e73
C
180function getPublicPluginSettings (req: express.Request, res: express.Response) {
181 const plugin = res.locals.plugin
182 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
183 const publicSettings = plugin.getPublicSettings(registeredSettings)
184
185 const json: PublicServerSetting = { publicSettings }
186
187 return res.json(json)
188}
189
8d2be0ed 190function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
ba211e73 191 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
ad91e700 192
ba211e73 193 const json: RegisteredServerSettings = { registeredSettings }
09071200
C
194
195 return res.json(json)
ad91e700
C
196}
197
198async function updatePluginSettings (req: express.Request, res: express.Response) {
199 const plugin = res.locals.plugin
200
201 plugin.settings = req.body.settings
202 await plugin.save()
203
a5896799
C
204 await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
205
76148b27 206 return res.status(HttpStatusCode.NO_CONTENT_204).end()
ad91e700 207}
6702a1b2
C
208
209async function listAvailablePlugins (req: express.Request, res: express.Response) {
210 const query: PeertubePluginIndexList = req.query
211
212 const resultList = await listAvailablePluginsFromIndex(query)
213
e0ce715a 214 if (!resultList) {
76148b27
RK
215 return res.fail({
216 status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
217 message: 'Plugin index unavailable. Please retry later'
218 })
e0ce715a
C
219 }
220
6702a1b2
C
221 return res.json(resultList)
222}