]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/plugins.ts
Add Podcast RSS feeds (#5487)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / plugins.ts
CommitLineData
41fb13c3 1import 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
3a1157a6
JL
147
148 const pluginVersion = body.pluginVersion && body.npmName
149 ? body.pluginVersion
150 : undefined
151
8d2be0ed 152 try {
841ddf88 153 const plugin = await PluginManager.Instance.install({ toInstall, version: pluginVersion, fromDisk })
b5f919ac
C
154
155 return res.json(plugin.toFormattedJSON())
8d2be0ed
C
156 } catch (err) {
157 logger.warn('Cannot install plugin %s.', toInstall, { err })
76148b27 158 return res.fail({ message: 'Cannot install plugin ' + toInstall })
8d2be0ed 159 }
b5f919ac 160}
ad91e700 161
b5f919ac
C
162async function updatePlugin (req: express.Request, res: express.Response) {
163 const body: InstallOrUpdatePlugin = req.body
164
165 const fromDisk = !!body.path
166 const toUpdate = body.npmName || body.path
167 try {
8280d0c2 168 const plugin = await PluginManager.Instance.update(toUpdate, fromDisk)
b5f919ac
C
169
170 return res.json(plugin.toFormattedJSON())
171 } catch (err) {
172 logger.warn('Cannot update plugin %s.', toUpdate, { err })
76148b27 173 return res.fail({ message: 'Cannot update plugin ' + toUpdate })
b5f919ac 174 }
ad91e700
C
175}
176
177async function uninstallPlugin (req: express.Request, res: express.Response) {
178 const body: ManagePlugin = req.body
179
841ddf88 180 await PluginManager.Instance.uninstall({ npmName: body.npmName })
ad91e700 181
76148b27 182 return res.status(HttpStatusCode.NO_CONTENT_204).end()
ad91e700
C
183}
184
ba211e73
C
185function getPublicPluginSettings (req: express.Request, res: express.Response) {
186 const plugin = res.locals.plugin
187 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
188 const publicSettings = plugin.getPublicSettings(registeredSettings)
189
190 const json: PublicServerSetting = { publicSettings }
191
192 return res.json(json)
193}
194
8d2be0ed 195function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
ba211e73 196 const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
ad91e700 197
ba211e73 198 const json: RegisteredServerSettings = { registeredSettings }
09071200
C
199
200 return res.json(json)
ad91e700
C
201}
202
203async function updatePluginSettings (req: express.Request, res: express.Response) {
204 const plugin = res.locals.plugin
205
206 plugin.settings = req.body.settings
207 await plugin.save()
208
a5896799
C
209 await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
210
76148b27 211 return res.status(HttpStatusCode.NO_CONTENT_204).end()
ad91e700 212}
6702a1b2
C
213
214async function listAvailablePlugins (req: express.Request, res: express.Response) {
215 const query: PeertubePluginIndexList = req.query
216
217 const resultList = await listAvailablePluginsFromIndex(query)
218
e0ce715a 219 if (!resultList) {
76148b27
RK
220 return res.fail({
221 status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
222 message: 'Plugin index unavailable. Please retry later'
223 })
e0ce715a
C
224 }
225
6702a1b2
C
226 return res.json(resultList)
227}