2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2021-04-09 19:28:16 +00:00
|
|
|
import type { Router, Request, Response, NextFunction } from 'express'
|
2021-04-12 15:53:12 +00:00
|
|
|
import { diag } from '../diagnostic'
|
2021-04-10 11:57:47 +00:00
|
|
|
import { getBaseStaticRoute, isUserAdmin } from '../helpers'
|
2021-05-03 18:06:36 +00:00
|
|
|
import { asyncMiddleware } from '../middlewares/async'
|
2021-04-09 19:28:16 +00:00
|
|
|
|
2021-04-12 18:15:44 +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
|
|
|
|
|
2021-05-03 18:06:36 +00:00
|
|
|
router.get('/diagnostic', asyncMiddleware(
|
|
|
|
async (req: Request, res: Response, _next: NextFunction) => {
|
2021-04-09 19:28:16 +00:00
|
|
|
logger.info('Accessing peertube-plugin-livechat diagnostic tool.')
|
2021-05-18 16:06:11 +00:00
|
|
|
const src = getBaseStaticRoute(options) + 'settings/settings.js'
|
2021-04-09 19:28:16 +00:00
|
|
|
res.status(200)
|
|
|
|
res.type('html')
|
2022-12-07 17:36:16 +00:00
|
|
|
res.send(`<html>
|
|
|
|
<body><div>Loading...</div></body>
|
|
|
|
<script type="module" src="${src}"></script>
|
|
|
|
</html>
|
|
|
|
`)
|
2021-04-09 19:28:16 +00:00
|
|
|
}
|
2021-05-03 18:06:36 +00:00
|
|
|
))
|
2021-04-09 19:28:16 +00:00
|
|
|
|
2021-05-03 18:06:36 +00:00
|
|
|
router.post('/diagnostic/test', asyncMiddleware(
|
|
|
|
async (req: Request, res: Response, _next: NextFunction) => {
|
2021-04-09 19:28:16 +00:00
|
|
|
if (!res.locals.authenticated) {
|
2021-05-03 18:06:36 +00:00
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
2021-04-09 19:28:16 +00:00
|
|
|
}
|
2021-05-05 13:55:38 +00:00
|
|
|
if (!await isUserAdmin(options, res)) {
|
2021-05-03 18:06:36 +00:00
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
2021-04-10 11:57:47 +00:00
|
|
|
}
|
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
|
|
|
|
2021-04-12 18:15:44 +00:00
|
|
|
const result = await diag(test, options)
|
2021-04-09 19:28:16 +00:00
|
|
|
|
|
|
|
res.status(200)
|
|
|
|
res.json(result)
|
|
|
|
}
|
2021-05-03 18:06:36 +00:00
|
|
|
))
|
2021-04-09 19:28:16 +00:00
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
initSettingsRouter
|
|
|
|
}
|