Restrict chat message length on client side

This commit is contained in:
coffee-compiler 2024-08-18 11:39:59 +00:00 committed by Marcel
parent b7d3dc998c
commit 34d6e8d53a
4 changed files with 9 additions and 6 deletions

View File

@ -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. - Localization support for prompt dialogs, diary sections, trade and group invitations, command descriptions.
- Added Freezing Potion - Added Freezing Potion
- Clear command to delete chat messages. - Clear command to delete chat messages.
- Chat message capping to prevent too long messages before sending.
### Changed ### Changed

View File

@ -197,6 +197,7 @@ pub type ChatMsg = GenericChatMsg<String>;
pub type UnresolvedChatMsg = GenericChatMsg<Group>; pub type UnresolvedChatMsg = GenericChatMsg<Group>;
impl<G> GenericChatMsg<G> { impl<G> GenericChatMsg<G> {
pub const MAX_BYTES_PLAYER_CHAT_MSG: usize = 256;
pub const NPC_DISTANCE: f32 = 100.0; pub const NPC_DISTANCE: f32 = 100.0;
pub const NPC_SAY_DISTANCE: f32 = 30.0; pub const NPC_SAY_DISTANCE: f32 = 30.0;
pub const REGION_DISTANCE: f32 = 1000.0; pub const REGION_DISTANCE: f32 = 1000.0;

View File

@ -1,7 +1,7 @@
use crate::settings::ModerationSettings; use crate::settings::ModerationSettings;
use authc::Uuid; use authc::Uuid;
use censor::Censor; use censor::Censor;
use common::comp::{AdminRole, ChatType, Group}; use common::comp::{AdminRole, ChatMsg, ChatType, Group};
use hashbrown::HashMap; use hashbrown::HashMap;
use std::{ use std::{
fmt, fmt,
@ -10,8 +10,6 @@ use std::{
}; };
use tracing::info; use tracing::info;
pub const MAX_BYTES_CHAT_MSG: usize = 256;
pub enum ActionNote { pub enum ActionNote {
SpamWarn, SpamWarn,
} }
@ -45,7 +43,7 @@ impl fmt::Display for ActionErr {
ActionErr::TooLong => write!( ActionErr::TooLong => write!(
f, f,
"Your message was too long, no more than {} characters are permitted.", "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!( ActionErr::SpamMuted(dur) => write!(
f, f,
@ -99,7 +97,7 @@ impl AutoMod {
msg: &str, msg: &str,
) -> Result<Option<ActionNote>, ActionErr> { ) -> Result<Option<ActionNote>, ActionErr> {
// TODO: Consider using grapheme cluster count instead of size in bytes // 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) Err(ActionErr::TooLong)
} else if !self.settings.automod } else if !self.settings.automod
// Is this a private chat message? // Is this a private chat message?

View File

@ -504,7 +504,10 @@ impl<'a> Widget for Chat<'a> {
.set(state.ids.chat_input, ui) .set(state.ids.chat_input, ui)
{ {
input.retain(|c| c != '\n'); 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);
}
} }
} }