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(
|
let (message_id, sent_at): (i64, String) = match conn.query_row(
|
||||||
"INSERT INTO message (sender_id, content, conversation_id) VALUES (?1, ?2, ?3)",
|
"INSERT INTO message (sender_id, content, conversation_id)
|
||||||
params![&user_id, &payload.message, &payload.conv_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();
|
};
|
||||||
|
//let message_id = conn.last_insert_rowid();
|
||||||
// FIXME: add sent_at and message id in the response.
|
// FIXME: add sent_at and message id in the response.
|
||||||
match result {
|
|
||||||
Ok(rows) if rows > 0 => {
|
|
||||||
// --- send to conversation participants ---
|
// --- send to conversation participants ---
|
||||||
let mut stmt_participants = conn
|
let mut stmt_participants = conn
|
||||||
.prepare("SELECT user_id FROM conversation_participants WHERE conversation_id = ?1")
|
.prepare("SELECT user_id FROM conversation_participants WHERE conversation_id = ?1")
|
||||||
@@ -326,25 +339,26 @@ pub async fn send_message(
|
|||||||
let update_msg = serde_json::json!({
|
let update_msg = serde_json::json!({
|
||||||
"event_type": "chat_message",
|
"event_type": "chat_message",
|
||||||
"conv_id": payload.conv_id,
|
"conv_id": payload.conv_id,
|
||||||
"sender": user_id,
|
"sender_id": user_id,
|
||||||
"content": payload.message,
|
"content": payload.message,
|
||||||
|
"id": message_id,
|
||||||
|
"sent_at": sent_at,
|
||||||
})
|
})
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
for uid in &participant_ids {
|
for uid in &participant_ids {
|
||||||
if let Some(sender) = hotel_users.get(&uid) {
|
if let Some(sender) = hotel_users.get(uid) {
|
||||||
let _ = sender.send(axum::extract::ws::Message::Text(update_msg.clone().into()));
|
let _ = sender.send(
|
||||||
|
axum::extract::ws::Message::Text(update_msg.clone().into())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(StatusCode::OK, format!("sent message: {}, to users:{:?}", payload.message, participant_ids))
|
(StatusCode::OK, format!("sent message: {}, to users:{:?}", payload.message, participant_ids))
|
||||||
}
|
}
|
||||||
Ok(_) => (StatusCode::NOT_FOUND, "Conversation not found".to_string()),
|
//Ok(_) => (StatusCode::NOT_FOUND, "Conversation not found".to_string()),
|
||||||
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Error from DB: {err}")),
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
|
|||||||
Reference in New Issue
Block a user