PeerTube/server/controllers/api/plugins.ts

212 lines
6.3 KiB
TypeScript
Raw Normal View History

import * as express from 'express'
import { getFormattedObjects } from '../../helpers/utils'
import {
asyncMiddleware,
authenticate,
ensureUserHasRight,
paginationValidator,
setDefaultPagination,
setDefaultSort
} from '../../middlewares'
import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators'
import { PluginModel } from '../../models/server/plugin'
import { UserRight } from '../../../shared/models/users'
import {
existingPluginValidator,
2019-07-12 11:39:58 +02:00
installOrUpdatePluginValidator,
listAvailablePluginsValidator,
listPluginsValidator,
uninstallPluginValidator,
updatePluginSettingsValidator
} from '../../middlewares/validators/plugins'
import { PluginManager } from '../../lib/plugins/plugin-manager'
2019-07-12 11:39:58 +02:00
import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
import { logger } from '../../helpers/logger'
import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index'
import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
2019-07-24 11:17:42 +02:00
import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
2019-07-26 09:35:43 +02:00
import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
const pluginRouter = express.Router()
pluginRouter.get('/available',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
listAvailablePluginsValidator,
paginationValidator,
availablePluginsSortValidator,
setDefaultSort,
setDefaultPagination,
asyncMiddleware(listAvailablePlugins)
)
pluginRouter.get('/',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
listPluginsValidator,
paginationValidator,
pluginsSortValidator,
setDefaultSort,
setDefaultPagination,
asyncMiddleware(listPlugins)
)
2019-07-26 09:35:43 +02:00
pluginRouter.get('/:npmName/registered-settings',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
asyncMiddleware(existingPluginValidator),
2019-07-26 09:35:43 +02:00
getPluginRegisteredSettings
)
2019-07-26 09:35:43 +02:00
pluginRouter.get('/:npmName/public-settings',
asyncMiddleware(existingPluginValidator),
2019-07-26 09:35:43 +02:00
getPublicPluginSettings
)
pluginRouter.put('/:npmName/settings',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
updatePluginSettingsValidator,
asyncMiddleware(existingPluginValidator),
asyncMiddleware(updatePluginSettings)
)
2019-07-26 09:35:43 +02:00
pluginRouter.get('/:npmName',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
asyncMiddleware(existingPluginValidator),
getPlugin
)
pluginRouter.post('/install',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
2019-07-12 11:39:58 +02:00
installOrUpdatePluginValidator,
asyncMiddleware(installPlugin)
)
2019-07-12 11:39:58 +02:00
pluginRouter.post('/update',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
installOrUpdatePluginValidator,
asyncMiddleware(updatePlugin)
)
pluginRouter.post('/uninstall',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
uninstallPluginValidator,
asyncMiddleware(uninstallPlugin)
)
// ---------------------------------------------------------------------------
export {
pluginRouter
}
// ---------------------------------------------------------------------------
async function listPlugins (req: express.Request, res: express.Response) {
const pluginType = req.query.pluginType
2019-07-19 14:36:04 +02:00
const uninstalled = req.query.uninstalled
const resultList = await PluginModel.listForApi({
pluginType,
2019-07-19 14:36:04 +02:00
uninstalled,
start: req.query.start,
count: req.query.count,
sort: req.query.sort
})
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
function getPlugin (req: express.Request, res: express.Response) {
const plugin = res.locals.plugin
return res.json(plugin.toFormattedJSON())
}
async function installPlugin (req: express.Request, res: express.Response) {
2019-07-12 11:39:58 +02:00
const body: InstallOrUpdatePlugin = req.body
const fromDisk = !!body.path
const toInstall = body.npmName || body.path
try {
2019-07-12 11:39:58 +02:00
const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
return res.json(plugin.toFormattedJSON())
} catch (err) {
logger.warn('Cannot install plugin %s.', toInstall, { err })
return res.sendStatus(HttpStatusCode.BAD_REQUEST_400)
}
2019-07-12 11:39:58 +02:00
}
2019-07-12 11:39:58 +02:00
async function updatePlugin (req: express.Request, res: express.Response) {
const body: InstallOrUpdatePlugin = req.body
const fromDisk = !!body.path
const toUpdate = body.npmName || body.path
try {
2021-04-12 10:10:48 +02:00
const plugin = await PluginManager.Instance.update(toUpdate, fromDisk)
2019-07-12 11:39:58 +02:00
return res.json(plugin.toFormattedJSON())
} catch (err) {
logger.warn('Cannot update plugin %s.', toUpdate, { err })
return res.sendStatus(HttpStatusCode.BAD_REQUEST_400)
2019-07-12 11:39:58 +02:00
}
}
async function uninstallPlugin (req: express.Request, res: express.Response) {
const body: ManagePlugin = req.body
await PluginManager.Instance.uninstall(body.npmName)
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
2019-07-26 09:35:43 +02:00
function getPublicPluginSettings (req: express.Request, res: express.Response) {
const plugin = res.locals.plugin
const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
const publicSettings = plugin.getPublicSettings(registeredSettings)
const json: PublicServerSetting = { publicSettings }
return res.json(json)
}
function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
2019-07-26 09:35:43 +02:00
const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
2019-07-26 09:35:43 +02:00
const json: RegisteredServerSettings = { registeredSettings }
2019-07-19 14:36:04 +02:00
return res.json(json)
}
async function updatePluginSettings (req: express.Request, res: express.Response) {
const plugin = res.locals.plugin
plugin.settings = req.body.settings
await plugin.save()
2020-04-30 09:28:39 +02:00
await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
async function listAvailablePlugins (req: express.Request, res: express.Response) {
const query: PeertubePluginIndexList = req.query
const resultList = await listAvailablePluginsFromIndex(query)
2019-07-16 14:52:24 +02:00
if (!resultList) {
return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503)
2019-07-16 14:52:24 +02:00
.json({ error: 'Plugin index unavailable. Please retry later' })
}
return res.json(resultList)
}