Renaming 'moderation' pages to 'configuration'.

This commit is contained in:
John Livingston
2023-09-06 15:23:39 +02:00
parent 85e7598c1f
commit 5373fb1570
13 changed files with 132 additions and 130 deletions

View File

@ -1,19 +1,19 @@
import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { ChannelModerationOptions, ChannelInfos } from '../../../../shared/lib/types'
import type { ChannelConfigurationOptions, ChannelInfos } from '../../../../shared/lib/types'
/**
* Sanitize data so that they can safely be used/stored for channel moderation configuration.
* Sanitize data so that they can safely be used/stored for channel configuration configuration.
* Throw an error if the format is obviously wrong.
* Cleans data (removing empty values, ...)
* @param options Peertube server options
* @param _channelInfos Channel infos
* @param data Input data
*/
async function sanitizeChannelModerationOptions (
async function sanitizeChannelConfigurationOptions (
_options: RegisterServerOptions,
_channelInfos: ChannelInfos,
data: any
): Promise<ChannelModerationOptions> {
): Promise<ChannelConfigurationOptions> {
const result = {
bot: false,
bannedJIDs: [],
@ -28,7 +28,7 @@ async function sanitizeChannelModerationOptions (
if (!(f in data) || (typeof data[f] !== 'boolean')) {
throw new Error('Invalid data type for field ' + f)
}
result[f as keyof ChannelModerationOptions] = data[f]
result[f as keyof ChannelConfigurationOptions] = data[f]
}
// value/regexp array fields
for (const f of ['bannedJIDs', 'forbiddenWords']) {
@ -50,7 +50,7 @@ async function sanitizeChannelModerationOptions (
} catch (_err) {
throw new Error('Invalid value in field ' + f)
}
(result[f as keyof ChannelModerationOptions] as string[]).push(v)
(result[f as keyof ChannelConfigurationOptions] as string[]).push(v)
}
}
@ -58,5 +58,5 @@ async function sanitizeChannelModerationOptions (
}
export {
sanitizeChannelModerationOptions
sanitizeChannelConfigurationOptions
}

View File

@ -1,27 +1,27 @@
import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { ChannelModeration, ChannelInfos } from '../../../../shared/lib/types'
import { sanitizeChannelModerationOptions } from '../../moderation/channel/sanitize'
import type { ChannelConfiguration, ChannelInfos } from '../../../../shared/lib/types'
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
import * as fs from 'fs'
import * as path from 'path'
/**
* Get saved moderation options for the given channel.
* Get saved configuration options for the given channel.
* Can throw an exception.
* @param options Peertube server options
* @param channelInfos Info from channel from which we want to get infos
* @returns Channel moderation data
* @returns Channel configuration data
*/
async function getChannelModerationOptions (
async function getChannelConfigurationOptions (
options: RegisterServerOptions,
channelInfos: ChannelInfos
): Promise<ChannelModeration> {
): Promise<ChannelConfiguration> {
const logger = options.peertubeHelpers.logger
const filePath = _getFilePath(options, channelInfos)
if (!fs.existsSync(filePath)) {
logger.debug('No stored data for channel, returning default values')
return {
channel: channelInfos,
moderation: {
configuration: {
bot: false,
bannedJIDs: [],
forbiddenWords: []
@ -31,24 +31,24 @@ async function getChannelModerationOptions (
const content = await fs.promises.readFile(filePath, {
encoding: 'utf-8'
})
const sanitized = await sanitizeChannelModerationOptions(options, channelInfos, JSON.parse(content))
const sanitized = await sanitizeChannelConfigurationOptions(options, channelInfos, JSON.parse(content))
return {
channel: channelInfos,
moderation: sanitized
configuration: sanitized
}
}
/**
* Save channel moderation options.
* Save channel configuration options.
* Can throw an exception.
* @param _options Peertube server options
* @param _channelModeration data to save
* @param options Peertube server options
* @param channelConfiguration data to save
*/
async function storeChannelModerationOptions (
async function storeChannelConfigurationOptions (
options: RegisterServerOptions,
channelModeration: ChannelModeration
channelConfiguration: ChannelConfiguration
): Promise<void> {
const channelInfos = channelModeration.channel
const channelInfos = channelConfiguration.channel
const filePath = _getFilePath(options, channelInfos)
if (!fs.existsSync(filePath)) {
@ -58,7 +58,7 @@ async function storeChannelModerationOptions (
}
}
const jsonContent = JSON.stringify(channelModeration.moderation)
const jsonContent = JSON.stringify(channelConfiguration.configuration)
await fs.promises.writeFile(filePath, jsonContent, {
encoding: 'utf-8'
@ -77,12 +77,12 @@ function _getFilePath (
return path.resolve(
options.peertubeHelpers.plugin.getDataDirectoryPath(),
'channelModerationOptions',
'channelConfigurationOptions',
channelId.toString() + '.json'
)
}
export {
getChannelModerationOptions,
storeChannelModerationOptions
getChannelConfigurationOptions,
storeChannelConfigurationOptions
}

View File

@ -6,11 +6,11 @@ import { isUserAdminOrModerator } from '../../helpers'
/**
* Returns a middleware handler to get the channelInfos from the channel parameter.
* This is used in api related to channel moderation options.
* This is used in api related to channel configuration options.
* @param options Peertube server options
* @returns middleware function
*/
function getCheckModerationChannelMiddleware (options: RegisterServerOptions): RequestPromiseHandler {
function getCheckConfigurationChannelMiddleware (options: RegisterServerOptions): RequestPromiseHandler {
return async (req: Request, res: Response, next: NextFunction) => {
const logger = options.peertubeHelpers.logger
const channelId = req.params.channelId
@ -42,12 +42,12 @@ function getCheckModerationChannelMiddleware (options: RegisterServerOptions): R
return
}
logger.debug('User can access the moderation channel api.')
logger.debug('User can access the configuration channel api.')
res.locals.channelInfos = channelInfos
next()
}
}
export {
getCheckModerationChannelMiddleware
getCheckConfigurationChannelMiddleware
}

View File

@ -7,7 +7,7 @@ import { isDebugMode } from '../debug'
import { initRoomApiRouter } from './api/room'
import { initAuthApiRouter, initUserAuthApiRouter } from './api/auth'
import { initFederationServerInfosApiRouter } from './api/federation-server-infos'
import { initModerationApiRouter } from './api/moderation'
import { initConfigurationApiRouter } from './api/configuration'
/**
* Initiate API routes
@ -53,7 +53,7 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
))
}
router.use('/moderation', await initModerationApiRouter(options))
router.use('/configuration', await initConfigurationApiRouter(options))
return router
}

View File

@ -2,16 +2,16 @@ import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { Router, Request, Response, NextFunction } from 'express'
import type { ChannelInfos } from '../../../../shared/lib/types'
import { asyncMiddleware } from '../../middlewares/async'
import { getCheckModerationChannelMiddleware } from '../../middlewares/moderation/channel'
import { getChannelModerationOptions, storeChannelModerationOptions } from '../../moderation/channel/storage'
import { sanitizeChannelModerationOptions } from '../../moderation/channel/sanitize'
import { getCheckConfigurationChannelMiddleware } from '../../middlewares/configuration/channel'
import { getChannelConfigurationOptions, storeChannelConfigurationOptions } from '../../configuration/channel/storage'
import { sanitizeChannelConfigurationOptions } from '../../configuration/channel/sanitize'
async function initModerationApiRouter (options: RegisterServerOptions): Promise<Router> {
async function initConfigurationApiRouter (options: RegisterServerOptions): Promise<Router> {
const router = options.getRouter()
const logger = options.peertubeHelpers.logger
router.get('/channel/:channelId', asyncMiddleware([
getCheckModerationChannelMiddleware(options),
getCheckConfigurationChannelMiddleware(options),
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
if (!res.locals.channelInfos) {
logger.error('Missing channelInfos in res.locals, should not happen')
@ -20,14 +20,14 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
}
const channelInfos = res.locals.channelInfos as ChannelInfos
const result = await getChannelModerationOptions(options, channelInfos)
const result = await getChannelConfigurationOptions(options, channelInfos)
res.status(200)
res.json(result)
}
]))
router.post('/channel/:channelId', asyncMiddleware([
getCheckModerationChannelMiddleware(options),
getCheckConfigurationChannelMiddleware(options),
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
if (!res.locals.channelInfos) {
logger.error('Missing channelInfos in res.locals, should not happen')
@ -35,11 +35,11 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
return
}
const channelInfos = res.locals.channelInfos as ChannelInfos
logger.debug('Trying to save ChannelModerationOptions')
logger.debug('Trying to save ChannelConfigurationOptions')
let moderation
let configuration
try {
moderation = await sanitizeChannelModerationOptions(options, channelInfos, req.body)
configuration = await sanitizeChannelConfigurationOptions(options, channelInfos, req.body)
} catch (err) {
logger.warn(err)
res.sendStatus(400)
@ -49,9 +49,9 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
logger.debug('Data seems ok, storing them.')
const result = {
channel: channelInfos,
moderation
configuration
}
await storeChannelModerationOptions(options, result)
await storeChannelConfigurationOptions(options, result)
res.status(200)
res.json(result)
}
@ -61,5 +61,5 @@ async function initModerationApiRouter (options: RegisterServerOptions): Promise
}
export {
initModerationApiRouter
initConfigurationApiRouter
}