diff --git a/db/1.sqlite-wal b/db/1.sqlite-wal index 802d13b..c35f57f 100644 Binary files a/db/1.sqlite-wal and b/db/1.sqlite-wal differ diff --git a/db/auth_copy_2.sqlite-shm b/db/auth_copy_2.sqlite-shm deleted file mode 100644 index fe9ac28..0000000 Binary files a/db/auth_copy_2.sqlite-shm and /dev/null differ diff --git a/db/auth_copy_2.sqlite-wal b/db/auth_copy_2.sqlite-wal deleted file mode 100644 index e69de29..0000000 diff --git a/src/chat/handlers.rs b/src/chat/handlers.rs index 27a0145..842f1c1 100644 --- a/src/chat/handlers.rs +++ b/src/chat/handlers.rs @@ -302,15 +302,28 @@ pub async fn send_message( } } - let result = conn.execute( - "INSERT INTO message (sender_id, content, conversation_id) VALUES (?1, ?2, ?3)", - params![&user_id, &payload.message, &payload.conv_id], - ); - - let message_id = conn.last_insert_rowid(); +let (message_id, sent_at): (i64, String) = match conn.query_row( + "INSERT INTO message (sender_id, content, conversation_id) + VALUES (?1, ?2, ?3) + RETURNING id, sent_at", + params![user_id, payload.message, payload.conv_id], + |row| Ok(( + row.get::<_, i64>(0)?, + row.get::<_, String>(1)?, + )), +) { + Ok(v) => v, + Err(err) => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("DB insert failed: {err}"), + ); + } +}; + //let message_id = conn.last_insert_rowid(); // FIXME: add sent_at and message id in the response. - 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") @@ -322,29 +335,30 @@ pub async fn send_message( .filter_map(Result::ok) .collect(); - if let Some(hotel_users) = state.ws_map.get(&hotel_id) { - let update_msg = serde_json::json!({ - "event_type": "chat_message", - "conv_id": payload.conv_id, - "sender": user_id, - "content": payload.message, - }) - .to_string(); +if let Some(hotel_users) = state.ws_map.get(&hotel_id) { + let update_msg = serde_json::json!({ + "event_type": "chat_message", + "conv_id": payload.conv_id, + "sender_id": user_id, + "content": payload.message, + "id": message_id, + "sent_at": sent_at, + }) + .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())); - } - } - } + 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}")), - } + //Ok(_) => (StatusCode::NOT_FOUND, "Conversation not found".to_string()), -} #[derive(Debug, Serialize)]