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