* changing delay by duration for slow mode options
* better labels
This commit is contained in:
John Livingston 2024-02-16 15:16:44 +01:00
parent 1e876ec43c
commit 3d9237624d
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
13 changed files with 60 additions and 57 deletions

View File

@ -5,8 +5,8 @@
### New features ### New features
* Slow mode (#192): * Slow mode (#192):
* new option in room configuration to set the slow mode delay (new prosody module mod_muc_slow_mode). * new option in room configuration to set the slow mode duration (new prosody module mod_muc_slow_mode).
* default delay is configurable in channel's chat rooms options. * default duration is configurable in channel's chat rooms options.
* backend rejects messages when the slow mode is not respected. * backend rejects messages when the slow mode is not respected.
* frontend: display an infobox when slow mode is active. * frontend: display an infobox when slow mode is active.

View File

@ -19,11 +19,11 @@
<label> <label>
<input <input
type="number" type="number"
name="slow_mode_default_delay" name="slow_mode_default_duration"
min="0" min="0"
max="1000" max="1000"
id="peertube-livechat-slow-mode-default-delay" id="peertube-livechat-slow-mode-default-duration"
value="{{channelConfiguration.configuration.slowMode.defaultDelay}}" value="{{channelConfiguration.configuration.slowMode.defaultDuration}}"
/> />
</label> </label>
</div> </div>

View File

@ -165,16 +165,16 @@ async function vivifyConfigurationChannel (
const validateData: Function = async (channelConfigurationOptions: ChannelConfigurationOptions): Promise<boolean> => { const validateData: Function = async (channelConfigurationOptions: ChannelConfigurationOptions): Promise<boolean> => {
const botConf = channelConfigurationOptions.bot const botConf = channelConfigurationOptions.bot
const slowModeDefaultDelay = channelConfigurationOptions.slowMode.defaultDelay const slowModeDefaultDuration = channelConfigurationOptions.slowMode.defaultDuration
const errorFieldSelectors = [] const errorFieldSelectors = []
if ( if (
(typeof slowModeDefaultDelay !== 'number') || (typeof slowModeDefaultDuration !== 'number') ||
isNaN(slowModeDefaultDelay) || isNaN(slowModeDefaultDuration) ||
slowModeDefaultDelay < 0 || slowModeDefaultDuration < 0 ||
slowModeDefaultDelay > 1000 slowModeDefaultDuration > 1000
) { ) {
const selector = '#peertube-livechat-slow-mode-default-delay' const selector = '#peertube-livechat-slow-mode-default-duration'
errorFieldSelectors.push(selector) errorFieldSelectors.push(selector)
await displayError(selector, await translate(LOC_INVALID_VALUE)) await displayError(selector, await translate(LOC_INVALID_VALUE))
} }
@ -233,7 +233,7 @@ async function vivifyConfigurationChannel (
removeDisplayedErrors() removeDisplayedErrors()
const channelConfigurationOptions: ChannelConfigurationOptions = { const channelConfigurationOptions: ChannelConfigurationOptions = {
slowMode: { slowMode: {
defaultDelay: parseInt(data.get('slow_mode_default_delay')?.toString() ?? '0') defaultDuration: parseInt(data.get('slow_mode_default_duration')?.toString() ?? '0')
}, },
bot: { bot: {
enabled: data.get('bot') === '1', enabled: data.get('bot') === '1',

View File

@ -28,20 +28,20 @@ class SlowMode extends CustomElement {
this.model = _converse.chatboxes.get(this.jid) this.model = _converse.chatboxes.get(this.jid)
await this.model.initialized await this.model.initialized
this.listenTo(this.model.config, 'change:slow_mode_delay', () => { this.listenTo(this.model.config, 'change:slow_mode_duration', () => {
this.requestUpdate() this.requestUpdate()
}) })
} }
render () { 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``
} }
return html` return html`
<converse-icon class="fa fa-info-circle" size="1.2em"></converse-icon> <converse-icon class="fa fa-info-circle" size="1.2em"></converse-icon>
${__( ${__(
'Slow mode is enabled, you have to wait %1$s seconds between two messages.', 'Slow mode is enabled, users can send a message every %1$s seconds.',
this.model.config.get('slow_mode_delay') this.model.config.get('slow_mode_duration')
)}` )}`
} }
} }

View File

@ -336,7 +336,7 @@ livechat_configuration_channel_default_slow_mode_desc: |
Default slow mode value for new chats: Default slow mode value for new chats:
<ul> <ul>
<li>0: slow mode disabled</li> <li>0: slow mode disabled</li>
<li>Any positive integer: minimum time (in seconds) between two messages from the same user (moderators are not concerned)</li> <li>Any positive integer: users can send a message every X seconds (moderators are not limited)</li>
</ul> </ul>
This value applies for new chat rooms. To change this value for an already existing room, you have to use the room configuration menu. 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_enable_bot_label: "Enable moderation bot"
@ -388,4 +388,4 @@ livechat_configuration_channel_bot_nickname: "Bot nickname"
invalid_value: "Invalid value." 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."

View File

@ -4,7 +4,7 @@
-- This file is MIT licensed. Please see the -- This file is MIT licensed. Please see the
-- COPYING file in the source package for more information. -- 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. -- 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. -- 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 if type(config.archiving) == "boolean" then room._config.archiving = config.archiving; end
-- specific to peertube-plugin-livechat: -- specific to peertube-plugin-livechat:
if (type(config.slow_mode_delay) == "number") and config.slow_mode_delay >= 0 then if (type(config.slow_mode_duration) == "number") and config.slow_mode_duration >= 0 then
room._data.slow_mode_delay = config.slow_mode_delay; room._data.slow_mode_duration = config.slow_mode_duration;
end end
elseif config ~= nil then elseif config ~= nil then
module:log("error", "Invalid config returned from API for %s: %q", room.jid, config); module:log("error", "Invalid config returned from API for %s: %q", room.jid, config);

View File

@ -12,12 +12,12 @@ Just enable the module on your MUC component.
The feature will be accessible throught the room configuration form. 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). 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. 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. By default, the field will be between muc#roomconfig_changesubject and muc#roomconfig_moderatedroom.
``` lua ``` lua
VirtualHost "muc.example.com" VirtualHost "muc.example.com"
modules_enabled = { "muc_slow_mode" } modules_enabled = { "muc_slow_mode" }
slow_mode_delay_form_position = 96 slow_mode_duration_form_position = 96
``` ```

View File

@ -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). -- 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. -- So there is an option to change this.
-- By default, field will be between muc#roomconfig_changesubject and muc#roomconfig_moderatedroom -- 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 -- Getter/Setter
local function get_slow_mode_delay(room) local function get_slow_mode_duration(room)
return room._data.slow_mode_delay or 0; return room._data.slow_mode_duration or 0;
end end
local function set_slow_mode_delay(room, delay) local function set_slow_mode_duration(room, duration)
if delay then if duration then
delay = assert(tonumber(delay), "Slow mode delay is not a valid number"); duration = assert(tonumber(duration), "Slow mode duration is not a valid number");
end end
if delay and delay < 0 then if duration and duration < 0 then
delay = 0; duration = 0;
end 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; return true;
end end
-- Discovering support -- Discovering support
local function add_disco_form(event) local function add_disco_form(event)
table.insert(event.form, { table.insert(event.form, {
name = "muc#roominfo_slow_mode_delay"; name = "muc#roominfo_slow_mode_duration";
value = ""; 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 end
module:hook("muc-disco#info", add_disco_form); module:hook("muc-disco#info", add_disco_form);
@ -63,17 +63,17 @@ module:hook("muc-disco#info", add_disco_form);
-- Config form declaration -- Config form declaration
local function add_form_option(event) local function add_form_option(event)
table.insert(event.form, { table.insert(event.form, {
name = "muc#roomconfig_slow_mode_delay"; name = "muc#roomconfig_slow_mode_duration";
type = "text-single"; type = "text-single";
datatype = "xs:integer"; datatype = "xs:integer";
label = "Slow Mode (0=disabled, any positive integer= minimal delay in seconds between two messages from the same user)"; label = "Slow Mode (0=disabled, any positive integer= users can send a message every X seconds.)";
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."; -- desc = "";
value = get_slow_mode_delay(event.room); value = get_slow_mode_duration(event.room);
}); });
end end
module:hook("muc-config-submitted/muc#roomconfig_slow_mode_delay", function(event) module:hook("muc-config-submitted/muc#roomconfig_slow_mode_duration", function(event)
if set_slow_mode_delay(event.room, event.value) then 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 -- status 104 = configuration change: Inform occupants that a non-privacy-related room configuration change has occurred
event.status_codes["104"] = true; event.status_codes["104"] = true;
end end
@ -86,8 +86,8 @@ function handle_groupchat(event)
local origin, stanza = event.origin, event.stanza; local origin, stanza = event.origin, event.stanza;
local room = event.room; local room = event.room;
local delay = get_slow_mode_delay(room) or 0; local duration = get_slow_mode_duration(room) or 0;
if delay <= 0 then if duration <= 0 then
-- no slow mode for this room -- no slow mode for this room
-- module:log("debug", "No slow mode for this room"); -- module:log("debug", "No slow mode for this room");
return; return;
@ -119,14 +119,14 @@ function handle_groupchat(event)
local previous = room.slow_mode_last_messages[actor_jid]; local previous = room.slow_mode_last_messages[actor_jid];
-- module:log( -- module:log(
-- "debug", -- "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, -- actor_jid,
-- previous or 0, -- previous or 0,
-- now, -- now,
-- delay, -- duration,
-- (now - (previous or 0)) -- (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"); -- module:log("debug", "Message accepted");
room.slow_mode_last_messages[actor_jid] = now; room.slow_mode_last_messages[actor_jid] = now;
return; return;
@ -139,7 +139,7 @@ function handle_groupchat(event)
"wait", "wait",
-- error_condition = 'policy-violation' (see RFC 6120 Defined Error Conditions https://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions) -- error_condition = 'policy-violation' (see RFC 6120 Defined Error Conditions https://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions)
"policy-violation", "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. -- Note: following commented lines were inspired by mod_muc_limits, but it seems it is not required.

View File

@ -23,7 +23,10 @@ async function sanitizeChannelConfigurationOptions (
throw new Error('Invalid data.bot data type') 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') { if (typeof slowModeData !== 'object') {
throw new Error('Invalid data.slowMode data type') throw new Error('Invalid data.slowMode data type')
} }
@ -38,7 +41,7 @@ async function sanitizeChannelConfigurationOptions (
// TODO: bannedJIDs // TODO: bannedJIDs
}, },
slowMode: { slowMode: {
defaultDelay: _readInteger(slowModeData, 'defaultDelay', 0, 1000) defaultDuration: _readInteger(slowModeData, 'defaultDuration', 0, 1000)
} }
} }

View File

@ -44,7 +44,7 @@ function getDefaultChannelConfigurationOptions (_options: RegisterServerOptions)
commands: [] commands: []
}, },
slowMode: { slowMode: {
defaultDelay: 0 defaultDuration: 0
} }
} }
} }

View File

@ -207,7 +207,7 @@ class ProsodyConfigContent {
this.muc.set('muc_room_default_history_length', 20) this.muc.set('muc_room_default_history_length', 20)
this.muc.add('modules_enabled', 'muc_slow_mode') 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 { useAnonymous (autoBanIP: boolean): void {

View File

@ -31,15 +31,15 @@ interface RoomDefaults {
archiving?: boolean archiving?: boolean
// Following fields are specific to livechat (for now), and requires a customized version for mod_muc_http_defaults. // 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 affiliations?: Affiliations
} }
async function defaultSlowModeDelay (options: RegisterServerOptions, channelId: number): Promise<number> { async function defaultSlowModeDuration (options: RegisterServerOptions, channelId: number): Promise<number> {
const channelOptions = await getChannelConfigurationOptions(options, channelId) ?? const channelOptions = await getChannelConfigurationOptions(options, channelId) ??
getDefaultChannelConfigurationOptions(options) getDefaultChannelConfigurationOptions(options)
return channelOptions.slowMode.defaultDelay return channelOptions.slowMode.defaultDuration
} }
/** /**
@ -89,7 +89,7 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router
name: channelInfos.displayName, name: channelInfos.displayName,
description: '', description: '',
// subject: channelInfos.displayName // subject: channelInfos.displayName
slow_mode_delay: await defaultSlowModeDelay(options, channelId) slow_mode_duration: await defaultSlowModeDuration(options, channelId)
}, },
affiliations: affiliations affiliations: affiliations
} }
@ -142,7 +142,7 @@ async function initRoomApiRouter (options: RegisterServerOptions, router: Router
description: '', description: '',
language: video.language, language: video.language,
// subject: video.name // subject: video.name
slow_mode_delay: await defaultSlowModeDelay(options, video.channelId) slow_mode_duration: await defaultSlowModeDuration(options, video.channelId)
}, },
affiliations: affiliations affiliations: affiliations
} }

View File

@ -75,7 +75,7 @@ interface ChannelConfigurationOptions {
// TODO: bannedJIDs: string[] // TODO: bannedJIDs: string[]
} }
slowMode: { slowMode: {
defaultDelay: number defaultDuration: number
} }
} }