mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 17:59:37 +02:00
WIP plugins: plugin settings on server side
This commit is contained in:
parent
ffb321bedc
commit
ad91e7006e
20 changed files with 419 additions and 61 deletions
121
server/controllers/api/plugins.ts
Normal file
121
server/controllers/api/plugins.ts
Normal file
|
@ -0,0 +1,121 @@
|
|||
import * as express from 'express'
|
||||
import { getFormattedObjects } from '../../helpers/utils'
|
||||
import {
|
||||
asyncMiddleware,
|
||||
authenticate,
|
||||
ensureUserHasRight,
|
||||
paginationValidator,
|
||||
setDefaultPagination,
|
||||
setDefaultSort
|
||||
} from '../../middlewares'
|
||||
import { pluginsSortValidator } from '../../middlewares/validators'
|
||||
import { PluginModel } from '../../models/server/plugin'
|
||||
import { UserRight } from '../../../shared/models/users'
|
||||
import {
|
||||
enabledPluginValidator,
|
||||
installPluginValidator,
|
||||
listPluginsValidator,
|
||||
uninstallPluginValidator,
|
||||
updatePluginSettingsValidator
|
||||
} from '../../middlewares/validators/plugins'
|
||||
import { PluginManager } from '../../lib/plugins/plugin-manager'
|
||||
import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
|
||||
import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
|
||||
|
||||
const pluginRouter = express.Router()
|
||||
|
||||
pluginRouter.get('/',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
||||
listPluginsValidator,
|
||||
paginationValidator,
|
||||
pluginsSortValidator,
|
||||
setDefaultSort,
|
||||
setDefaultPagination,
|
||||
asyncMiddleware(listPlugins)
|
||||
)
|
||||
|
||||
pluginRouter.get('/:pluginName/settings',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
||||
asyncMiddleware(enabledPluginValidator),
|
||||
asyncMiddleware(listPluginSettings)
|
||||
)
|
||||
|
||||
pluginRouter.put('/:pluginName/settings',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
||||
updatePluginSettingsValidator,
|
||||
asyncMiddleware(enabledPluginValidator),
|
||||
asyncMiddleware(updatePluginSettings)
|
||||
)
|
||||
|
||||
pluginRouter.post('/install',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
||||
installPluginValidator,
|
||||
asyncMiddleware(installPlugin)
|
||||
)
|
||||
|
||||
pluginRouter.post('/uninstall',
|
||||
authenticate,
|
||||
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
||||
uninstallPluginValidator,
|
||||
asyncMiddleware(uninstallPlugin)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
pluginRouter
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function listPlugins (req: express.Request, res: express.Response) {
|
||||
const type = req.query.type
|
||||
|
||||
const resultList = await PluginModel.listForApi({
|
||||
type,
|
||||
start: req.query.start,
|
||||
count: req.query.count,
|
||||
sort: req.query.sort
|
||||
})
|
||||
|
||||
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
||||
}
|
||||
|
||||
async function installPlugin (req: express.Request, res: express.Response) {
|
||||
const body: InstallPlugin = req.body
|
||||
|
||||
await PluginManager.Instance.install(body.npmName)
|
||||
|
||||
return res.sendStatus(204)
|
||||
}
|
||||
|
||||
async function uninstallPlugin (req: express.Request, res: express.Response) {
|
||||
const body: ManagePlugin = req.body
|
||||
|
||||
await PluginManager.Instance.uninstall(body.npmName)
|
||||
|
||||
return res.sendStatus(204)
|
||||
}
|
||||
|
||||
async function listPluginSettings (req: express.Request, res: express.Response) {
|
||||
const plugin = res.locals.plugin
|
||||
|
||||
const settings = await PluginManager.Instance.getSettings(plugin.name)
|
||||
|
||||
return res.json({
|
||||
settings
|
||||
})
|
||||
}
|
||||
|
||||
async function updatePluginSettings (req: express.Request, res: express.Response) {
|
||||
const plugin = res.locals.plugin
|
||||
|
||||
plugin.settings = req.body.settings
|
||||
await plugin.save()
|
||||
|
||||
return res.sendStatus(204)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue