Initializing a diagnostic tools. WIP

This commit is contained in:
John Livingston
2021-04-09 21:28:16 +02:00
parent 8b3763761b
commit 59cd78ee82
7 changed files with 115 additions and 5 deletions

25
server/lib/helpers.ts Normal file
View File

@ -0,0 +1,25 @@
const packagejson: any = require('../../../package.json')
const version: string = packagejson.version || ''
if (!/^\d+\.\d+\.\d+/.test(version)) {
throw new Error('Incorrect version in package.json.')
}
const name: string = packagejson.name || ''
if (!/^peertube-plugin-[-a-z]+$/.test(name)) {
throw new Error('Incorrect plugin name in package.json.')
}
const shortName = name.substring('peertube-plugin-'.length)
// FIXME: in Peertube <= 3.1.0, PeertubeHelpers dont provide this function
function getBaseRouter (): string {
return '/plugins/' + shortName + '/router/'
}
// FIXME: in Peertube <= 3.1.0, PeertubeHelpers dont provide this function
function getBaseStaticRoute (): string {
return '/plugins/' + shortName + '/' + version + '/static/'
}
export {
getBaseRouter,
getBaseStaticRoute
}

View File

@ -1,5 +1,6 @@
import type { NextFunction, Request, Response } from 'express'
import { initWebchatRouter } from './webchat'
import { initSettingsRouter } from './settings'
async function initRouters (options: RegisterServerOptions): Promise<void> {
const { getRouter } = options
@ -8,6 +9,7 @@ async function initRouters (options: RegisterServerOptions): Promise<void> {
router.get('/ping', (req: Request, res: Response, _next: NextFunction) => res.json({ message: 'pong' }))
router.use('/webchat', await initWebchatRouter(options))
router.use('/settings', await initSettingsRouter(options))
}
export {

View File

@ -0,0 +1,52 @@
import type { Router, Request, Response, NextFunction } from 'express'
import { getBaseStaticRoute } from '../helpers'
async function initSettingsRouter ({
peertubeHelpers,
getRouter
}: RegisterServerOptions): Promise<Router> {
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)
}
// FIXME: test that user is admin.
logger.error('FIXME: test that user is admin')
const test: string = req.body.test || ''
logger.info('Accessing peertube-plugin-livechat diagnostic tool, test "' + test + '".')
const result: any = {
test: test,
message: null,
next: null,
ok: false
}
res.status(200)
res.json(result)
} catch (error) {
return next(error)
}
})
return router
}
export {
initSettingsRouter
}

View File

@ -1,10 +1,8 @@
interface InitSettingsOptions {
registerSetting: (options: RegisterServerSettingOptions) => void
}
import { getBaseRouter } from './helpers'
export function initSettings ({
registerSetting
}: InitSettingsOptions): void {
}: RegisterServerOptions): void {
registerSetting({
name: 'chat-auto-display',
label: 'Automatically open the chat',
@ -54,6 +52,18 @@ export function initSettings ({
private: false
})
registerSetting({
name: 'chat-use-prosody',
label: 'Use builtin Prosody XMPP Server',
type: 'input-checkbox',
// /!\ dont auto-activate on existing settups. FIXME: how to do this?
default: false, // TODO: set to true when peertube has fixed https://github.com/Chocobozzz/PeerTube/issues/3838
private: false,
descriptionHTML: 'If checked, this will use a builtin XMPP server. This is the recommanded setup.<br>' +
'TODO: add link to documentation.<br>' +
'<a href="' + getBaseRouter() + 'settings/diagnostic" target="_blank">Launch diagnostic</a>.<br>'
})
registerSetting({
name: 'chat-use-builtin',
label: 'Use builtin ConverseJS',