peertube-plugin-livechat/server/lib/routers/settings.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-04-09 19:28:16 +00:00
import type { Router, Request, Response, NextFunction } from 'express'
import { diag } from '../diagnostic'
2021-04-10 11:57:47 +00:00
import { getBaseStaticRoute, isUserAdmin } from '../helpers'
2021-04-09 19:28:16 +00:00
async function initSettingsRouter (options: RegisterServerOptions): Promise<Router> {
const { peertubeHelpers, getRouter } = options
2021-04-09 19:28:16 +00:00
const router = getRouter()
const logger = peertubeHelpers.logger
router.get('/diagnostic', async (req: Request, res: Response, next: NextFunction) => {
try {
logger.info('Accessing peertube-plugin-livechat diagnostic tool.')
const src = getBaseStaticRoute() + 'settings/settings.js'
res.status(200)
res.type('html')
res.send('<html><body><div>Loading...</div></body><script src="' + src + '"></script></html>')
} catch (error) {
return next(error)
}
})
router.post('/diagnostic/test', async (req: Request, res: Response, next: NextFunction) => {
try {
if (!res.locals.authenticated) {
return res.sendStatus(403)
}
2021-04-10 11:57:47 +00:00
if (!isUserAdmin(res)) {
return res.sendStatus(403)
}
2021-04-09 19:28:16 +00:00
const test: string = req.body.test || ''
logger.info('Accessing peertube-plugin-livechat diagnostic tool, test "' + test + '".')
2021-04-10 02:16:25 +00:00
const result = await diag(test, options)
2021-04-09 19:28:16 +00:00
res.status(200)
res.json(result)
} catch (error) {
return next(error)
}
})
return router
}
export {
initSettingsRouter
}