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

66 lines
2.5 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'
import { getChannelConfigurationOptions, storeChannelConfigurationOptions } from '../../configuration/channel/storage'
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
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
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
const result = await getChannelConfigurationOptions(options, channelInfos)
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
let configuration
2023-08-09 14:16:02 +00:00
try {
configuration = await sanitizeChannelConfigurationOptions(options, channelInfos, 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,
configuration
}
await storeChannelConfigurationOptions(options, result)
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
}