From 3d9237624d6176c92aad90e8673736a71c7bad1a Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 16 Feb 2024 15:16:44 +0100 Subject: [PATCH] Naming: * changing delay by duration for slow mode options * better labels --- CHANGELOG.md | 4 +- .../configuration/templates/channel.mustache | 6 +-- .../configuration/templates/logic/channel.ts | 14 +++--- .../custom/templates/muc-bottom-panel.js | 8 ++-- languages/en.yml | 4 +- .../mod_muc_http_defaults.lua | 6 +-- prosody-modules/mod_muc_slow_mode/README.md | 4 +- .../mod_muc_slow_mode/mod_muc_slow_mode.lua | 48 +++++++++---------- server/lib/configuration/channel/sanitize.ts | 7 ++- server/lib/configuration/channel/storage.ts | 2 +- server/lib/prosody/config/content.ts | 2 +- server/lib/routers/api/room.ts | 10 ++-- shared/lib/types.ts | 2 +- 13 files changed, 60 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1290c021..222b1a79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ ### New features * 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. + * new option in room configuration to set the slow mode duration (new prosody module mod_muc_slow_mode). + * default duration is configurable in channel's chat rooms options. * backend rejects messages when the slow mode is not respected. * frontend: display an infobox when slow mode is active. diff --git a/client/common/configuration/templates/channel.mustache b/client/common/configuration/templates/channel.mustache index f69d09f4..abd2068b 100644 --- a/client/common/configuration/templates/channel.mustache +++ b/client/common/configuration/templates/channel.mustache @@ -19,11 +19,11 @@ diff --git a/client/common/configuration/templates/logic/channel.ts b/client/common/configuration/templates/logic/channel.ts index 66454e75..0c028ed0 100644 --- a/client/common/configuration/templates/logic/channel.ts +++ b/client/common/configuration/templates/logic/channel.ts @@ -165,16 +165,16 @@ async function vivifyConfigurationChannel ( const validateData: Function = async (channelConfigurationOptions: ChannelConfigurationOptions): Promise => { const botConf = channelConfigurationOptions.bot - const slowModeDefaultDelay = channelConfigurationOptions.slowMode.defaultDelay + const slowModeDefaultDuration = channelConfigurationOptions.slowMode.defaultDuration const errorFieldSelectors = [] if ( - (typeof slowModeDefaultDelay !== 'number') || - isNaN(slowModeDefaultDelay) || - slowModeDefaultDelay < 0 || - slowModeDefaultDelay > 1000 + (typeof slowModeDefaultDuration !== 'number') || + isNaN(slowModeDefaultDuration) || + slowModeDefaultDuration < 0 || + slowModeDefaultDuration > 1000 ) { - const selector = '#peertube-livechat-slow-mode-default-delay' + const selector = '#peertube-livechat-slow-mode-default-duration' errorFieldSelectors.push(selector) await displayError(selector, await translate(LOC_INVALID_VALUE)) } @@ -233,7 +233,7 @@ async function vivifyConfigurationChannel ( removeDisplayedErrors() const channelConfigurationOptions: ChannelConfigurationOptions = { slowMode: { - defaultDelay: parseInt(data.get('slow_mode_default_delay')?.toString() ?? '0') + defaultDuration: parseInt(data.get('slow_mode_default_duration')?.toString() ?? '0') }, bot: { enabled: data.get('bot') === '1', diff --git a/conversejs/custom/templates/muc-bottom-panel.js b/conversejs/custom/templates/muc-bottom-panel.js index 69d85c60..e0b26540 100644 --- a/conversejs/custom/templates/muc-bottom-panel.js +++ b/conversejs/custom/templates/muc-bottom-panel.js @@ -28,20 +28,20 @@ class SlowMode extends CustomElement { this.model = _converse.chatboxes.get(this.jid) await this.model.initialized - this.listenTo(this.model.config, 'change:slow_mode_delay', () => { + this.listenTo(this.model.config, 'change:slow_mode_duration', () => { this.requestUpdate() }) } render () { - if (!(parseInt(this.model.config.get('slow_mode_delay')) > 0)) { // This includes NaN, for which ">0"===false + if (!(parseInt(this.model.config.get('slow_mode_duration')) > 0)) { // This includes NaN, for which ">0"===false return html`` } return html` ${__( - 'Slow mode is enabled, you have to wait %1$s seconds between two messages.', - this.model.config.get('slow_mode_delay') + 'Slow mode is enabled, users can send a message every %1$s seconds.', + this.model.config.get('slow_mode_duration') )}` } } diff --git a/languages/en.yml b/languages/en.yml index b500cc05..16778857 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -336,7 +336,7 @@ livechat_configuration_channel_default_slow_mode_desc: | Default slow mode value for new chats: 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" @@ -388,4 +388,4 @@ livechat_configuration_channel_bot_nickname: "Bot nickname" invalid_value: "Invalid value." -slow_mode_info: "Slow mode is enabled, you have to wait 10 seconds between two messages." +slow_mode_info: "Slow mode is enabled, users can send a message every %1$s seconds." 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 2bd1b5c4..ad9c4ca7 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 @@ -4,7 +4,7 @@ -- 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 version contains a modification to take into account new config option "slow_mode_duration". -- 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. -- @@ -111,8 +111,8 @@ local function apply_config(room, settings) 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; + if (type(config.slow_mode_duration) == "number") and config.slow_mode_duration >= 0 then + room._data.slow_mode_duration = config.slow_mode_duration; end elseif config ~= nil then module:log("error", "Invalid config returned from API for %s: %q", room.jid, config); diff --git a/prosody-modules/mod_muc_slow_mode/README.md b/prosody-modules/mod_muc_slow_mode/README.md index 71f213e0..7ccc6a01 100644 --- a/prosody-modules/mod_muc_slow_mode/README.md +++ b/prosody-modules/mod_muc_slow_mode/README.md @@ -12,12 +12,12 @@ Just enable the module on your MUC component. The feature will be accessible throught the room configuration form. Depending on your application, it is possible that the slow mode is more important than other fields (for example for a video streaming service). -The position in the room config form can be changed be setting the option `slow_mode_delay_form_position`. +The position in the room config form can be changed be setting the option `slow_mode_duration_form_position`. This value will be passed as priority for the "muc-config-form" hook. By default, the field will be between muc#roomconfig_changesubject and muc#roomconfig_moderatedroom. ``` lua VirtualHost "muc.example.com" modules_enabled = { "muc_slow_mode" } - slow_mode_delay_form_position = 96 + slow_mode_duration_form_position = 96 ``` diff --git a/prosody-modules/mod_muc_slow_mode/mod_muc_slow_mode.lua b/prosody-modules/mod_muc_slow_mode/mod_muc_slow_mode.lua index ce3623e3..8a81d21e 100644 --- a/prosody-modules/mod_muc_slow_mode/mod_muc_slow_mode.lua +++ b/prosody-modules/mod_muc_slow_mode/mod_muc_slow_mode.lua @@ -28,34 +28,34 @@ local xmlns_muc = "http://jabber.org/protocol/muc"; -- Depending on your application, it is possible that the slow mode is more important than other fields (for example for a video streaming service). -- So there is an option to change this. -- By default, field will be between muc#roomconfig_changesubject and muc#roomconfig_moderatedroom -local form_position = module:get_option_number("slow_mode_delay_form_position") or 80-2; +local form_position = module:get_option_number("slow_mode_duration_form_position") or 80-2; -- Getter/Setter -local function get_slow_mode_delay(room) - return room._data.slow_mode_delay or 0; +local function get_slow_mode_duration(room) + return room._data.slow_mode_duration or 0; end -local function set_slow_mode_delay(room, delay) - if delay then - delay = assert(tonumber(delay), "Slow mode delay is not a valid number"); +local function set_slow_mode_duration(room, duration) + if duration then + duration = assert(tonumber(duration), "Slow mode duration is not a valid number"); end - if delay and delay < 0 then - delay = 0; + if duration and duration < 0 then + duration = 0; end - if get_slow_mode_delay(room) == delay then return false; end + if get_slow_mode_duration(room) == duration then return false; end - room._data.slow_mode_delay = delay; + room._data.slow_mode_duration = duration; return true; end -- Discovering support local function add_disco_form(event) table.insert(event.form, { - name = "muc#roominfo_slow_mode_delay"; + name = "muc#roominfo_slow_mode_duration"; value = ""; }); - event.formdata["muc#roominfo_slow_mode_delay"] = get_slow_mode_delay(event.room); + event.formdata["muc#roominfo_slow_mode_duration"] = get_slow_mode_duration(event.room); end module:hook("muc-disco#info", add_disco_form); @@ -63,17 +63,17 @@ module:hook("muc-disco#info", add_disco_form); -- Config form declaration local function add_form_option(event) table.insert(event.form, { - name = "muc#roomconfig_slow_mode_delay"; + name = "muc#roomconfig_slow_mode_duration"; type = "text-single"; datatype = "xs:integer"; - label = "Slow Mode (0=disabled, any positive integer= minimal delay in seconds between two messages from the same user)"; - desc = "Minimal delay, in seconds, between two messages for the same user in the room. If value is set to 0, the slow mode is not active."; - value = get_slow_mode_delay(event.room); + label = "Slow Mode (0=disabled, any positive integer= users can send a message every X seconds.)"; + -- desc = ""; + value = get_slow_mode_duration(event.room); }); end -module:hook("muc-config-submitted/muc#roomconfig_slow_mode_delay", function(event) - if set_slow_mode_delay(event.room, event.value) then +module:hook("muc-config-submitted/muc#roomconfig_slow_mode_duration", function(event) + if set_slow_mode_duration(event.room, event.value) then -- status 104 = configuration change: Inform occupants that a non-privacy-related room configuration change has occurred event.status_codes["104"] = true; end @@ -86,8 +86,8 @@ function handle_groupchat(event) local origin, stanza = event.origin, event.stanza; local room = event.room; - local delay = get_slow_mode_delay(room) or 0; - if delay <= 0 then + local duration = get_slow_mode_duration(room) or 0; + if duration <= 0 then -- no slow mode for this room -- module:log("debug", "No slow mode for this room"); return; @@ -119,14 +119,14 @@ function handle_groupchat(event) local previous = room.slow_mode_last_messages[actor_jid]; -- module:log( -- "debug", - -- "Last message for user %s was at %s, now is %s, delay is %s, now - previous is %s", + -- "Last message for user %s was at %s, now is %s, duration is %s, now - previous is %s", -- actor_jid, -- previous or 0, -- now, - -- delay, + -- duration, -- (now - (previous or 0)) -- ); - if ((not previous) or (now - previous > delay)) then + if ((not previous) or (now - previous > duration)) then -- module:log("debug", "Message accepted"); room.slow_mode_last_messages[actor_jid] = now; return; @@ -139,7 +139,7 @@ function handle_groupchat(event) "wait", -- error_condition = 'policy-violation' (see RFC 6120 Defined Error Conditions https://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions) "policy-violation", - "You have exceeded the limit imposed by the slow mode in this room. You have to wait " .. delay .. " seconds between messages. Please try again later" + "You have exceeded the limit imposed by the slow mode in this room. You have to wait " .. duration .. " seconds between messages. Please try again later" ); -- Note: following commented lines were inspired by mod_muc_limits, but it seems it is not required. diff --git a/server/lib/configuration/channel/sanitize.ts b/server/lib/configuration/channel/sanitize.ts index b6787e77..176fe123 100644 --- a/server/lib/configuration/channel/sanitize.ts +++ b/server/lib/configuration/channel/sanitize.ts @@ -23,7 +23,10 @@ async function sanitizeChannelConfigurationOptions ( throw new Error('Invalid data.bot data type') } - const slowModeData = data.slowMode ?? { defaultDelay: 0 } // not present in livechat <= 8.2.0 + // slowMode not present in livechat <= 8.2.0: + const slowModeData = data.slowMode ?? {} + slowModeData.defaultDuration ??= 0 + if (typeof slowModeData !== 'object') { throw new Error('Invalid data.slowMode data type') } @@ -38,7 +41,7 @@ async function sanitizeChannelConfigurationOptions ( // TODO: bannedJIDs }, slowMode: { - defaultDelay: _readInteger(slowModeData, 'defaultDelay', 0, 1000) + defaultDuration: _readInteger(slowModeData, 'defaultDuration', 0, 1000) } } diff --git a/server/lib/configuration/channel/storage.ts b/server/lib/configuration/channel/storage.ts index 5f1d7221..450790f4 100644 --- a/server/lib/configuration/channel/storage.ts +++ b/server/lib/configuration/channel/storage.ts @@ -44,7 +44,7 @@ function getDefaultChannelConfigurationOptions (_options: RegisterServerOptions) commands: [] }, slowMode: { - defaultDelay: 0 + defaultDuration: 0 } } } diff --git a/server/lib/prosody/config/content.ts b/server/lib/prosody/config/content.ts index 0f2c2ebd..104ddc87 100644 --- a/server/lib/prosody/config/content.ts +++ b/server/lib/prosody/config/content.ts @@ -207,7 +207,7 @@ class ProsodyConfigContent { this.muc.set('muc_room_default_history_length', 20) this.muc.add('modules_enabled', 'muc_slow_mode') - this.muc.add('slow_mode_delay_form_position', 120) + this.muc.add('slow_mode_duration_form_position', 120) } useAnonymous (autoBanIP: boolean): void { diff --git a/server/lib/routers/api/room.ts b/server/lib/routers/api/room.ts index 916686e1..d1c2bd59 100644 --- a/server/lib/routers/api/room.ts +++ b/server/lib/routers/api/room.ts @@ -31,15 +31,15 @@ interface RoomDefaults { archiving?: boolean // Following fields are specific to livechat (for now), and requires a customized version for mod_muc_http_defaults. - slow_mode_delay?: number + slow_mode_duration?: number } affiliations?: Affiliations } -async function defaultSlowModeDelay (options: RegisterServerOptions, channelId: number): Promise { +async function defaultSlowModeDuration (options: RegisterServerOptions, channelId: number): Promise { const channelOptions = await getChannelConfigurationOptions(options, channelId) ?? getDefaultChannelConfigurationOptions(options) - return channelOptions.slowMode.defaultDelay + return channelOptions.slowMode.defaultDuration } /** @@ -89,7 +89,7 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router name: channelInfos.displayName, description: '', // subject: channelInfos.displayName - slow_mode_delay: await defaultSlowModeDelay(options, channelId) + slow_mode_duration: await defaultSlowModeDuration(options, channelId) }, affiliations: affiliations } @@ -142,7 +142,7 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router description: '', language: video.language, // subject: video.name - slow_mode_delay: await defaultSlowModeDelay(options, video.channelId) + slow_mode_duration: await defaultSlowModeDuration(options, video.channelId) }, affiliations: affiliations } diff --git a/shared/lib/types.ts b/shared/lib/types.ts index e00321b5..b851ad17 100644 --- a/shared/lib/types.ts +++ b/shared/lib/types.ts @@ -75,7 +75,7 @@ interface ChannelConfigurationOptions { // TODO: bannedJIDs: string[] } slowMode: { - defaultDelay: number + defaultDuration: number } }