]>
Commit | Line | Data |
---|---|---|
ad91e700 C |
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' | |
6702a1b2 | 11 | import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators' |
ad91e700 C |
12 | import { PluginModel } from '../../models/server/plugin' |
13 | import { UserRight } from '../../../shared/models/users' | |
14 | import { | |
dba85a1e | 15 | existingPluginValidator, |
b5f919ac | 16 | installOrUpdatePluginValidator, |
6702a1b2 | 17 | listAvailablePluginsValidator, |
ad91e700 C |
18 | listPluginsValidator, |
19 | uninstallPluginValidator, | |
20 | updatePluginSettingsValidator | |
21 | } from '../../middlewares/validators/plugins' | |
22 | import { PluginManager } from '../../lib/plugins/plugin-manager' | |
b5f919ac | 23 | import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model' |
ad91e700 | 24 | import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model' |
8d2be0ed | 25 | import { logger } from '../../helpers/logger' |
6702a1b2 C |
26 | import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index' |
27 | import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model' | |
9ae88819 | 28 | import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model' |
ad91e700 C |
29 | |
30 | const pluginRouter = express.Router() | |
31 | ||
6702a1b2 C |
32 | pluginRouter.get('/available', |
33 | authenticate, | |
34 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
35 | listAvailablePluginsValidator, | |
36 | paginationValidator, | |
37 | availablePluginsSortValidator, | |
38 | setDefaultSort, | |
39 | setDefaultPagination, | |
40 | asyncMiddleware(listAvailablePlugins) | |
41 | ) | |
42 | ||
ad91e700 C |
43 | pluginRouter.get('/', |
44 | authenticate, | |
45 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
46 | listPluginsValidator, | |
47 | paginationValidator, | |
48 | pluginsSortValidator, | |
49 | setDefaultSort, | |
50 | setDefaultPagination, | |
51 | asyncMiddleware(listPlugins) | |
52 | ) | |
53 | ||
dba85a1e | 54 | pluginRouter.get('/:npmName', |
ad91e700 C |
55 | authenticate, |
56 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
dba85a1e C |
57 | asyncMiddleware(existingPluginValidator), |
58 | getPlugin | |
ad91e700 C |
59 | ) |
60 | ||
dba85a1e C |
61 | pluginRouter.get('/:npmName/registered-settings', |
62 | authenticate, | |
63 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
64 | asyncMiddleware(existingPluginValidator), | |
8d2be0ed | 65 | getPluginRegisteredSettings |
dba85a1e C |
66 | ) |
67 | ||
68 | pluginRouter.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 | ||
76 | pluginRouter.post('/install', | |
77 | authenticate, | |
78 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
b5f919ac | 79 | installOrUpdatePluginValidator, |
ad91e700 C |
80 | asyncMiddleware(installPlugin) |
81 | ) | |
82 | ||
b5f919ac C |
83 | pluginRouter.post('/update', |
84 | authenticate, | |
85 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
86 | installOrUpdatePluginValidator, | |
87 | asyncMiddleware(updatePlugin) | |
88 | ) | |
89 | ||
ad91e700 C |
90 | pluginRouter.post('/uninstall', |
91 | authenticate, | |
92 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
93 | uninstallPluginValidator, | |
94 | asyncMiddleware(uninstallPlugin) | |
95 | ) | |
96 | ||
97 | // --------------------------------------------------------------------------- | |
98 | ||
99 | export { | |
100 | pluginRouter | |
101 | } | |
102 | ||
103 | // --------------------------------------------------------------------------- | |
104 | ||
105 | async function listPlugins (req: express.Request, res: express.Response) { | |
6702a1b2 | 106 | const pluginType = req.query.pluginType |
09071200 | 107 | const uninstalled = req.query.uninstalled |
ad91e700 C |
108 | |
109 | const resultList = await PluginModel.listForApi({ | |
6702a1b2 | 110 | pluginType, |
09071200 | 111 | uninstalled, |
ad91e700 C |
112 | start: req.query.start, |
113 | count: req.query.count, | |
114 | sort: req.query.sort | |
115 | }) | |
116 | ||
117 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
118 | } | |
119 | ||
dba85a1e C |
120 | function getPlugin (req: express.Request, res: express.Response) { |
121 | const plugin = res.locals.plugin | |
122 | ||
123 | return res.json(plugin.toFormattedJSON()) | |
124 | } | |
125 | ||
ad91e700 | 126 | async function installPlugin (req: express.Request, res: express.Response) { |
b5f919ac | 127 | const body: InstallOrUpdatePlugin = req.body |
ad91e700 | 128 | |
8d2be0ed C |
129 | const fromDisk = !!body.path |
130 | const toInstall = body.npmName || body.path | |
131 | try { | |
b5f919ac C |
132 | const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk) |
133 | ||
134 | return res.json(plugin.toFormattedJSON()) | |
8d2be0ed C |
135 | } catch (err) { |
136 | logger.warn('Cannot install plugin %s.', toInstall, { err }) | |
137 | return res.sendStatus(400) | |
138 | } | |
b5f919ac | 139 | } |
ad91e700 | 140 | |
b5f919ac C |
141 | async function updatePlugin (req: express.Request, res: express.Response) { |
142 | const body: InstallOrUpdatePlugin = req.body | |
143 | ||
144 | const fromDisk = !!body.path | |
145 | const toUpdate = body.npmName || body.path | |
146 | try { | |
147 | const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk) | |
148 | ||
149 | return res.json(plugin.toFormattedJSON()) | |
150 | } catch (err) { | |
151 | logger.warn('Cannot update plugin %s.', toUpdate, { err }) | |
152 | return res.sendStatus(400) | |
153 | } | |
ad91e700 C |
154 | } |
155 | ||
156 | async function uninstallPlugin (req: express.Request, res: express.Response) { | |
157 | const body: ManagePlugin = req.body | |
158 | ||
159 | await PluginManager.Instance.uninstall(body.npmName) | |
160 | ||
161 | return res.sendStatus(204) | |
162 | } | |
163 | ||
8d2be0ed | 164 | function getPluginRegisteredSettings (req: express.Request, res: express.Response) { |
b5f919ac | 165 | const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) |
ad91e700 | 166 | |
9ae88819 | 167 | const json: RegisteredServerSettings = { settings } |
09071200 C |
168 | |
169 | return res.json(json) | |
ad91e700 C |
170 | } |
171 | ||
172 | async function updatePluginSettings (req: express.Request, res: express.Response) { | |
173 | const plugin = res.locals.plugin | |
174 | ||
175 | plugin.settings = req.body.settings | |
176 | await plugin.save() | |
177 | ||
178 | return res.sendStatus(204) | |
179 | } | |
6702a1b2 C |
180 | |
181 | async function listAvailablePlugins (req: express.Request, res: express.Response) { | |
182 | const query: PeertubePluginIndexList = req.query | |
183 | ||
184 | const resultList = await listAvailablePluginsFromIndex(query) | |
185 | ||
e0ce715a C |
186 | if (!resultList) { |
187 | return res.status(503) | |
188 | .json({ error: 'Plugin index unavailable. Please retry later' }) | |
189 | .end() | |
190 | } | |
191 | ||
6702a1b2 C |
192 | return res.json(resultList) |
193 | } |