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

ambee/giterated-api

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

Fixes!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨46cd262

⁨src/daemon_backend.rs⁩ - ⁨3772⁩ bytes
Raw
1 use std::fmt::Debug;
2
3 use futures_util::{SinkExt, StreamExt};
4 use giterated_models::{
5 authenticated::Authenticated,
6 error::OperationError,
7 message::GiteratedMessage,
8 object::{GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse},
9 object_backend::ObjectBackend,
10 operation::GiteratedOperation,
11 };
12 use serde::de::DeserializeOwned;
13 use tokio_tungstenite::tungstenite::Message;
14
15 use crate::{DaemonConnectionPool, Socket};
16
17 #[async_trait::async_trait]
18 impl ObjectBackend for DaemonConnectionPool {
19 async fn object_operation<O: GiteratedObject + Debug, D: GiteratedOperation<O> + Debug>(
20 &self,
21 object: O,
22 operation: &str,
23 payload: D,
24 ) -> Result<D::Success, OperationError<D::Failure>> {
25 let message = GiteratedMessage {
26 object,
27 operation: operation.to_string(),
28 payload,
29 };
30
31 let mut connection = self
32 .0
33 .get()
34 .await
35 .map_err(|e| OperationError::Internal(e.to_string()))?;
36
37 let authenticated = Authenticated::new(message);
38
39 send_expect(&mut connection, authenticated).await
40 }
41
42 async fn get_object<O: GiteratedObject + Debug>(
43 &self,
44 object_str: &str,
45 ) -> Result<Object<O, Self>, OperationError<ObjectRequestError>> {
46 let operation = ObjectRequest(object_str.to_string());
47 info!("Get object: {:?}", operation);
48 let message = GiteratedMessage {
49 object: self.0.manager().target_instance.clone(),
50 operation: ObjectRequest::operation_name().to_string(),
51 payload: operation,
52 };
53
54 let mut connection = self
55 .0
56 .get()
57 .await
58 .map_err(|e| OperationError::Internal(e.to_string()))?;
59
60 let authenticated = Authenticated::new(message);
61
62 let object_raw: ObjectResponse = send_expect(&mut connection, authenticated).await?;
63 Ok(unsafe {
64 Object::new_unchecked(
65 O::from_str(&object_raw.0)
66 .map_err(|_e| OperationError::Internal("heck".to_string()))?,
67 self.clone(),
68 )
69 })
70 }
71 }
72
73 async fn send_expect<
74 O: GiteratedObject,
75 D: GiteratedOperation<O>,
76 B: DeserializeOwned,
77 R: DeserializeOwned,
78 >(
79 socket: &mut Socket,
80 message: Authenticated<O, D>,
81 ) -> Result<R, OperationError<B>> {
82 let payload = bincode::serialize(&message.into_payload()).unwrap();
83
84 socket
85 .send(Message::Binary(payload))
86 .await
87 .map_err(|e| OperationError::Internal(e.to_string()))?;
88
89 while let Some(message) = socket.next().await {
90 let payload = match message.map_err(|e| OperationError::Internal(e.to_string()))? {
91 Message::Binary(payload) => payload,
92 _ => {
93 continue;
94 }
95 };
96
97 let raw_result = bincode::deserialize::<Result<Vec<u8>, OperationError<Vec<u8>>>>(&payload)
98 .map_err(|e| OperationError::Internal(e.to_string()))?;
99
100 // Map ok
101 let raw_result = match raw_result {
102 Ok(raw) => Ok(serde_json::from_slice(&raw)
103 .map_err(|e| OperationError::Internal(e.to_string()))?),
104 Err(err) => Err(match err {
105 OperationError::Operation(err) => OperationError::Operation(
106 serde_json::from_slice(&err)
107 .map_err(|e| OperationError::Internal(e.to_string()))?,
108 ),
109 OperationError::Internal(err) => OperationError::Internal(err),
110 OperationError::Unhandled => OperationError::Unhandled,
111 }),
112 };
113
114 return raw_result;
115 }
116
117 panic!()
118 }
119