]>
Commit | Line | Data |
---|---|---|
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' | |
11 | import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators' | |
12 | import { PluginModel } from '../../models/server/plugin' | |
13 | import { UserRight } from '../../../shared/models/users' | |
14 | import { | |
15 | existingPluginValidator, | |
16 | installOrUpdatePluginValidator, | |
17 | listAvailablePluginsValidator, | |
18 | listPluginsValidator, | |
19 | uninstallPluginValidator, | |
20 | updatePluginSettingsValidator | |
21 | } from '../../middlewares/validators/plugins' | |
22 | import { PluginManager } from '../../lib/plugins/plugin-manager' | |
23 | import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model' | |
24 | import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model' | |
25 | import { logger } from '../../helpers/logger' | |
26 | import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index' | |
27 | import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model' | |
28 | import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model' | |
29 | import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting' | |
30 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
31 | ||
32 | const pluginRouter = express.Router() | |
33 | ||
34 | pluginRouter.get('/available', | |
35 | authenticate, | |
36 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
37 | listAvailablePluginsValidator, | |
38 | paginationValidator, | |
39 | availablePluginsSortValidator, | |
40 | setDefaultSort, | |
41 | setDefaultPagination, | |
42 | asyncMiddleware(listAvailablePlugins) | |
43 | ) | |
44 | ||
45 | pluginRouter.get('/', | |
46 | authenticate, | |
47 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
48 | listPluginsValidator, | |
49 | paginationValidator, | |
50 | pluginsSortValidator, | |
51 | setDefaultSort, | |
52 | setDefaultPagination, | |
53 | asyncMiddleware(listPlugins) | |
54 | ) | |
55 | ||
56 | pluginRouter.get('/:npmName/registered-settings', | |
57 | authenticate, | |
58 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
59 | asyncMiddleware(existingPluginValidator), | |
60 | getPluginRegisteredSettings | |
61 | ) | |
62 | ||
63 | pluginRouter.get('/:npmName/public-settings', | |
64 | asyncMiddleware(existingPluginValidator), | |
65 | getPublicPluginSettings | |
66 | ) | |
67 | ||
68 | pluginRouter.put('/:npmName/settings', | |
69 | authenticate, | |
70 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
71 | updatePluginSettingsValidator, | |
72 | asyncMiddleware(existingPluginValidator), | |
73 | asyncMiddleware(updatePluginSettings) | |
74 | ) | |
75 | ||
76 | pluginRouter.get('/:npmName', | |
77 | authenticate, | |
78 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
79 | asyncMiddleware(existingPluginValidator), | |
80 | getPlugin | |
81 | ) | |
82 | ||
83 | pluginRouter.post('/install', | |
84 | authenticate, | |
85 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
86 | installOrUpdatePluginValidator, | |
87 | asyncMiddleware(installPlugin) | |
88 | ) | |
89 | ||
90 | pluginRouter.post('/update', | |
91 | authenticate, | |
92 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
93 | installOrUpdatePluginValidator, | |
94 | asyncMiddleware(updatePlugin) | |
95 | ) | |
96 | ||
97 | pluginRouter.post('/uninstall', | |
98 | authenticate, | |
99 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
100 | uninstallPluginValidator, | |
101 | asyncMiddleware(uninstallPlugin) | |
102 | ) | |
103 | ||
104 | // --------------------------------------------------------------------------- | |
105 | ||
106 | export { | |
107 | pluginRouter | |
108 | } | |
109 | ||
110 | // --------------------------------------------------------------------------- | |
111 | ||
112 | async function listPlugins (req: express.Request, res: express.Response) { | |
113 | const pluginType = req.query.pluginType | |
114 | const uninstalled = req.query.uninstalled | |
115 | ||
116 | const resultList = await PluginModel.listForApi({ | |
117 | pluginType, | |
118 | uninstalled, | |
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 | ||
127 | function getPlugin (req: express.Request, res: express.Response) { | |
128 | const plugin = res.locals.plugin | |
129 | ||
130 | return res.json(plugin.toFormattedJSON()) | |
131 | } | |
132 | ||
133 | async function installPlugin (req: express.Request, res: express.Response) { | |
134 | const body: InstallOrUpdatePlugin = req.body | |
135 | ||
136 | const fromDisk = !!body.path | |
137 | const toInstall = body.npmName || body.path | |
138 | try { | |
139 | const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk) | |
140 | ||
141 | return res.json(plugin.toFormattedJSON()) | |
142 | } catch (err) { | |
143 | logger.warn('Cannot install plugin %s.', toInstall, { err }) | |
144 | return res.sendStatus(HttpStatusCode.BAD_REQUEST_400) | |
145 | } | |
146 | } | |
147 | ||
148 | async 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 { | |
154 | const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk) | |
155 | ||
156 | return res.json(plugin.toFormattedJSON()) | |
157 | } catch (err) { | |
158 | logger.warn('Cannot update plugin %s.', toUpdate, { err }) | |
159 | return res.sendStatus(HttpStatusCode.BAD_REQUEST_400) | |
160 | } | |
161 | } | |
162 | ||
163 | async function uninstallPlugin (req: express.Request, res: express.Response) { | |
164 | const body: ManagePlugin = req.body | |
165 | ||
166 | await PluginManager.Instance.uninstall(body.npmName) | |
167 | ||
168 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
169 | } | |
170 | ||
171 | function 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 | ||
181 | function getPluginRegisteredSettings (req: express.Request, res: express.Response) { | |
182 | const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) | |
183 | ||
184 | const json: RegisteredServerSettings = { registeredSettings } | |
185 | ||
186 | return res.json(json) | |
187 | } | |
188 | ||
189 | async 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 | ||
195 | await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings) | |
196 | ||
197 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
198 | } | |
199 | ||
200 | async function listAvailablePlugins (req: express.Request, res: express.Response) { | |
201 | const query: PeertubePluginIndexList = req.query | |
202 | ||
203 | const resultList = await listAvailablePluginsFromIndex(query) | |
204 | ||
205 | if (!resultList) { | |
206 | return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503) | |
207 | .json({ error: 'Plugin index unavailable. Please retry later' }) | |
208 | } | |
209 | ||
210 | return res.json(resultList) | |
211 | } |