2019-07-10 10:59:53 -04:00
|
|
|
import * as express from 'express'
|
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort
|
|
|
|
} from '../../middlewares'
|
2019-07-16 05:33:22 -04:00
|
|
|
import { availablePluginsSortValidator, pluginsSortValidator } from '../../middlewares/validators'
|
2019-07-10 10:59:53 -04:00
|
|
|
import { PluginModel } from '../../models/server/plugin'
|
|
|
|
import { UserRight } from '../../../shared/models/users'
|
|
|
|
import {
|
2019-07-11 08:40:19 -04:00
|
|
|
existingPluginValidator,
|
2019-07-12 05:39:58 -04:00
|
|
|
installOrUpdatePluginValidator,
|
2019-07-16 05:33:22 -04:00
|
|
|
listAvailablePluginsValidator,
|
2019-07-10 10:59:53 -04:00
|
|
|
listPluginsValidator,
|
|
|
|
uninstallPluginValidator,
|
|
|
|
updatePluginSettingsValidator
|
|
|
|
} from '../../middlewares/validators/plugins'
|
|
|
|
import { PluginManager } from '../../lib/plugins/plugin-manager'
|
2019-07-12 05:39:58 -04:00
|
|
|
import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model'
|
2019-07-10 10:59:53 -04:00
|
|
|
import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
|
2019-07-11 11:23:24 -04:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-07-16 05:33:22 -04:00
|
|
|
import { listAvailablePluginsFromIndex } from '../../lib/plugins/plugin-index'
|
|
|
|
import { PeertubePluginIndexList } from '../../../shared/models/plugins/peertube-plugin-index-list.model'
|
2019-07-24 05:17:42 -04:00
|
|
|
import { RegisteredServerSettings } from '../../../shared/models/plugins/register-server-setting.model'
|
2019-07-26 03:35:43 -04:00
|
|
|
import { PublicServerSetting } from '../../../shared/models/plugins/public-server.setting'
|
2019-07-10 10:59:53 -04:00
|
|
|
|
|
|
|
const pluginRouter = express.Router()
|
|
|
|
|
2019-07-16 05:33:22 -04:00
|
|
|
pluginRouter.get('/available',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
listAvailablePluginsValidator,
|
|
|
|
paginationValidator,
|
|
|
|
availablePluginsSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
asyncMiddleware(listAvailablePlugins)
|
|
|
|
)
|
|
|
|
|
2019-07-10 10:59:53 -04:00
|
|
|
pluginRouter.get('/',
|
|
|
|
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',
|
2019-07-10 10:59:53 -04:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
2019-07-11 08:40:19 -04:00
|
|
|
asyncMiddleware(existingPluginValidator),
|
2019-07-26 03:35:43 -04:00
|
|
|
getPluginRegisteredSettings
|
2019-07-10 10:59:53 -04:00
|
|
|
)
|
|
|
|
|
2019-07-26 03:35:43 -04:00
|
|
|
pluginRouter.get('/:npmName/public-settings',
|
2019-07-11 08:40:19 -04:00
|
|
|
asyncMiddleware(existingPluginValidator),
|
2019-07-26 03:35:43 -04:00
|
|
|
getPublicPluginSettings
|
2019-07-11 08:40:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
pluginRouter.put('/:npmName/settings',
|
2019-07-10 10:59:53 -04:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
updatePluginSettingsValidator,
|
2019-07-11 08:40:19 -04:00
|
|
|
asyncMiddleware(existingPluginValidator),
|
2019-07-10 10:59:53 -04:00
|
|
|
asyncMiddleware(updatePluginSettings)
|
|
|
|
)
|
|
|
|
|
2019-07-26 03:35:43 -04:00
|
|
|
pluginRouter.get('/:npmName',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
asyncMiddleware(existingPluginValidator),
|
|
|
|
getPlugin
|
|
|
|
)
|
|
|
|
|
2019-07-10 10:59:53 -04:00
|
|
|
pluginRouter.post('/install',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
2019-07-12 05:39:58 -04:00
|
|
|
installOrUpdatePluginValidator,
|
2019-07-10 10:59:53 -04:00
|
|
|
asyncMiddleware(installPlugin)
|
|
|
|
)
|
|
|
|
|
2019-07-12 05:39:58 -04:00
|
|
|
pluginRouter.post('/update',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
installOrUpdatePluginValidator,
|
|
|
|
asyncMiddleware(updatePlugin)
|
|
|
|
)
|
|
|
|
|
2019-07-10 10:59:53 -04:00
|
|
|
pluginRouter.post('/uninstall',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
uninstallPluginValidator,
|
|
|
|
asyncMiddleware(uninstallPlugin)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
pluginRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function listPlugins (req: express.Request, res: express.Response) {
|
2019-07-16 05:33:22 -04:00
|
|
|
const pluginType = req.query.pluginType
|
2019-07-19 08:36:04 -04:00
|
|
|
const uninstalled = req.query.uninstalled
|
2019-07-10 10:59:53 -04:00
|
|
|
|
|
|
|
const resultList = await PluginModel.listForApi({
|
2019-07-16 05:33:22 -04:00
|
|
|
pluginType,
|
2019-07-19 08:36:04 -04:00
|
|
|
uninstalled,
|
2019-07-10 10:59:53 -04:00
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
sort: req.query.sort
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2019-07-11 08:40:19 -04:00
|
|
|
function getPlugin (req: express.Request, res: express.Response) {
|
|
|
|
const plugin = res.locals.plugin
|
|
|
|
|
|
|
|
return res.json(plugin.toFormattedJSON())
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:59:53 -04:00
|
|
|
async function installPlugin (req: express.Request, res: express.Response) {
|
2019-07-12 05:39:58 -04:00
|
|
|
const body: InstallOrUpdatePlugin = req.body
|
2019-07-10 10:59:53 -04:00
|
|
|
|
2019-07-11 11:23:24 -04:00
|
|
|
const fromDisk = !!body.path
|
|
|
|
const toInstall = body.npmName || body.path
|
|
|
|
try {
|
2019-07-12 05:39:58 -04:00
|
|
|
const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
|
|
|
|
|
|
|
|
return res.json(plugin.toFormattedJSON())
|
2019-07-11 11:23:24 -04:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot install plugin %s.', toInstall, { err })
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
2019-07-12 05:39:58 -04:00
|
|
|
}
|
2019-07-10 10:59:53 -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 {
|
|
|
|
const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
|
|
|
|
|
|
|
|
return res.json(plugin.toFormattedJSON())
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot update plugin %s.', toUpdate, { err })
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
2019-07-10 10:59:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function uninstallPlugin (req: express.Request, res: express.Response) {
|
|
|
|
const body: ManagePlugin = req.body
|
|
|
|
|
|
|
|
await PluginManager.Instance.uninstall(body.npmName)
|
|
|
|
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:23:24 -04:00
|
|
|
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-10 10:59:53 -04:00
|
|
|
|
2019-07-26 03:35:43 -04:00
|
|
|
const json: RegisteredServerSettings = { registeredSettings }
|
2019-07-19 08:36:04 -04:00
|
|
|
|
|
|
|
return res.json(json)
|
2019-07-10 10:59:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2019-07-16 05:33:22 -04:00
|
|
|
|
|
|
|
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.status(503)
|
|
|
|
.json({ error: 'Plugin index unavailable. Please retry later' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:22 -04:00
|
|
|
return res.json(resultList)
|
|
|
|
}
|