1
0
Fork 0
peertube/server/core/controllers/api/plugins.ts

231 lines
6.4 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04:00
import express from 'express'
import { logger } from '@server/helpers/logger.js'
import { getFormattedObjects } from '@server/helpers/utils.js'
import { listAvailablePluginsFromIndex } from '@server/lib/plugins/plugin-index.js'
import { PluginManager } from '@server/lib/plugins/plugin-manager.js'
import {
2023-06-20 08:17:34 -04:00
apiRateLimiter,
asyncMiddleware,
authenticate,
2021-05-11 06:04:47 -04:00
availablePluginsSortValidator,
ensureUserHasRight,
openapiOperationDoc,
paginationValidator,
2021-05-11 06:04:47 -04:00
pluginsSortValidator,
setDefaultPagination,
setDefaultSort
} from '@server/middlewares/index.js'
import {
existingPluginValidator,
2019-07-12 05:39:58 -04:00
installOrUpdatePluginValidator,
listAvailablePluginsValidator,
listPluginsValidator,
uninstallPluginValidator,
updatePluginSettingsValidator
} from '@server/middlewares/validators/plugins.js'
import { PluginModel } from '@server/models/server/plugin.js'
2021-05-11 06:04:47 -04:00
import {
2021-07-16 08:27:30 -04:00
HttpStatusCode,
2021-05-11 06:04:47 -04:00
InstallOrUpdatePlugin,
ManagePlugin,
PeertubePluginIndexList,
PublicServerSetting,
RegisteredServerSettings,
UserRight
} from '@peertube/peertube-models'
const pluginRouter = express.Router()
2023-06-20 08:17:34 -04:00
pluginRouter.use(apiRateLimiter)
pluginRouter.get('/available',
openapiOperationDoc({ operationId: 'getAvailablePlugins' }),
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
listAvailablePluginsValidator,
paginationValidator,
availablePluginsSortValidator,
setDefaultSort,
setDefaultPagination,
asyncMiddleware(listAvailablePlugins)
)
pluginRouter.get('/',
openapiOperationDoc({ operationId: 'getPlugins' }),
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
listPluginsValidator,
paginationValidator,
pluginsSortValidator,
setDefaultSort,
setDefaultPagination,
asyncMiddleware(listPlugins)
)
2019-07-26 03:35:43 -04:00
pluginRouter.get('/:npmName/registered-settings',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
asyncMiddleware(existingPluginValidator),
2019-07-26 03:35:43 -04:00
getPluginRegisteredSettings
)
2019-07-26 03:35:43 -04:00
pluginRouter.get('/:npmName/public-settings',
asyncMiddleware(existingPluginValidator),
2019-07-26 03:35:43 -04:00
getPublicPluginSettings
)
pluginRouter.put('/:npmName/settings',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
updatePluginSettingsValidator,
asyncMiddleware(existingPluginValidator),
asyncMiddleware(updatePluginSettings)
)
2019-07-26 03:35:43 -04:00
pluginRouter.get('/:npmName',
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
asyncMiddleware(existingPluginValidator),
getPlugin
)
pluginRouter.post('/install',
openapiOperationDoc({ operationId: 'addPlugin' }),
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
2019-07-12 05:39:58 -04:00
installOrUpdatePluginValidator,
asyncMiddleware(installPlugin)
)
2019-07-12 05:39:58 -04:00
pluginRouter.post('/update',
openapiOperationDoc({ operationId: 'updatePlugin' }),
2019-07-12 05:39:58 -04:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
installOrUpdatePluginValidator,
asyncMiddleware(updatePlugin)
)
pluginRouter.post('/uninstall',
openapiOperationDoc({ operationId: 'uninstallPlugin' }),
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 08:36:04 -04:00
const uninstalled = req.query.uninstalled
const resultList = await PluginModel.listForApi({
pluginType,
2019-07-19 08:36:04 -04: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 05:39:58 -04:00
const body: InstallOrUpdatePlugin = req.body
const fromDisk = !!body.path
const toInstall = body.npmName || body.path
const pluginVersion = body.pluginVersion && body.npmName
? body.pluginVersion
: undefined
try {
const plugin = await PluginManager.Instance.install({ toInstall, version: pluginVersion, fromDisk })
2019-07-12 05:39:58 -04:00
return res.json(plugin.toFormattedJSON())
} catch (err) {
logger.warn('Cannot install plugin %s.', toInstall, { err })
return res.fail({ message: 'Cannot install plugin ' + toInstall })
}
2019-07-12 05:39:58 -04:00
}
2019-07-12 05:39:58 -04: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 04:10:48 -04:00
const plugin = await PluginManager.Instance.update(toUpdate, fromDisk)
2019-07-12 05:39:58 -04:00
return res.json(plugin.toFormattedJSON())
} catch (err) {
logger.warn('Cannot update plugin %s.', toUpdate, { err })
return res.fail({ message: 'Cannot update plugin ' + toUpdate })
2019-07-12 05:39:58 -04:00
}
}
async function uninstallPlugin (req: express.Request, res: express.Response) {
const body: ManagePlugin = req.body
await PluginManager.Instance.uninstall({ npmName: body.npmName })
return res.status(HttpStatusCode.NO_CONTENT_204).end()
}
2019-07-26 03:35:43 -04: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 03:35:43 -04:00
const registeredSettings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
2019-07-26 03:35:43 -04:00
const json: RegisteredServerSettings = { registeredSettings }
2019-07-19 08:36:04 -04: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 03:28:39 -04:00
await PluginManager.Instance.onSettingsChanged(plugin.name, plugin.settings)
return res.status(HttpStatusCode.NO_CONTENT_204).end()
}
async function listAvailablePlugins (req: express.Request, res: express.Response) {
const query: PeertubePluginIndexList = req.query
const resultList = await listAvailablePluginsFromIndex(query)
2019-07-16 08:52:24 -04:00
if (!resultList) {
return res.fail({
status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
message: 'Plugin index unavailable. Please retry later'
})
2019-07-16 08:52:24 -04:00
}
return res.json(resultList)
}