JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Add message forwarding

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨e4fa992

⁨giterated-daemon/src/connection/forwarded.rs⁩ - ⁨1945⁩ bytes
Raw
1 use futures_util::{SinkExt, StreamExt};
2 use giterated_api::DaemonConnectionPool;
3 use giterated_models::{messages::error::ConnectionError, model::authenticated::Authenticated};
4 use serde::Serialize;
5 use tokio_tungstenite::tungstenite::Message;
6
7 pub async fn wrap_forwarded<T: Serialize>(
8 pool: &DaemonConnectionPool,
9 message: Authenticated<T>,
10 ) -> Message {
11 let connection = pool.get().await;
12
13 let mut connection = match connection {
14 Ok(connection) => connection,
15 Err(e) => {
16 return Message::Binary(serde_json::to_vec(&ConnectionError(e.to_string())).unwrap())
17 }
18 };
19
20 let send_result = connection
21 .send(Message::Binary(serde_json::to_vec(&message).unwrap()))
22 .await;
23
24 if let Err(e) = send_result {
25 return Message::Binary(serde_json::to_vec(&ConnectionError(e.to_string())).unwrap());
26 }
27
28 loop {
29 let message = connection.next().await;
30
31 match message {
32 Some(Ok(message)) => {
33 match message {
34 Message::Binary(payload) => return Message::Binary(payload),
35 Message::Ping(_) => {
36 let _ = connection.send(Message::Pong(vec![])).await;
37 continue;
38 }
39 Message::Close(_) => {
40 return Message::Binary(
41 String::from("The instance you wanted to talk to hung up on me :(")
42 .into_bytes(),
43 )
44 }
45 _ => continue,
46 };
47 }
48 Some(Err(e)) => {
49 return Message::Binary(
50 serde_json::to_vec(&ConnectionError(e.to_string())).unwrap(),
51 )
52 }
53 _ => {
54 info!("Unhandled");
55 continue;
56 }
57 }
58 }
59
60 todo!()
61 }
62