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