peertube-plugin-livechat/server/lib/routers/api/configuration.ts

104 lines
4.0 KiB
TypeScript
Raw Normal View History

2023-08-08 16:26:40 +00:00
import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { Router, Request, Response, NextFunction } from 'express'
2023-08-09 14:16:02 +00:00
import type { ChannelInfos } from '../../../../shared/lib/types'
2023-08-08 16:26:40 +00:00
import { asyncMiddleware } from '../../middlewares/async'
import { getCheckConfigurationChannelMiddleware } from '../../middlewares/configuration/channel'
import { checkConfigurationEnabledMiddleware } from '../../middlewares/configuration/configuration'
2023-09-18 10:23:35 +00:00
import {
getChannelConfigurationOptions,
getDefaultChannelConfigurationOptions,
storeChannelConfigurationOptions
} from '../../configuration/channel/storage'
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
import { getConverseJSParams } from '../../../lib/conversejs/params'
2023-08-08 16:26:40 +00:00
2023-09-07 15:25:48 +00:00
async function initConfigurationApiRouter (options: RegisterServerOptions, router: Router): Promise<void> {
2023-08-08 16:26:40 +00:00
const logger = options.peertubeHelpers.logger
router.get('/configuration/room/:roomKey', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
const roomKey = req.params.roomKey
const initConverseJSParam = await getConverseJSParams(options, roomKey, {})
if (('isError' in initConverseJSParam) && initConverseJSParam.isError) {
res.sendStatus(initConverseJSParam.code)
return
}
res.status(200)
res.json(initConverseJSParam)
}
))
2023-09-07 15:25:48 +00:00
router.get('/configuration/channel/:channelId', asyncMiddleware([
checkConfigurationEnabledMiddleware(options),
getCheckConfigurationChannelMiddleware(options),
2023-08-08 16:26:40 +00:00
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
2023-08-09 14:16:02 +00:00
if (!res.locals.channelInfos) {
logger.error('Missing channelInfos in res.locals, should not happen')
res.sendStatus(500)
2023-08-08 16:26:40 +00:00
return
}
2023-08-09 14:16:02 +00:00
const channelInfos = res.locals.channelInfos as ChannelInfos
2023-09-18 10:23:35 +00:00
const channelOptions =
await getChannelConfigurationOptions(options, channelInfos.id) ??
getDefaultChannelConfigurationOptions(options)
const result = {
channel: channelInfos,
configuration: channelOptions
}
2023-08-09 14:16:02 +00:00
res.status(200)
res.json(result)
}
]))
2023-08-08 16:26:40 +00:00
2023-09-07 15:25:48 +00:00
router.post('/configuration/channel/:channelId', asyncMiddleware([
checkConfigurationEnabledMiddleware(options),
getCheckConfigurationChannelMiddleware(options),
2023-08-09 14:16:02 +00:00
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
if (!res.locals.channelInfos) {
logger.error('Missing channelInfos in res.locals, should not happen')
res.sendStatus(500)
2023-08-08 16:26:40 +00:00
return
}
2023-08-09 14:16:02 +00:00
const channelInfos = res.locals.channelInfos as ChannelInfos
logger.debug('Trying to save ChannelConfigurationOptions')
2023-08-08 16:26:40 +00:00
2023-09-18 10:23:35 +00:00
let channelOptions
2023-08-09 14:16:02 +00:00
try {
2023-09-21 15:13:28 +00:00
// Note: the front-end should do some input validation.
// If there is any invalid value, we just return a 400 error.
// The frontend should have prevented to post invalid data.
2023-09-21 17:32:47 +00:00
// Note: if !bot.enabled, we wont try to save hidden fields values, to minimize the risk of error
if (req.body.bot?.enabled === false) {
logger.debug('Bot disabled, loading the previous bot conf to not override hidden fields')
const channelOptions =
await getChannelConfigurationOptions(options, channelInfos.id) ??
getDefaultChannelConfigurationOptions(options)
req.body.bot = channelOptions.bot
2023-09-26 12:37:56 +00:00
req.body.bot.enabled = false
2023-09-21 17:32:47 +00:00
}
2023-09-18 10:23:35 +00:00
channelOptions = await sanitizeChannelConfigurationOptions(options, channelInfos.id, req.body)
2023-08-09 14:16:02 +00:00
} catch (err) {
logger.warn(err)
res.sendStatus(400)
2023-08-08 16:26:40 +00:00
return
}
2023-08-09 14:16:02 +00:00
logger.debug('Data seems ok, storing them.')
const result = {
2023-08-09 14:16:02 +00:00
channel: channelInfos,
2023-09-18 10:23:35 +00:00
configuration: channelOptions
}
2023-09-18 10:23:35 +00:00
await storeChannelConfigurationOptions(options, channelInfos.id, channelOptions)
2023-08-08 16:26:40 +00:00
res.status(200)
res.json(result)
}
2023-08-09 14:16:02 +00:00
]))
2023-08-08 16:26:40 +00:00
}
export {
initConfigurationApiRouter
2023-08-08 16:26:40 +00:00
}