From 91ae1e4e75ad251f608fa62ca9ff2ef2963a6048 Mon Sep 17 00:00:00 2001 From: Romain Mallard Date: Fri, 26 Sep 2025 04:47:42 +0200 Subject: [PATCH] websocket for sending chat messages. --- db/1.sqlite | Bin 40960 -> 40960 bytes src/chat/handlers.rs | 62 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/db/1.sqlite b/db/1.sqlite index 9dcef9e8b61225675d98cf87e8d47cdf1b07f663..b67f55d89188480caef8cdf623df9f93c6355a29 100644 GIT binary patch delta 271 zcmZoTz|?SnX@WGP`a~ILM)i#eP46by{4Ow6qe3=G+^NSIj}85pu5OK^d87+G4Gm~8&z z$0{hyf0lune+C2pbN_i!7M%j%CP4 0 => { + // --- send to conversation participants --- + let mut stmt_participants = conn + .prepare("SELECT user_id FROM conversation_participants WHERE conversation_id = ?1") + .expect("prepare participants failed"); + + let participant_ids: Vec = stmt_participants + .query_map(params![payload.conv_id], |row| row.get(0)) + .expect("query_map failed") + .filter_map(Result::ok) + .collect(); + + if let Some(hotel_users) = state.ws_map.get(&hotel_id) { + let update_msg = serde_json::json!({ + "conv_id": payload.conv_id, + "sender": user_id, + "content": payload.message, + }) + .to_string(); + + for uid in &participant_ids { + if let Some(sender) = hotel_users.get(&uid) { + let _ = sender.send(axum::extract::ws::Message::Text(update_msg.clone().into())); + } + } + } + + (StatusCode::OK, format!("sent message: {}, to users:{:?}", payload.message, participant_ids)) + } + Ok(_) => (StatusCode::NOT_FOUND, "Conversation not found".to_string()), + Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB: {err}")), + } +/* match result { Ok(rows) if rows > 0 => (StatusCode::OK, "added message succesfully".to_string()), Ok(_) => (StatusCode::NOT_FOUND, "not able to add the message, conversation may not exist".to_string() ), Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error when adding the message : {err}")), } +*/ + } - #[derive(Debug, Serialize)] - struct Message { - id: i32, - sender_id: i32, - content: String, - sent_at: String, - } +#[derive(Debug, Serialize)] +struct Message { + id: i32, + sender_id: i32, + content: String, + sent_at: String, +} pub async fn get_message(