fix + refac send message WS payload
This commit is contained in:
BIN
db/1.sqlite-wal
BIN
db/1.sqlite-wal
Binary file not shown.
Binary file not shown.
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user