Moderation.

This commit is contained in:
John Livingston
2023-09-19 20:49:14 +02:00
parent 4fe972dc10
commit 852221d232
3 changed files with 57 additions and 9 deletions

View File

@ -6,6 +6,10 @@ import { sanitizeChannelConfigurationOptions } from '../../configuration/channel
import * as fs from 'fs'
import * as path from 'path'
// FIXME: should be exported by xmppjs-chat-bot
type ConfigHandlers = ChannelCommonRoomConf['handlers']
type ConfigHandler = ConfigHandlers[0]
/**
* Get saved configuration options for the given channel.
* Can throw an exception.
@ -78,9 +82,18 @@ function channelConfigurationOptionsToBotRoomConf (
options: RegisterServerOptions,
channelConfigurationOptions: ChannelConfigurationOptions
): ChannelCommonRoomConf {
// Note concerning handlers:
// If we want the bot to correctly enable/disable the handlers,
// we must always define all handlers, even if not used.
const handlers: ConfigHandlers = []
handlers.push(_getForbiddenWordsHandler(
'forbidden_words_0',
channelConfigurationOptions.forbiddenWords
))
const roomConf: ChannelCommonRoomConf = {
enabled: channelConfigurationOptions.bot,
handlers: []
handlers
}
if (channelConfigurationOptions.botNickname && channelConfigurationOptions.botNickname !== '') {
roomConf.nick = channelConfigurationOptions.botNickname
@ -88,6 +101,39 @@ function channelConfigurationOptionsToBotRoomConf (
return roomConf
}
function _getForbiddenWordsHandler (
id: string,
forbiddenWords: string[],
reason?: string
): ConfigHandler {
const handler: ConfigHandler = {
type: 'moderate',
id,
enabled: false,
options: {
rules: []
}
}
if (forbiddenWords.length === 0) {
return handler
}
handler.enabled = true
// Note: on the Peertube frontend, channelConfigurationOptions.forbiddenWords
// is an array of RegExp definition (strings).
// They are validated one by bone.
// To increase the bot performance, we will join them all (hopping the bot will optimize them).
const rule: any = {
name: id,
regexp: '(?:' + forbiddenWords.join(')|(?:') + ')'
}
if (reason) {
rule.reason = reason
}
handler.options.rules.push(rule)
return handler
}
function _getFilePath (
options: RegisterServerOptions,
channelId: number | string