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

192 lines
6.8 KiB
TypeScript
Raw Normal View History

2024-05-23 09:42:14 +00:00
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
2023-08-08 16:26:40 +00:00
import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { Router, Request, Response, NextFunction } from 'express'
2024-06-04 14:39:25 +00:00
import type { ChannelConfiguration, ChannelEmojisConfiguration, 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'
2024-06-04 14:39:25 +00:00
import { Emojis } from '../../../lib/emojis'
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 user = await options.peertubeHelpers.user.getAuthUser(res)
const initConverseJSParam = await getConverseJSParams(
options,
roomKey,
{
forcetype: req.query.forcetype === '1'
},
!!user
)
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)
2024-06-04 14:39:25 +00:00
const result: ChannelConfiguration = {
2023-09-18 10:23:35 +00:00
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.')
2024-06-04 14:39:25 +00:00
const result: ChannelConfiguration = {
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
]))
2024-05-28 15:56:24 +00:00
router.get('/configuration/channel/emojis/:channelId', asyncMiddleware([
checkConfigurationEnabledMiddleware(options),
2024-06-04 14:39:25 +00:00
getCheckConfigurationChannelMiddleware(options),
2024-05-28 15:56:24 +00:00
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
2024-06-04 14:39:25 +00:00
try {
if (!res.locals.channelInfos) {
throw new Error('Missing channelInfos in res.locals, should not happen')
}
const emojis = Emojis.singleton()
const channelInfos = res.locals.channelInfos as ChannelInfos
const channelEmojis =
(await emojis.channelCustomEmojisDefinition(channelInfos.id)) ??
emojis.emptyChannelDefinition()
const result: ChannelEmojisConfiguration = {
channel: channelInfos,
emojis: channelEmojis
}
res.status(200)
res.json(result)
} catch (err) {
logger.error(err)
2024-05-28 15:56:24 +00:00
res.sendStatus(500)
}
2024-06-04 14:39:25 +00:00
}
]))
2024-05-28 15:56:24 +00:00
2024-06-04 14:39:25 +00:00
router.post('/configuration/channel/emojis/:channelId', asyncMiddleware([
checkConfigurationEnabledMiddleware(options),
getCheckConfigurationChannelMiddleware(options),
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
try {
if (!res.locals.channelInfos) {
throw new Error('Missing channelInfos in res.locals, should not happen')
}
const emojis = Emojis.singleton()
const channelInfos = res.locals.channelInfos as ChannelInfos
const emojisDefinition = req.body
2024-06-06 09:36:07 +00:00
let emojisDefinitionSanitized, bufferInfos
2024-06-04 14:39:25 +00:00
try {
2024-06-06 09:36:07 +00:00
[emojisDefinitionSanitized, bufferInfos] = await emojis.sanitizeChannelDefinition(
channelInfos.id,
emojisDefinition
)
2024-06-04 14:39:25 +00:00
} catch (err) {
logger.warn(err)
res.sendStatus(400)
return
}
2024-06-06 09:36:07 +00:00
await emojis.saveChannelDefinition(channelInfos.id, emojisDefinitionSanitized, bufferInfos)
2024-06-04 14:39:25 +00:00
// Reloading data, to send them back to front:
const channelEmojis =
(await emojis.channelCustomEmojisDefinition(channelInfos.id)) ??
emojis.emptyChannelDefinition()
const result: ChannelEmojisConfiguration = {
channel: channelInfos,
emojis: channelEmojis
}
res.status(200)
res.json(result)
2024-06-04 14:39:25 +00:00
} catch (err) {
logger.error(err)
res.sendStatus(500)
}
2024-05-28 15:56:24 +00:00
}
]))
2023-08-08 16:26:40 +00:00
}
export {
initConfigurationApiRouter
2023-08-08 16:26:40 +00:00
}