]>
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' |
ba211e73 | 29 | import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting' |
2d53be02 | 30 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
ad91e700 C |
31 | |
32 | const pluginRouter = express.Router() | |
33 | ||
6702a1b2 C |
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 | ||
ad91e700 C |
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 | ||
ba211e73 | 56 | pluginRouter.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 | 63 | pluginRouter.get('/:npmName/public-settings', |
dba85a1e | 64 | asyncMiddleware(existingPluginValidator), |
ba211e73 | 65 | getPublicPluginSettings |
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 | ||
ba211e73 C |
76 | pluginRouter.get('/:npmName', |
77 | authenticate, | |
78 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
79 | asyncMiddleware(existingPluginValidator), | |
80 | getPlugin | |
81 | ) | |
82 | ||
ad91e700 C |
83 | pluginRouter.post('/install', |
84 | authenticate, | |
85 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
b5f919ac | 86 | installOrUpdatePluginValidator, |
ad91e700 C |
87 | asyncMiddleware(installPlugin) |
88 | ) | |
89 | ||
b5f919ac C |
90 | pluginRouter.post('/update', |
91 | authenticate, | |
92 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
93 | installOrUpdatePluginValidator, | |
94 | asyncMiddleware(updatePlugin) | |
95 | ) | |
96 | ||
ad91e700 C |
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) { | |
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 |
127 | function getPlugin (req: express.Request, res: express.Response) { |
128 | const plugin = res.locals.plugin | |
129 | ||
130 | return res.json(plugin.toFormattedJSON()) | |
131 | } | |
132 | ||
ad91e700 | 133 | async 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 |
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 }) | |
2d53be02 | 159 | return res.sendStatus(HttpStatusCode.BAD_REQUEST_400) |
b5f919ac | 160 | } |
ad91e700 C |
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 | ||
2d53be02 | 168 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
ad91e700 C |
169 | } |
170 | ||
ba211e73 C |
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 | ||
8d2be0ed | 181 | function 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 | ||
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 | ||
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 | |
200 | async 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 C |
207 | .json({ error: 'Plugin index unavailable. Please retry later' }) |
208 | .end() | |
209 | } | |
210 | ||
6702a1b2 C |
211 | return res.json(resultList) |
212 | } |