From cf179d7f72160a17ed95a1877dd7f5c83b02047b Mon Sep 17 00:00:00 2001 From: John Livingston Date: Tue, 13 Feb 2024 12:49:22 +0100 Subject: [PATCH] Slow mode WIP (#192): * default delay is configurable in channel's chat rooms options. --- CHANGELOG.md | 1 + client/@types/global.d.ts | 2 ++ .../configuration/templates/channel.mustache | 19 +++++++++++++++++++ .../common/configuration/templates/channel.ts | 4 ++++ .../configuration/templates/logic/channel.ts | 15 +++++++++++++++ languages/en.yml | 8 ++++++++ .../mod_muc_http_defaults.lua | 10 ++++++++++ server/lib/configuration/channel/sanitize.ts | 8 ++++++++ server/lib/configuration/channel/storage.ts | 5 ++++- server/lib/routers/api/room.ts | 19 +++++++++++++++++-- shared/lib/types.ts | 3 +++ 11 files changed, 91 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7c5903f..bf4e176e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Slow mode (#192): * new option in room configuration to set the slow mode delay (new prosody module mod_muc_slow_mode). + * default delay is configurable in channel's chat rooms options. ### Minor changes and fixes diff --git a/client/@types/global.d.ts b/client/@types/global.d.ts index c13f008c..9b16cd5e 100644 --- a/client/@types/global.d.ts +++ b/client/@types/global.d.ts @@ -44,6 +44,8 @@ declare const LOC_LIVECHAT_CONFIGURATION_DESC: string declare const LOC_LIVECHAT_CONFIGURATION_PLEASE_SELECT: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_TITLE: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_DESC: string +declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_DEFAULT_SLOW_MODE_LABEL: string +declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_DEFAULT_SLOW_MODE_DESC: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_ENABLE_BOT_LABEL: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_BOT_OPTIONS_TITLE: string declare const LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL: string diff --git a/client/common/configuration/templates/channel.mustache b/client/common/configuration/templates/channel.mustache index 2b4a3ca2..f69d09f4 100644 --- a/client/common/configuration/templates/channel.mustache +++ b/client/common/configuration/templates/channel.mustache @@ -9,6 +9,25 @@

{{description}}

+
+
+

{{defaultSlowModeLabel}}

+

{{{defaultSlowModeDesc}}}

+
+
+
+ +
+

{{botOptions}}

diff --git a/client/common/configuration/templates/channel.ts b/client/common/configuration/templates/channel.ts index 6f77e009..35d0acf1 100644 --- a/client/common/configuration/templates/channel.ts +++ b/client/common/configuration/templates/channel.ts @@ -68,6 +68,10 @@ async function fillLabels ( view.title = await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_TITLE) view.description = await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_DESC) + view.defaultSlowModeLabel = await peertubeHelpers.translate( + LOC_LIVECHAT_CONFIGURATION_CHANNEL_DEFAULT_SLOW_MODE_LABEL + ) + view.defaultSlowModeDesc = await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_DEFAULT_SLOW_MODE_DESC) view.enableBot = await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_ENABLE_BOT_LABEL) view.botOptions = await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_BOT_OPTIONS_TITLE) view.forbiddenWords = await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL) diff --git a/client/common/configuration/templates/logic/channel.ts b/client/common/configuration/templates/logic/channel.ts index 77840c43..66454e75 100644 --- a/client/common/configuration/templates/logic/channel.ts +++ b/client/common/configuration/templates/logic/channel.ts @@ -165,8 +165,20 @@ async function vivifyConfigurationChannel ( const validateData: Function = async (channelConfigurationOptions: ChannelConfigurationOptions): Promise => { const botConf = channelConfigurationOptions.bot + const slowModeDefaultDelay = channelConfigurationOptions.slowMode.defaultDelay const errorFieldSelectors = [] + if ( + (typeof slowModeDefaultDelay !== 'number') || + isNaN(slowModeDefaultDelay) || + slowModeDefaultDelay < 0 || + slowModeDefaultDelay > 1000 + ) { + const selector = '#peertube-livechat-slow-mode-default-delay' + errorFieldSelectors.push(selector) + await displayError(selector, await translate(LOC_INVALID_VALUE)) + } + // If !bot.enabled, we don't have to validate these fields: // The backend will ignore those values. if (botConf.enabled) { @@ -220,6 +232,9 @@ async function vivifyConfigurationChannel ( const data = new FormData(form) removeDisplayedErrors() const channelConfigurationOptions: ChannelConfigurationOptions = { + slowMode: { + defaultDelay: parseInt(data.get('slow_mode_default_delay')?.toString() ?? '0') + }, bot: { enabled: data.get('bot') === '1', nickname: data.get('bot_nickname')?.toString() ?? '', diff --git a/languages/en.yml b/languages/en.yml index 88258c81..276d2305 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -331,6 +331,14 @@ livechat_configuration_desc: "Here you can configure some advanced options for c livechat_configuration_please_select: "Please select bellow one of your channel, to setup its chatting options." livechat_configuration_channel_title: "Channel options" livechat_configuration_channel_desc: "You can setup here some options for this channel (moderation policies, ...)." +livechat_configuration_channel_default_slow_mode_label: "Slow mode" +livechat_configuration_channel_default_slow_mode_desc: | + Default slow mode value for new chats: +
    +
  • 0: slow mode disabled
  • +
  • Any positive integer: minimum time (in seconds) between two messages from the same user (moderators are not concerned)
  • +
+ This value applies for new chat rooms. To change this value for an already existing room, you have to use the room configuration menu. livechat_configuration_channel_enable_bot_label: "Enable moderation bot" livechat_configuration_channel_bot_options_title: "Moderation bot options" livechat_configuration_channel_forbidden_words_label: "Forbidden words or expressions" diff --git a/prosody-modules/mod_muc_http_defaults/mod_muc_http_defaults.lua b/prosody-modules/mod_muc_http_defaults/mod_muc_http_defaults.lua index 5b2369a9..2bd1b5c4 100644 --- a/prosody-modules/mod_muc_http_defaults/mod_muc_http_defaults.lua +++ b/prosody-modules/mod_muc_http_defaults/mod_muc_http_defaults.lua @@ -1,8 +1,13 @@ -- Copyright (C) 2021 Kim Alvefur +-- Copyright (C) 2024 John Livingston -- -- This file is MIT licensed. Please see the -- COPYING file in the source package for more information. -- +-- This version contains a modification to take into account new config option "slow_mode_delay". +-- This option is introduced in the Peertube livechat plugin, by mod_muc_slow_mode. +-- There will be a XEP proposal. When done, these modifications will be submitted to the mod_muc_http_defaults maintainer. +-- local http = require "net.http"; local async = require "util.async"; @@ -104,6 +109,11 @@ local function apply_config(room, settings) -- mod_muc_mam if type(config.archiving) == "boolean" then room._config.archiving = config.archiving; end + + -- specific to peertube-plugin-livechat: + if (type(config.slow_mode_delay) == "number") and config.slow_mode_delay >= 0 then + room._data.slow_mode_delay = config.slow_mode_delay; + end elseif config ~= nil then module:log("error", "Invalid config returned from API for %s: %q", room.jid, config); return nil, "format", { field = "config" }; diff --git a/server/lib/configuration/channel/sanitize.ts b/server/lib/configuration/channel/sanitize.ts index 491f5ba3..b6787e77 100644 --- a/server/lib/configuration/channel/sanitize.ts +++ b/server/lib/configuration/channel/sanitize.ts @@ -23,6 +23,11 @@ async function sanitizeChannelConfigurationOptions ( throw new Error('Invalid data.bot data type') } + const slowModeData = data.slowMode ?? { defaultDelay: 0 } // not present in livechat <= 8.2.0 + if (typeof slowModeData !== 'object') { + throw new Error('Invalid data.slowMode data type') + } + const result: ChannelConfigurationOptions = { bot: { enabled: _readBoolean(botData, 'enabled'), @@ -31,6 +36,9 @@ async function sanitizeChannelConfigurationOptions ( quotes: _readQuotes(botData), commands: _readCommands(botData) // TODO: bannedJIDs + }, + slowMode: { + defaultDelay: _readInteger(slowModeData, 'defaultDelay', 0, 1000) } } diff --git a/server/lib/configuration/channel/storage.ts b/server/lib/configuration/channel/storage.ts index 681cfeae..5f1d7221 100644 --- a/server/lib/configuration/channel/storage.ts +++ b/server/lib/configuration/channel/storage.ts @@ -24,7 +24,7 @@ async function getChannelConfigurationOptions ( const logger = options.peertubeHelpers.logger const filePath = _getFilePath(options, channelId) if (!fs.existsSync(filePath)) { - logger.debug('No stored data for channel, returning default values') + logger.debug('No stored data for channel, returning null') return null } const content = await fs.promises.readFile(filePath, { @@ -42,6 +42,9 @@ function getDefaultChannelConfigurationOptions (_options: RegisterServerOptions) forbiddenWords: [], quotes: [], commands: [] + }, + slowMode: { + defaultDelay: 0 } } } diff --git a/server/lib/routers/api/room.ts b/server/lib/routers/api/room.ts index ad7b7f26..916686e1 100644 --- a/server/lib/routers/api/room.ts +++ b/server/lib/routers/api/room.ts @@ -7,6 +7,10 @@ import { Affiliations, getVideoAffiliations, getChannelAffiliations } from '../. import { fillVideoCustomFields } from '../../custom-fields' import { getChannelInfosById } from '../../database/channel' import { RoomChannel } from '../../room-channel' +import { + getChannelConfigurationOptions, + getDefaultChannelConfigurationOptions +} from '../../configuration/channel/storage' // See here for description: https://modules.prosody.im/mod_muc_http_defaults.html interface RoomDefaults { @@ -25,10 +29,19 @@ interface RoomDefaults { // historylength: number moderated?: boolean archiving?: boolean + + // Following fields are specific to livechat (for now), and requires a customized version for mod_muc_http_defaults. + slow_mode_delay?: number } affiliations?: Affiliations } +async function defaultSlowModeDelay (options: RegisterServerOptions, channelId: number): Promise { + const channelOptions = await getChannelConfigurationOptions(options, channelId) ?? + getDefaultChannelConfigurationOptions(options) + return channelOptions.slowMode.defaultDelay +} + /** * Instanciate the route for room APIs. * These APIs are used by Prosody to get room defaults from the Peertube server. @@ -74,8 +87,9 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router const roomDefaults: RoomDefaults = { config: { name: channelInfos.displayName, - description: '' + description: '', // subject: channelInfos.displayName + slow_mode_delay: await defaultSlowModeDelay(options, channelId) }, affiliations: affiliations } @@ -126,8 +140,9 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router config: { name: video.name, description: '', - language: video.language + language: video.language, // subject: video.name + slow_mode_delay: await defaultSlowModeDelay(options, video.channelId) }, affiliations: affiliations } diff --git a/shared/lib/types.ts b/shared/lib/types.ts index ab99b52d..e00321b5 100644 --- a/shared/lib/types.ts +++ b/shared/lib/types.ts @@ -74,6 +74,9 @@ interface ChannelConfigurationOptions { }> // TODO: bannedJIDs: string[] } + slowMode: { + defaultDelay: number + } } interface ChannelConfiguration {