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