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

65 lines
2.2 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'
2023-08-09 14:16:02 +00:00
import { getCheckModerationChannelMiddleware } from '../../middlewares/moderation/channel'
import { getChannelModerationOptions, storeChannelModerationOptions } from '../../moderation/channel/storage'
import { sanitizeChannelModerationOptions } from '../../moderation/channel/sanitize'
2023-08-08 16:26:40 +00:00
async function initModerationApiRouter (options: RegisterServerOptions): Promise<Router> {
const router = options.getRouter()
const logger = options.peertubeHelpers.logger
2023-08-09 14:16:02 +00:00
router.get('/channel/:channelId', asyncMiddleware([
getCheckModerationChannelMiddleware(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 getChannelModerationOptions(options, channelInfos)
res.status(200)
res.json(result)
}
]))
2023-08-08 16:26:40 +00:00
2023-08-09 14:16:02 +00:00
router.post('/channel/:channelId', asyncMiddleware([
getCheckModerationChannelMiddleware(options),
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 ChannelModerationOptions')
2023-08-08 16:26:40 +00:00
2023-08-09 14:16:02 +00:00
let moderation
try {
moderation = await sanitizeChannelModerationOptions(options, channelInfos, req.body)
} 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 = await storeChannelModerationOptions(options, {
channel: channelInfos,
moderation
})
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
return router
}
export {
initModerationApiRouter
}