websocket for sending chat messages.

This commit is contained in:
2025-09-26 04:47:42 +02:00
parent a3b55f00df
commit 91ae1e4e75
2 changed files with 51 additions and 11 deletions

View File

@@ -2,12 +2,15 @@
use axum::{
extract::{
FromRequest,FromRequestParts, State,
}, http::StatusCode, response::{sse::KeepAlive, IntoResponse}
}, http::StatusCode, response::{sse::KeepAlive, IntoResponse},
};
//use axum::extract::ws::Message;
use chrono::NaiveDateTime;
use rusqlite::params;
use serde::Serialize;
use serde_json::json;
use crate::chat::extractor::{
AddUserConversationPayload, CreateConversationPayload, GetMessagesPayload, SendMessagePayload
@@ -112,10 +115,11 @@ pub async fn send_message(
};
if !statement.exists(params![user_id, payload.conv_id])
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Query failed".to_string())).unwrap()
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Query failed".to_string()))
.unwrap()
{
// Early exit if not creator
return ((StatusCode::FORBIDDEN, "Not the creator".to_string()));
return ((StatusCode::FORBIDDEN, "Not part of the conversation".to_string()));
}
let result = conn.execute(
@@ -123,21 +127,57 @@ pub async fn send_message(
params![&user_id, &payload.message, &payload.conv_id],
);
match result {
Ok(rows) if rows > 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<i32> = 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(