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

ambee/giterated-api

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

Fixes schmixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨92ff417

⁨src/daemon_backend.rs⁩ - ⁨3070⁩ 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: D,
23 ) -> Result<D::Success, OperationError<D::Failure>> {
24 let message = GiteratedMessage {
25 object,
26 operation: D::operation_name().to_string(),
27 payload: operation,
28 };
29
30 let mut connection = self
31 .0
32 .get()
33 .await
34 .map_err(|e| OperationError::Internal(e.to_string()))?;
35
36 let authenticated = Authenticated::new(message);
37
38 send_expect(&mut connection, authenticated).await
39 }
40
41 async fn get_object<O: GiteratedObject + Debug>(
42 &self,
43 object_str: &str,
44 ) -> Result<Object<O, Self>, OperationError<ObjectRequestError>> {
45 let operation = ObjectRequest(object_str.to_string());
46 info!("Get object: {:?}", operation);
47 let message = GiteratedMessage {
48 object: self.0.manager().target_instance.clone(),
49 operation: ObjectRequest::operation_name().to_string(),
50 payload: operation,
51 };
52
53 let mut connection = self
54 .0
55 .get()
56 .await
57 .map_err(|e| OperationError::Internal(e.to_string()))?;
58
59 let authenticated = Authenticated::new(message);
60
61 let object_raw: ObjectResponse = send_expect(&mut connection, authenticated).await?;
62 Ok(unsafe {
63 Object::new_unchecked(
64 O::from_str(&object_raw.0)
65 .map_err(|_e| OperationError::Internal("heck".to_string()))?,
66 self.clone(),
67 )
68 })
69 }
70 }
71
72 async fn send_expect<
73 O: GiteratedObject,
74 D: GiteratedOperation<O>,
75 B: DeserializeOwned,
76 R: DeserializeOwned,
77 >(
78 socket: &mut Socket,
79 message: Authenticated<O, D>,
80 ) -> Result<R, OperationError<B>> {
81 let payload = bincode::serialize(&message.into_payload()).unwrap();
82
83 socket
84 .send(Message::Binary(payload))
85 .await
86 .map_err(|e| OperationError::Internal(e.to_string()))?;
87
88 while let Some(message) = socket.next().await {
89 let payload = match message.map_err(|e| OperationError::Internal(e.to_string()))? {
90 Message::Binary(payload) => payload,
91 _ => {
92 continue;
93 }
94 };
95
96 let _as_target = serde_json::from_slice::<R>(&payload)
97 .map_err(|e| OperationError::Internal(e.to_string()))?;
98 }
99
100 panic!()
101 }
102