From 852221d232559fc0290b8d26411df404132a3403 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Tue, 19 Sep 2023 20:49:14 +0200 Subject: [PATCH] Moderation. --- package-lock.json | 16 ++++--- package.json | 2 +- server/lib/configuration/channel/storage.ts | 48 ++++++++++++++++++++- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 377af083..5d569d62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" diff --git a/package.json b/package.json index 9731b98e..71cb4c09 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/lib/configuration/channel/storage.ts b/server/lib/configuration/channel/storage.ts index fdc21ad9..963d4dc4 100644 --- a/server/lib/configuration/channel/storage.ts +++ b/server/lib/configuration/channel/storage.ts @@ -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