aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/plugins.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/controllers/api/plugins.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/controllers/api/plugins.ts')
-rw-r--r--server/controllers/api/plugins.ts230
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 @@
1import express from 'express'
2import { logger } from '@server/helpers/logger'
3import { getFormattedObjects } from '@server/helpers/utils'
4import { listAvailablePluginsFromIndex } from '@server/lib/plugins/plugin-index'
5import { PluginManager } from '@server/lib/plugins/plugin-manager'
6import {
7 apiRateLimiter,
8 asyncMiddleware,
9 authenticate,
10 availablePluginsSortValidator,
11 ensureUserHasRight,
12 openapiOperationDoc,
13 paginationValidator,
14 pluginsSortValidator,
15 setDefaultPagination,
16 setDefaultSort
17} from '@server/middlewares'
18import {
19 existingPluginValidator,
20 installOrUpdatePluginValidator,
21 listAvailablePluginsValidator,
22 listPluginsValidator,
23 uninstallPluginValidator,
24 updatePluginSettingsValidator
25} from '@server/middlewares/validators/plugins'
26import { PluginModel } from '@server/models/server/plugin'
27import {
28 HttpStatusCode,
29 InstallOrUpdatePlugin,
30 ManagePlugin,
31 PeertubePluginIndexList,
32 PublicServerSetting,
33 RegisteredServerSettings,
34 UserRight
35} from '@shared/models'
36
37const pluginRouter = express.Router()
38
39pluginRouter.use(apiRateLimiter)
40
41pluginRouter.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
53pluginRouter.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
65pluginRouter.get('/:npmName/registered-settings',
66 authenticate,
67 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
68 asyncMiddleware(existingPluginValidator),
69 getPluginRegisteredSettings
70)
71
72pluginRouter.get('/:npmName/public-settings',
73 asyncMiddleware(existingPluginValidator),
74 getPublicPluginSettings
75)
76
77pluginRouter.put('/:npmName/settings',
78 authenticate,
79 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
80 updatePluginSettingsValidator,
81 asyncMiddleware(existingPluginValidator),
82 asyncMiddleware(updatePluginSettings)
83)
84
85pluginRouter.get('/:npmName',
86 authenticate,
87 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
88 asyncMiddleware(existingPluginValidator),
89 getPlugin
90)
91
92pluginRouter.post('/install',
93 openapiOperationDoc({ operationId: 'addPlugin' }),
94 authenticate,
95 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
96 installOrUpdatePluginValidator,
97 asyncMiddleware(installPlugin)
98)
99
100pluginRouter.post('/update',
101 openapiOperationDoc({ operationId: 'updatePlugin' }),
102 authenticate,
103 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
104 installOrUpdatePluginValidator,
105 asyncMiddleware(updatePlugin)
106)
107
108pluginRouter.post('/uninstall',
109 openapiOperationDoc({ operationId: 'uninstallPlugin' }),
110 authenticate,
111 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
112 uninstallPluginValidator,
113 asyncMiddleware(uninstallPlugin)
114)
115
116// ---------------------------------------------------------------------------
117
118export {
119 pluginRouter
120}
121
122// ---------------------------------------------------------------------------
123
124async 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
139function getPlugin (req: express.Request, res: express.Response) {
140 const plugin = res.locals.plugin
141
142 return res.json(plugin.toFormattedJSON())
143}
144
145async 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
165async 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
180async 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
188function 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
198function 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
206async 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
217async 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}