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