Moderation.

This commit is contained in:
John Livingston 2023-09-19 20:49:14 +02:00
parent 4fe972dc10
commit 852221d232
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
3 changed files with 57 additions and 9 deletions

16
package-lock.json generated
View File

@ -15,7 +15,7 @@
"http-proxy": "^1.18.1",
"log-rotate": "^0.2.8",
"validate-color": "^2.2.1",
"xmppjs-chat-bot": "^0.2.4"
"xmppjs-chat-bot": "^0.2.5"
},
"devDependencies": {
"@peertube/feed": "^5.1.0",
@ -12026,9 +12026,9 @@
}
},
"node_modules/xmppjs-chat-bot": {
"version": "0.2.4",
"resolved": "https://registry.npmjs.org/xmppjs-chat-bot/-/xmppjs-chat-bot-0.2.4.tgz",
"integrity": "sha512-gS6oS9tpRENjrx5nr++PmlYZF6LlvsmLQqS56Di8npASIY9nXS7Ka+OvK13Kb6mzVTiR7HyG9qYQQLKVYpoUhg==",
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/xmppjs-chat-bot/-/xmppjs-chat-bot-0.2.5.tgz",
"integrity": "sha512-74gTrXmvz8a2xTm5oFdKxyeMWPz3MQZFF+TI6aB75IUhJlT03+SuGixB6u2rDyWGe6DHy1A9lsZFwCtdeQfE3g==",
"funding": [
"https://paypal.me/JohnXLivingston",
"https://liberapay.com/JohnLivingston/"
@ -12037,6 +12037,7 @@
"@xmpp/client": "^0.13.1",
"@xmpp/component": "^0.13.1",
"@xmpp/debug": "^0.13.0",
"@xmpp/id": "^0.13.1",
"@xmpp/jid": "^0.13.1",
"@xmpp/xml": "^0.13.1",
"commander": "^11.0.0"
@ -21248,13 +21249,14 @@
}
},
"xmppjs-chat-bot": {
"version": "0.2.4",
"resolved": "https://registry.npmjs.org/xmppjs-chat-bot/-/xmppjs-chat-bot-0.2.4.tgz",
"integrity": "sha512-gS6oS9tpRENjrx5nr++PmlYZF6LlvsmLQqS56Di8npASIY9nXS7Ka+OvK13Kb6mzVTiR7HyG9qYQQLKVYpoUhg==",
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/xmppjs-chat-bot/-/xmppjs-chat-bot-0.2.5.tgz",
"integrity": "sha512-74gTrXmvz8a2xTm5oFdKxyeMWPz3MQZFF+TI6aB75IUhJlT03+SuGixB6u2rDyWGe6DHy1A9lsZFwCtdeQfE3g==",
"requires": {
"@xmpp/client": "^0.13.1",
"@xmpp/component": "^0.13.1",
"@xmpp/debug": "^0.13.0",
"@xmpp/id": "^0.13.1",
"@xmpp/jid": "^0.13.1",
"@xmpp/xml": "^0.13.1",
"commander": "^11.0.0"

View File

@ -38,7 +38,7 @@
"http-proxy": "^1.18.1",
"log-rotate": "^0.2.8",
"validate-color": "^2.2.1",
"xmppjs-chat-bot": "^0.2.4"
"xmppjs-chat-bot": "^0.2.5"
},
"devDependencies": {
"@peertube/feed": "^5.1.0",

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