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

ambee/giterated

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

Change forwarding

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨2556801

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