Limit nickname length to 50 unicode codepoints

maia arson crimew 2022-01-18 12:55:44 +01:00
parent 152d51f4fc
commit 244145696c
4 changed files with 10 additions and 0 deletions

View File

@ -26,6 +26,7 @@
type="text" type="text"
id="join-session-nickname" id="join-session-nickname"
placeholder="Nickname" placeholder="Nickname"
maxlength="50"
required required
/> />

View File

@ -8,6 +8,7 @@ use warp as warb; // i think it's funny
mod events; mod events;
mod viewer_connection; mod viewer_connection;
mod watch_session; mod watch_session;
mod utils;
use serde::Deserialize; use serde::Deserialize;

6
src/utils.rs Normal file
View File

@ -0,0 +1,6 @@
pub fn truncate_str(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
None => s,
Some((idx, _)) => &s[..idx],
}
}

View File

@ -16,6 +16,7 @@ use warp::ws::{Message, WebSocket};
use crate::{ use crate::{
events::{WatchEvent, WatchEventData}, events::{WatchEvent, WatchEventData},
utils::truncate_str,
watch_session::{get_session, handle_watch_event_data}, watch_session::{get_session, handle_watch_event_data},
}; };
@ -53,6 +54,7 @@ pub async fn ws_subscribe(session_uuid: Uuid, nickname: String, colour: String,
if !colour.len() == 6 || !colour.chars().all(|x| x.is_ascii_hexdigit()) { if !colour.len() == 6 || !colour.chars().all(|x| x.is_ascii_hexdigit()) {
colour = String::from("7ed0ff"); colour = String::from("7ed0ff");
} }
let nickname = truncate_str(&nickname, 50).to_string();
CONNECTED_VIEWERS.write().await.insert( CONNECTED_VIEWERS.write().await.insert(
viewer_id, viewer_id,