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'
|
2023-09-06 13:23:39 +00:00
|
|
|
import { getCheckConfigurationChannelMiddleware } from '../../middlewares/configuration/channel'
|
2023-09-06 13:56:55 +00:00
|
|
|
import { checkConfigurationEnabledMiddleware } from '../../middlewares/configuration/configuration'
|
2023-09-06 13:23:39 +00:00
|
|
|
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([
|
2023-09-06 13:56:55 +00:00
|
|
|
checkConfigurationEnabledMiddleware(options),
|
2023-09-06 13:23:39 +00:00
|
|
|
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-06 13:23:39 +00:00
|
|
|
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([
|
2023-09-06 13:56:55 +00:00
|
|
|
checkConfigurationEnabledMiddleware(options),
|
2023-09-06 13:23:39 +00:00
|
|
|
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
|
2023-09-06 13:23:39 +00:00
|
|
|
logger.debug('Trying to save ChannelConfigurationOptions')
|
2023-08-08 16:26:40 +00:00
|
|
|
|
2023-09-06 13:23:39 +00:00
|
|
|
let configuration
|
2023-08-09 14:16:02 +00:00
|
|
|
try {
|
2023-09-06 13:23:39 +00:00
|
|
|
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.')
|
2023-08-09 14:39:12 +00:00
|
|
|
const result = {
|
2023-08-09 14:16:02 +00:00
|
|
|
channel: channelInfos,
|
2023-09-06 13:23:39 +00:00
|
|
|
configuration
|
2023-08-09 14:39:12 +00:00
|
|
|
}
|
2023-09-06 13:23:39 +00:00
|
|
|
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 {
|
2023-09-06 13:23:39 +00:00
|
|
|
initConfigurationApiRouter
|
2023-08-08 16:26:40 +00:00
|
|
|
}
|