New option for the moderation bot: forbid duplicate messages (#516).

This commit is contained in:
John Livingston
2024-09-10 18:59:56 +02:00
parent 651641f63c
commit 5225257bb5
11 changed files with 227 additions and 10 deletions

View File

@ -57,7 +57,18 @@ async function sanitizeChannelConfigurationOptions (
applyToModerators: false
}
if (!_assertObjectType(botData.forbidSpecialChars)) {
throw new Error('Invalid data.forbidSpecialChars data type')
throw new Error('Invalid data.bot.forbidSpecialChars data type')
}
// noDuplicate comes with livechat 11.1.0
botData.noDuplicate ??= {
enabled: false,
reason: '',
delay: 60,
applyToModerators: false
}
if (!_assertObjectType(botData.noDuplicate)) {
throw new Error('Invalid data.bot.noDuplicate data type')
}
// terms not present in livechat <= 10.2.0
@ -76,6 +87,7 @@ async function sanitizeChannelConfigurationOptions (
nickname: _readSimpleInput(botData, 'nickname', true),
forbiddenWords: await _readForbiddenWords(botData),
forbidSpecialChars: await _readForbidSpecialChars(botData),
noDuplicate: await _readNoDuplicate(botData),
quotes: _readQuotes(botData),
commands: _readCommands(botData)
// TODO: bannedJIDs
@ -266,6 +278,21 @@ async function _readForbidSpecialChars (
return result
}
async function _readNoDuplicate (
botData: Record<string, unknown>
): Promise<ChannelConfigurationOptions['bot']['noDuplicate']> {
if (!_assertObjectType(botData.noDuplicate)) {
throw new Error('Invalid forbidSpecialChars data')
}
const result: ChannelConfigurationOptions['bot']['noDuplicate'] = {
enabled: _readBoolean(botData.noDuplicate, 'enabled'),
reason: _readSimpleInput(botData.noDuplicate, 'reason'),
delay: _readInteger(botData.noDuplicate, 'delay', 0, 24 * 3600),
applyToModerators: _readBoolean(botData.noDuplicate, 'applyToModerators')
}
return result
}
function _readQuotes (botData: Record<string, unknown>): ChannelConfigurationOptions['bot']['quotes'] {
if (!Array.isArray(botData.quotes)) {
throw new Error('Invalid quotes data')

View File

@ -50,6 +50,12 @@ function getDefaultChannelConfigurationOptions (_options: RegisterServerOptions)
tolerance: 0,
applyToModerators: false
},
noDuplicate: {
enabled: false,
reason: '',
delay: 60,
applyToModerators: false
},
quotes: [],
commands: []
},
@ -124,6 +130,11 @@ function channelConfigurationOptionsToBotRoomConf (
handlersIds.set(id, true)
handlers.push(_getForbidSpecialCharsHandler(id, channelConfigurationOptions.bot.forbidSpecialChars))
}
if (channelConfigurationOptions.bot.noDuplicate.enabled) {
const id = 'no_duplicate'
handlersIds.set(id, true)
handlers.push(_getNoDuplicateHandler(id, channelConfigurationOptions.bot.noDuplicate))
}
channelConfigurationOptions.bot.quotes.forEach((v, i) => {
const id = 'quote_' + i.toString()
handlersIds.set(id, true)
@ -254,6 +265,23 @@ function _getForbidSpecialCharsHandler (
return handler
}
function _getNoDuplicateHandler (
id: string,
noDuplicate: ChannelConfigurationOptions['bot']['noDuplicate']
): ConfigHandler {
const handler: ConfigHandler = {
type: 'no-duplicate',
id,
enabled: true,
options: {
reason: noDuplicate.reason,
delay: noDuplicate.delay,
applyToModerators: !!noDuplicate.applyToModerators
}
}
return handler
}
function _getQuotesHandler (
id: string,
quotes: ChannelConfigurationOptions['bot']['quotes'][0]

View File

@ -96,7 +96,7 @@ async function initConfigurationApiRouter (options: RegisterServerOptions, route
req.body.bot = channelOptions.bot
req.body.bot.enabled = false
}
// TODO: Same for forbidSpecialChars: if disabled, don't save reason and tolerance
// TODO: Same for forbidSpecialChars/noDuplicate: if disabled, don't save reason and tolerance
// (disabling for now, because it is not acceptable to load twice the channel configuration.
// Must find better way)
// if (req.body.bot?.enabled === true && req.body.bot.forbidSpecialChars?.enabled === false) {
@ -108,6 +108,7 @@ async function initConfigurationApiRouter (options: RegisterServerOptions, route
// req.body.bot.forbidSpecialChars.tolerance = channelOptions.bot.forbidSpecialChars.tolerance
// req.body.bot.forbidSpecialChars.applyToModerators = channelOptions.bot.forbidSpecialChars.applyToModerators
// req.body.bot.forbidSpecialChars.enabled = false
// ... NoDuplicate...
// }
channelOptions = await sanitizeChannelConfigurationOptions(options, channelInfos.id, req.body)
} catch (err) {