Fix notify sending TokenAlert to incorrect session id, use 'notify' id for TokenAlert name

This commit is contained in:
2024-12-23 19:36:51 -06:00
parent b5a5c47ece
commit de3dacda77
2 changed files with 29 additions and 20 deletions

View File

@@ -106,7 +106,9 @@ async fn handle_socket(session_id: u32, websocket: WebSocket) {
session.tx = Some(tx_channel); session.tx = Some(tx_channel);
session.send_state(); session.send_state();
session.send_message(executable_message); session
.send_message(executable_message)
.expect("Failed to buffer executables message");
// Handle incoming messages // Handle incoming messages
let fut = async move { let fut = async move {
@@ -207,23 +209,22 @@ pub async fn notify(req: &mut Request, res: &mut Response, depot: &mut Depot) {
match key { match key {
Some(key) => { Some(key) => {
// Parse key into u32 // Parse key into u32
let key = key.parse::<u32>(); let key = match key.parse::<u32>() {
if key.is_err() { Ok(k) => k,
res.status_code(StatusCode::BAD_REQUEST); Err(e) => {
return; tracing::error!("Error parsing key: {}", e);
} res.status_code(StatusCode::BAD_REQUEST);
return;
}
};
let store = &mut *STORE.lock().await; let store = &mut *STORE.lock().await;
let session_id = get_session_id(req, depot).unwrap(); let session = store.sessions.get_mut(&key).expect("Session not found");
let session = store
.sessions
.get_mut(&session_id)
.expect("Session not found");
let message = OutgoingMessage::TokenAlert { let message = OutgoingMessage::TokenAlert { token: key };
token: key.unwrap(), session
}; .send_message(message)
session.send_message(message); .expect("Failed to buffer token alert message");
res.render("Notification sent"); res.render("Notification sent");
} }

View File

@@ -51,9 +51,10 @@ impl Session {
return self.downloads.last().unwrap(); return self.downloads.last().unwrap();
} }
// This function's failure is not a failure to transmit the message, but a failure to buffer it into the channel (or any preceding steps).
pub fn send_message(&mut self, message: OutgoingMessage) -> Result<(), anyhow::Error> { pub fn send_message(&mut self, message: OutgoingMessage) -> Result<(), anyhow::Error> {
if self.tx.is_none() { if self.tx.is_none() {
return Err(anyhow::anyhow!("Session has no sender")); return Err(anyhow::anyhow!("Session {} has no sender", self.id));
} }
// TODO: Error handling // TODO: Error handling
@@ -210,13 +211,20 @@ pub enum IncomingMessage {
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "lowercase")] #[serde(tag = "type", rename_all = "kebab-case")]
pub enum OutgoingMessage { pub enum OutgoingMessage {
// An alert to the client that a session download has been used. // An alert to the client that a session download has been used.
TokenAlert { token: u32 }, #[serde(rename = "notify")]
TokenAlert {
token: u32,
},
// A message describing the current session state // A message describing the current session state
State { session: Session }, State {
Executables { executables: Vec<ExecutableJson> }, session: Session,
},
Executables {
executables: Vec<ExecutableJson>,
},
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]