From 34d6e8d53a0002f7fa57709b2d38673f721d67aa Mon Sep 17 00:00:00 2001 From: coffee-compiler <22056268-coffee-compiler@users.noreply.gitlab.com> Date: Sun, 18 Aug 2024 11:39:59 +0000 Subject: [PATCH] Restrict chat message length on client side --- CHANGELOG.md | 1 + common/src/comp/chat.rs | 1 + server/src/automod.rs | 8 +++----- voxygen/src/hud/chat.rs | 5 ++++- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b78b76b0b..3c25f0bebf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Localization support for prompt dialogs, diary sections, trade and group invitations, command descriptions. - Added Freezing Potion - Clear command to delete chat messages. +- Chat message capping to prevent too long messages before sending. ### Changed diff --git a/common/src/comp/chat.rs b/common/src/comp/chat.rs index 22783a3afb..75bc458397 100644 --- a/common/src/comp/chat.rs +++ b/common/src/comp/chat.rs @@ -197,6 +197,7 @@ pub type ChatMsg = GenericChatMsg; pub type UnresolvedChatMsg = GenericChatMsg; impl GenericChatMsg { + pub const MAX_BYTES_PLAYER_CHAT_MSG: usize = 256; pub const NPC_DISTANCE: f32 = 100.0; pub const NPC_SAY_DISTANCE: f32 = 30.0; pub const REGION_DISTANCE: f32 = 1000.0; diff --git a/server/src/automod.rs b/server/src/automod.rs index 4f6e683895..a9cf0bdabf 100644 --- a/server/src/automod.rs +++ b/server/src/automod.rs @@ -1,7 +1,7 @@ use crate::settings::ModerationSettings; use authc::Uuid; use censor::Censor; -use common::comp::{AdminRole, ChatType, Group}; +use common::comp::{AdminRole, ChatMsg, ChatType, Group}; use hashbrown::HashMap; use std::{ fmt, @@ -10,8 +10,6 @@ use std::{ }; use tracing::info; -pub const MAX_BYTES_CHAT_MSG: usize = 256; - pub enum ActionNote { SpamWarn, } @@ -45,7 +43,7 @@ impl fmt::Display for ActionErr { ActionErr::TooLong => write!( f, "Your message was too long, no more than {} characters are permitted.", - MAX_BYTES_CHAT_MSG + ChatMsg::MAX_BYTES_PLAYER_CHAT_MSG ), ActionErr::SpamMuted(dur) => write!( f, @@ -99,7 +97,7 @@ impl AutoMod { msg: &str, ) -> Result, ActionErr> { // TODO: Consider using grapheme cluster count instead of size in bytes - if msg.len() > MAX_BYTES_CHAT_MSG { + if msg.len() > ChatMsg::MAX_BYTES_PLAYER_CHAT_MSG { Err(ActionErr::TooLong) } else if !self.settings.automod // Is this a private chat message? diff --git a/voxygen/src/hud/chat.rs b/voxygen/src/hud/chat.rs index 240fd058ed..74c5799e11 100644 --- a/voxygen/src/hud/chat.rs +++ b/voxygen/src/hud/chat.rs @@ -504,7 +504,10 @@ impl<'a> Widget for Chat<'a> { .set(state.ids.chat_input, ui) { input.retain(|c| c != '\n'); - state.update(|s| s.input.message = input); + + if input.len() <= ChatMsg::MAX_BYTES_PLAYER_CHAT_MSG { + state.update(|s| s.input.message = input); + } } }