use futures_util::{SinkExt, StreamExt}; use giterated_api::DaemonConnectionPool; use giterated_models::{ messages::error::ConnectionError, model::authenticated::{Authenticated, AuthenticatedPayload}, }; use serde::Serialize; use tokio_tungstenite::tungstenite::Message; pub async fn wrap_forwarded(pool: &DaemonConnectionPool, message: AuthenticatedPayload) -> Message { let connection = pool.get().await; let mut connection = match connection { Ok(connection) => connection, Err(e) => { return Message::Binary(serde_json::to_vec(&ConnectionError(e.to_string())).unwrap()) } }; let send_result = connection .send(Message::Binary(serde_json::to_vec(&message).unwrap())) .await; if let Err(e) = send_result { return Message::Binary(serde_json::to_vec(&ConnectionError(e.to_string())).unwrap()); } loop { let message = connection.next().await; match message { Some(Ok(message)) => { match message { Message::Binary(payload) => return Message::Binary(payload), Message::Ping(_) => { let _ = connection.send(Message::Pong(vec![])).await; continue; } Message::Close(_) => { return Message::Binary( String::from("The instance you wanted to talk to hung up on me :(") .into_bytes(), ) } _ => continue, }; } Some(Err(e)) => { return Message::Binary( serde_json::to_vec(&ConnectionError(e.to_string())).unwrap(), ) } _ => { info!("Unhandled"); continue; } } } todo!() }